繁体   English   中英

提取内容的所有者不存在

[英]Owner of the fetching not present

我试图在这里学习休眠,并遇到了这一堂课:

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)

学生和导游有多对一的关系。

注意:我知道将select查询中的student替换为guide可以解决问题,但是我试图找出为什么它不能正常工作的原因。

Join fetch在这里没有意义,因为它是一种性能提示,迫使人们迫切希望加载该关系。

在这里,您实际上是在告诉休眠:

将所有学生从“ Guide join Student表,并确保渴望获取对Guide实体中Guide Student Guide的引用。 但是您没有Guide实体

如果您想让学生从表中选择学生,并且如果您需要与Guide获取的Guide的关系,则将其声明。

select student from Student student join fetch 
student.guide guide

或在这种情况下完全删除抓取

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM