0

我有这种情况。想象一下,我有 A 类和 B 类。它们之间的关系是一对多的。例如我有

// Class A
public class A {
    private Integer id;

    // some fields

    @OneToMany(name = "A_ID", targetEntity = B.class)
    private List<B> objectsB;

    // getters and setters
}

// CLass B

public class B {
   private Integer id;

   // some fields

   private Integer A_ID;
}

我想写 HQL: Query = ("select someField from A aEntity where aEntity.objectsB.id = :parameter ");

但由于映射一对多 A 对象包含 B 列表(objectsB)和 aEntity.objectsB.id 导致错误。谁能帮我 ?

4

1 回答 1

0

First of all, you shouldn't have a field A_ID in B. Either make the association unidirectional, and remove this field completely, or make the association bidirectional, and add a ManyToOne association from B to A.

Now, to answer your question, you need to learn about HQL joins:

select someField from A aEntity 
join aEntity.objectsB bEntity
where bEntity.id = :parameter
于 2013-10-22T13:48:07.960 回答