【问题标题】:JPA/Hibernate/HSQLDB querying but not assigning sub-objectJPA/Hibernate/HSQLDB 查询但不分配子对象
【发布时间】:2012-03-02 16:29:27
【问题描述】:

我在使用这个查询时遇到了问题,但仅限于 HSQLDB 下。我们将 Oracle 用于生产数据库,将 HSQL 用于自动化集成测试。这是我的主要目标:

@Entity
@Table(name="STUDENTS")
@org.hibernate.annotations.Proxy(lazy=false)
public class Student implements Serializable {
...
   @OneToMany(fetch=FetchType.LAZY)
   @JoinColumn(name="STUDENTID",referencedColumnName="ID")
   private Set<StudentRace> races;
...
}

StudentRace 看起来像这样:

@Entity
@Table(name="STUDENTRACE")
@org.hibernate.annotations.Proxy(lazy=false)
public class StudentRace implements Serializable {
...
    @Column(name="STUDENTID")
    private Integer studentid;
...
}

而我的 JPA 查询是这样的:

entityManager.createQuery("select distinct s from Student s left join fetch s.races "+
  "where s.schoolNumber = :schoolNumber");

我知道我在 HSQLDB 数据库中有正确的数据 - 我可以手动进行查询并查看数据。然而,学生对象总是将“种族”设为空。正如我所说,这个查询在 Oracle 中运行良好。我缺少某种 HSQLDB 设置吗?

编辑:像这样?在学生中:

@OneToMany(fetch=FetchType.LAZY, mappedBy="id")
private Set<StudentRace> races;

在学生竞赛中:

@ManyToOne
@JoinColumn(name="STUDENTID")
private Student student;

仍然没有运气。 “races”元素仍然为空。

【问题讨论】:

    标签: hibernate jpa hsqldb


    【解决方案1】:

    您的映射不正确,因为STUDENTRACE.STUDENTID 列被映射了两次:一次作为Student 中的JoinColumn,一次作为StudentRace 中的列。

    要么使关联单向并删除StudentRace 中的studentid 字段,要么使其成为双向,并在StudentRace 中具有Student student 字段,映射为ManyToOne,如this example from the Hibernate documentation 所示。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2011-11-08
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-04-02
      • 2015-03-14
      • 1970-01-01
      相关资源
      最近更新 更多