JPA或Hibernate或Spring注释来指定关系的查询?
我可以使用JPA或Hibernate注释显式指定要运行的查询来检索关联对象吗 具体而言,我拥有以下实体: 导师, 学生, 以及导师和学生之间明确的一对一关系,导师:JPA或Hibernate或Spring注释来指定关系的查询?,hibernate,jpa,Hibernate,Jpa,我可以使用JPA或Hibernate注释显式指定要运行的查询来检索关联对象吗 具体而言,我拥有以下实体: 导师, 学生, 以及导师和学生之间明确的一对一关系,导师: @Entity public class Student { @OneToMany Collection<Tutorial> tutorials; } @Entity public class Tutor { @OneToMany Collection<Tutorial> tutorials; }
@Entity
public class Student {
@OneToMany Collection<Tutorial> tutorials;
}
@Entity
public class Tutor {
@OneToMany Collection<Tutorial> tutorials;
}
@Entity
public class Tutorial {
@OneToOne Tutor tutor;
@OneToOne Student student;
// other data
}
现在我想检索学生的所有家庭作业:
@Entity
public class Student {
@OneToMany Collection<Tutorial> tutorials;
@OneToMany Collection<Homework> homework;
}
我可以直接在student类中使用JPA、Hibernate或Spring注释来表示这一点,以便在调用student.getHomegram()时使用它吗
换句话说,我想要的是:
@Entity
public class Student {
@OneToMany Collection<Tutorial> tutorials;
@ExplicitQuery("select Homework where Homework.student = this or something")
@OneToMany Collection<Homework> homework;
}
@实体
公立班学生{
@OneToMany收藏教程;
@明确查询(“选择家庭作业,其中家庭作业.student=this或某事”)
@一次收集作业;
}
您应该能够在Hibernate中使用
@实体
@NamedNativeRequesty(name=“customHomeworkLoader”,query=“从作业a中选择a.*加入(a.Tutorial\u id=b.id)上的教程b,其中b.student\u id=?”,resultClass=journal.class)
公立班学生{
@OneToMany收藏教程;
@加载程序(namedQuery=“customHomeworkLoader”)
@一次收集作业;
}
命名查询中的位置参数(?
)应在运行时自动替换为主键。我没有尝试过这段代码,所以不能保证它会工作,但希望它能给你一个提示,让你以何种方式继续
select a.*
from Homework a
join Tutorial b on (a.tutorial_id = b.id)
where b.student_id = ?student.id?
@Entity
public class Student {
@OneToMany Collection<Tutorial> tutorials;
@ExplicitQuery("select Homework where Homework.student = this or something")
@OneToMany Collection<Homework> homework;
}
@Entity
@NamedNativeQuery(name="customHomeworkLoader", query="select a.* from Homework a join Tutorial b on (a.tutorial_id = b.id) where b.student_id = ?", resultClass = Homework.class)
public class Student {
@OneToMany Collection<Tutorial> tutorials;
@Loader(namedQuery = "customHomeworkLoader")
@OneToMany Collection<Homework> homework;
}