[英]Proper mapping ( JPA ) composite key
For example i have entities例如我有实体
@Entity
public class A{
@Id
Long Id;
...
}
@Entity
public class B{
@Id
Long Id;
...
}
@Entity
@IdClass(ABId.class).
public class AB{
@Id
@ManyToOne
private A a;
@Id
@ManyToOne
private B b;
private boolean state;
}
Class for composite primary key:
public ABId implements Serializable{
Long a;
Long b;
.........
}
and i want to get from class A something like this select * from AB ab where ab.a_id=1;我想从 A 类中得到这样的东西 select * from AB ab where ab.a_id=1; ( id from A object)
(来自 A 对象的 id)
i did such mapping in class A我在 A 类中做了这样的映射
@OneToMany(fetch = FetchType.EAGER)
@JoinColumns({
@JoinColumn(name="A_id", referencedColumnName="id"),
@JoinColumn(name="B_id", referencedColumnName="id")
})
List<AB> listAB;
but it does nothing i alawys get empty list.但它什么也没做,我总是得到空列表。
Solved,解决了,
I did mapping我做了映射
@OneToMany(fetch = FetchType.EAGER)
@JoinColumns({
@JoinColumn(name="A_id", referencedColumnName="id")
})
List<AB> listAB;
and it works like it should.它像它应该的那样工作。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.