提取的所有者不在场
Owner of the fetching not present
我正在尝试在这里学习休眠并遇到这个 Class:
HelloWorldClient.java
public class HelloWorldClient {
public static void main(String[] args) {
EntityManagerFactory emf =
Persistence.createEntityManagerFactory("hello-world");
EntityManager em = emf.createEntityManager();
EntityTransaction txn = em.getTransaction();
try {
txn.begin();
Query query = em.createQuery("select student from Guide guide join fetch
guide.students student");
List<Student> students = query.getResultList();
System.out.println(students);
txn.commit();
} catch (Exception e) {
if (txn != null) {
txn.rollback();
}
e.printStackTrace();
} finally {
if (em != null) {
em.close();
}
}
}
}
执行查询时出现错误:
java.lang.IllegalArgumentException: org.hibernate.QueryException: query
specified join fetching, but the owner of the fetched association was not
present in the select list [FromElement{explicit,not a collection join,fetch
join,fetch non-lazy properties,classAlias=student,role=entity.Guide.students,tableName=Student,tabl
eAlias=students1_,origin=Guide guide0_,columns={guide0_.id ,className=entity.Student}}] [select student from entity.Guide guide join fetch
guide.students student]at Impl.java:294)
at client.HelloWorldClient.main(HelloWorldClient.java:31)
学生和向导之间存在多对一关系..
注意:我知道用 guide 替换 select 查询中的 student 可以解决问题,但我试图找出为什么它不能反过来工作。
Join fetch 在这里没有意义,因为它是一个强制预先加载关系的性能提示。
这里你实际上是在告诉hibernate:
从 Guide join Student
table 中获取所有学生,并确保在 Guide
实体中预先获取对 Student
的引用。但是你没有Guide
实体
如果您只想要学生 select 来自他们 table 的学生,如果您需要与 Guide
的关系,请声明它。
select student from Student student join fetch
student.guide guide
或者在这种情况下完全删除提取
我正在尝试在这里学习休眠并遇到这个 Class:
HelloWorldClient.java
public class HelloWorldClient {
public static void main(String[] args) {
EntityManagerFactory emf =
Persistence.createEntityManagerFactory("hello-world");
EntityManager em = emf.createEntityManager();
EntityTransaction txn = em.getTransaction();
try {
txn.begin();
Query query = em.createQuery("select student from Guide guide join fetch
guide.students student");
List<Student> students = query.getResultList();
System.out.println(students);
txn.commit();
} catch (Exception e) {
if (txn != null) {
txn.rollback();
}
e.printStackTrace();
} finally {
if (em != null) {
em.close();
}
}
}
}
执行查询时出现错误:
java.lang.IllegalArgumentException: org.hibernate.QueryException: query
specified join fetching, but the owner of the fetched association was not
present in the select list [FromElement{explicit,not a collection join,fetch
join,fetch non-lazy properties,classAlias=student,role=entity.Guide.students,tableName=Student,tabl
eAlias=students1_,origin=Guide guide0_,columns={guide0_.id ,className=entity.Student}}] [select student from entity.Guide guide join fetch
guide.students student]at Impl.java:294)
at client.HelloWorldClient.main(HelloWorldClient.java:31)
学生和向导之间存在多对一关系..
注意:我知道用 guide 替换 select 查询中的 student 可以解决问题,但我试图找出为什么它不能反过来工作。
Join fetch 在这里没有意义,因为它是一个强制预先加载关系的性能提示。
这里你实际上是在告诉hibernate:
从 Guide join Student
table 中获取所有学生,并确保在 Guide
实体中预先获取对 Student
的引用。但是你没有Guide
实体
如果您只想要学生 select 来自他们 table 的学生,如果您需要与 Guide
的关系,请声明它。
select student from Student student join fetch
student.guide guide
或者在这种情况下完全删除提取