I have the following classes:
class A{
@OneToOne(cascade=CascadeType.ALL)
private B b;
}
class C{
@ManyToOne
private A a;
}
class B{
@OneToOne
private A a;
@MapKey(name = "name")
@OneToMany(cascade = CascadeType.ALL, ...)
@JoinColumn(...)
private Map<String C> cs;
}
How do I have to specify the mapping on B.cs to join where B.a 开发者_如何学Python== C.a?
Is this possible? Or do I have to change the property C.a to C.b? (I would prefer to keep it as it is, as the entity B is just a helper class.) I also tried to change B to @Embeddable, but Map is not supported for embeddables.
JPA requires that all relationships be by Id (the foreign key references the primary key). So, you need to either add a @ManyToOne from C to B. Or, ensure that B's Id is the foreign key to A (add @Id on the @OneToOne from B to A and remove A's other @Id). If B was a subclass of A instead of having a OneToOne this would also work.
If you are using EclipseLink, you can defined more complex criteria for a relationship. You would need to define the OneToMany's foreign keys using a DescriptorCustomizer and the OneToManyMapping API.
I think you can extends B from A.
If this doesn't work for you, maybe you can add a transient property to refer A's id,
@Transient
Integer getId1() {
return a.getId();
}
and join C using id1
instead of B's primary key.
Edit: This doesn't work.
精彩评论