我正面临着一种典型的问题。想象一下物体之间典型的1-N关系。准确地说,用户(U)和房间(R):[U] * - 1 [R]。JPA与ManyToOne映射的超类
问题在于,房间应该是抽象基类,例如BlueRoom,RedRoom的实现。如何正确设置用户实体内的关系?
public interface Room { ... }
@MappedSuperclass
public abstract class RoomSuperclass implements Room { ... }
@Entity
public class BlueRoom extends RoomSuperclass { ... }
@Entity
public class RedRoom extends RoomSuperclass { ... }
@Entity
public class User {
@ManyToOne(targetEntity = ???) // I don't know if it will be BlueRoom or RedRoom
private Room room; // ManyToOne cannot be applied on mapped superclass
}
我知道这可能会通过RoomSuperclass而不是使用@MappedSuperclass被@Entity可能解决,但我不知道这是否是很好的解决方案,如果有任何更好的。
是的,这是解决方案。如果你有某种关联,那么这个东西必须是一个实体。 – 2012-01-07 20:11:30
感谢您的回复JB,我会等待一天,如果其他人回复,然后关闭问题 – d1x 2012-01-07 20:24:19