【问题标题】:Firing HQL in ManyToOne relationship在多对一关系中触发 HQL
【发布时间】:2015-02-16 01:29:55
【问题描述】:

我有具有 OneToMany 关系的 College 和 Student 实体:

@Entity
public class College {

    @Id
    @GeneratedValue
    private int collegeId;

    private String collegeName;

    @OneToMany

    private Collection<Student> students = new ArrayList<Student>();

    public int getCollegeId() {
        return collegeId;
    }

    public void setCollegeId(int collegeId) {
        this.collegeId = collegeId;
    }

    public String getCollegeName() {
        return collegeName;
    }

    public void setCollegeName(String collegeName) {
        this.collegeName = collegeName;
    }

    public Collection<Student> getStudents() {
        return students;
    }
    public void setStudents(Collection<Student> students) {
        this.students = students;
    }
}

@Entity
public class Student {

    @Id
    @GeneratedValue
    private int studentId;

    private String studentName;


    public int getStudentId() {
        return studentId;
    }

    public void setStudentId(int studentId) {
        this.studentId = studentId;
    }

    public String getStudentName() {
        return studentName;
    }

    public void setStudentName(String studentName) {
        this.studentName = studentName;
    }

}

Student 表中有一个指向大学的外键。

如果我想从特定大学获取所有学生,那么在原生 SQL 中,我可以使用以下查询来完成:

Select * from student where collegeId=1

是否可以通过从学生实体而不是大学实体中选择来实现与 HQL 相同的效果。

我的实用类:

public class ManyToOne {

    public static void main(String[] args) {
        EntityManagerFactory emf = Persistence.createEntityManagerFactory("org.hibernate.examples");

        EntityManager em = emf.createEntityManager();
        College college1 = new College();
        college1.setCollegeName("College1");


        College college2 = new College();
        college2.setCollegeName("College2");

        Student student1 = new Student();
        student1.setStudentName("std1");

        college1.getStudents().add(student1);

        Student student2 = new Student();
        student2.setStudentName("std2");

        college2.getStudents().add(student2);

        Student student3 = new Student();
        student3.setStudentName("std3");

        college1.getStudents().add(student3);

        Student student4 = new Student();
        student4.setStudentName("std4");

        college1.getStudents().add(student4);

        em.getTransaction().begin();

        em.persist(college1);
        em.persist(college2);
        em.persist(student1);
        em.persist(student2);
        em.persist(student3);
        em.persist(student4);

        em.getTransaction().commit();
        em.close();

        em = emf.createEntityManager();
        em.getTransaction().begin();

        String queryString = "select students from "+ College.class.getName()+"   where collegeId = 1";

        Query query = em.createQuery(queryString);

        List<Student> students = query.getResultList();
        for(int i=0;i<students.size();i++)
            System.out.println(students.get(i).getStudentName());
        em.getTransaction().commit();
        em.close();
        emf.close();
    }
}

异常堆栈跟踪:

Exception in thread "main" java.lang.IllegalStateException: No data type for node: org.hibernate.hql.internal.ast.tree.IdentNode 
 \-[IDENT] IdentNode: 'students' {originalText=students}

    at org.hibernate.hql.internal.ast.tree.SelectClause.initializeExplicitSelectClause(SelectClause.java:174)
    at org.hibernate.hql.internal.ast.HqlSqlWalker.useSelectClause(HqlSqlWalker.java:924)
    at org.hibernate.hql.internal.ast.HqlSqlWalker.processQuery(HqlSqlWalker.java:692)
    at org.hibernate.hql.internal.antlr.HqlSqlBaseWalker.query(HqlSqlBaseWalker.java:665)
    at org.hibernate.hql.internal.antlr.HqlSqlBaseWalker.selectStatement(HqlSqlBaseWalker.java:301)
    at org.hibernate.hql.internal.antlr.HqlSqlBaseWalker.statement(HqlSqlBaseWalker.java:249)
    at org.hibernate.hql.internal.ast.QueryTranslatorImpl.analyze(QueryTranslatorImpl.java:278)
    at org.hibernate.hql.internal.ast.QueryTranslatorImpl.doCompile(QueryTranslatorImpl.java:206)
    at org.hibernate.hql.internal.ast.QueryTranslatorImpl.compile(QueryTranslatorImpl.java:158)
    at org.hibernate.engine.query.spi.HQLQueryPlan.<init>(HQLQueryPlan.java:131)
    at org.hibernate.engine.query.spi.HQLQueryPlan.<init>(HQLQueryPlan.java:93)
    at org.hibernate.engine.query.spi.QueryPlanCache.getHQLQueryPlan(QueryPlanCache.java:167)
    at org.hibernate.internal.AbstractSessionImpl.getHQLQueryPlan(AbstractSessionImpl.java:301)
    at org.hibernate.internal.AbstractSessionImpl.createQuery(AbstractSessionImpl.java:236)
    at org.hibernate.internal.SessionImpl.createQuery(SessionImpl.java:1800)
    at org.hibernate.jpa.spi.AbstractEntityManagerImpl.createQuery(AbstractEntityManagerImpl.java:328)
    at utils.ManyToOne.main(ManyToOne.java:66)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke(Method.java:483)
    at com.intellij.rt.execution.application.AppMain.main(AppMain.java:134)

【问题讨论】:

  • 这些是你真正的映射吗? @OneToMany 应该有一个mappedBy 属性集,Student 应该与College 有关系。

标签: java hibernate orm


【解决方案1】:

试试这个(刚刚在your other question 看到映射没问题)

String queryString = "select s from Student s where s.college.collegeId = 1";

编辑

关于您的评论,Hibernate 有一个“功能”,如果它找不到具有给定名称的字段,它只会将您提供的任何内容传递给 SQL。在你的情况下,这意味着

String queryString = "from Student where collegeId=1";

由于collegeId不是Student中的字段,所以会照原样传入SQL,从而得到你想要的查询。当然,缺点是将代码与底层数据库模型耦合,绕过 ORM 映射。

【讨论】:

  • :我故意在这个问题中保留了这样的映射。我的问题是这个问题中给出的代码。您提到的另一个问题不同,您的回答解决了它,我已经接受了您的回答。:)
  • 这正是我想要的。 :)
  • 当我尝试执行您提到的查询时遇到以下异常:java.lang.IllegalStateException: No data type for node: org.hibernate.hql.internal.ast.tree.IdentNode。我已经添加了有问题的完整堆栈跟踪。
  • 不确定,但试试这个String queryString = "from Student where collegeId=1";
  • 修复了它。从查询中删除了“select *”,它工作正常。谢谢。
猜你喜欢
  • 2016-09-01
  • 2015-03-31
  • 1970-01-01
  • 2013-09-19
  • 2014-09-03
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2017-12-24
相关资源
最近更新 更多