首页 文章

JPA / Hibernate / HSQLDB查询但不分配子对象

提问于
浏览
0

我遇到此查询的问题,但仅限于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数据库中有正确的数据 - 我可以手动执行查询并查看数据 . 然而,Student对象的“races”总是为null . 正如我所说,这个查询在Oracle中运行良好 . 我缺少某种HSQLDB设置吗?

编辑:喜欢这个?在学生中:

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

在StudentRace中:

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

仍然没有运气 . “races”元素仍为null .

1 回答

  • 1

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

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

相关问题