【问题标题】:Creating a Hibernate Criteria that can filter by referenced entity创建一个可以按引用实体过滤的 Hibernate Criteria
【发布时间】:2011-08-11 23:05:23
【问题描述】:

我有一个以 PersonEntity 和 QuestionEntity 作为其主键的实体 (PersonQuestionsEntity)。我使用复合键来反映这种关系。

现在,我想创建一个 Criteria 对象,它可以执行以下操作:查找给定 questionId 和一个人的年龄的所有 PersonQuestion 实体。

这是我为此创建标准的尝试:

    Session session = getHibernateTemplate().getSessionFactory().openSession();
    Criteria criteria = session.createCriteria(PersonQuestionsEntity.class);
    criteria.add(Restrictions.eq("question.questionId", "87"));
    criteria = criteria.createCriteria("person");
    criteria.add(Restrictions.eq("age", 23));
    criteria.setMaxResults(100);
    List l = criteria.list();

问题是我得到这个错误:

Caused by: java.sql.SQLException: ORA-00904: "PERSONENTI1_"."AGE": invalid identifier

在生成的 SQL 中,此人似乎被引用为 PERSONENTI4,而不是 PERSONENTI1。如果我复制 SQL 并使用 PERSONENTIT4 而不是 PERSONENTI4 运行它,它可以工作(有点——它似乎在做某种笛卡尔连接)。

关于我可能做错了什么的任何线索?我对使用 Hibernate 很陌生。

PersonQuestionsEntity

@Entity
@IdClass(com.anonymous.model.PersonQuestionsKey.class)
@Table(name = "PERSON_QUESTIONS")
public class PersonQuestionsEntity implements Serializable
{
    private static final long serialVersionUID = -8254277382097937813L;

    @Id
    @ManyToOne
    @JoinColumn(name = "USER_NAME", nullable = false)
    private PersonEntity person;

    @Id
    @ManyToOne
    @JoinColumn(name = "QUESTION_ID", nullable = false)
    private QuestionEntity question;

    @Column(name = "THEIR_ANSWER")
    private int theirAnswer;
}

个人实体

@Entity
@Table(name = "PERSON")

public class PersonEntity implements Serializable
{
    private static final long serialVersionUID = -1699435979266209440L;

    @Id
    @Column(name = "USER_NAME", length = 20, nullable = false)
    private String userName;

    @Column(name = "USER_NAME_REAL", length = 20, nullable = false)
    private String userNameReal;

    @Column(name = "AGE", nullable = false)
    private int age;
}

PersonQuestionsKey

   @Embeddable
    public class PersonQuestionsKey implements Serializable
    {
        private static final long serialVersionUID = -264160855961369405L;

        @Id
        @ManyToOne
        @JoinColumn(name = "USER_NAME", nullable = false)
        private PersonEntity person;

        @Id
        @ManyToOne
        @JoinColumn(name = "QUESTION_ID", nullable = false)
        private QuestionEntity question;

}

【问题讨论】:

  • 你是怎么解决这个问题的?
  • @MikhailBatcer 老实说我不记得了。今晚或明天我会检查代码,看看我是否能弄清楚我做了什么——已经有一段时间了,这只是一个爱好项目。

标签: java hibernate persistence criteria


【解决方案1】:

首先,你并不需要内在的标准,只需使用:

条件标准 = session.createCriteria(PersonQuestionsEntity.class); criteria.add(Restrictions.eq("question.questionId", "87")); criteria.add(Restrictions.eq("person.age", 23)); 标准.setMaxResults(100); 列表 l = criteria.list();

其次(关于连接类型),在这种情况下,我通常使用产生内部连接的 HQL。 HQL 可能如下所示:

来自 PersonQeustionEntity 其中 question.questionId = :questionId 和 person.age = :age

在结果Query 对象中,您可以将参数questionIdage 设置为您想要的输入。

【讨论】:

  • 我想使用 Criteria,因为我想为过滤结果提供许多不同的选项(真实的 PersonEntity 比这个缩短的字段多得多)。另外,我尝试了您刚开始时显示的代码,但没有成功。我收到此错误:org.hibernate.QueryException:无法解析属性:person.age of:com.anonymous.model.PersonQuestionsEntity。我想知道我对复合键的使用是否搞砸了?
  • 如果您只使用 questionId 标准,它是否工作正常?你写的错误看起来你的实体映射确实有问题,但我不能确定这是因为你使用的复合键。
  • 如果我只使用 questionId 标准,它确实可以正常工作。我回家后必须仔细检查,但我认为此人的用户名标准也有效。
  • createCriteria 自 5.2 起已弃用。有没有和这里展示的一样干净的替代品?手册中显示的方法相当丑陋...
猜你喜欢
  • 2020-01-24
  • 1970-01-01
  • 2018-07-04
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2013-02-04
  • 1970-01-01
相关资源
最近更新 更多