我面临一个典型问题。想象一下对象之间的典型1-N关系。更准确地说,用户(U)和房间(R):[U]*---1[R]。
问题来了,Room应该是一个抽象基类,有BlueRoom、RedRoom等实现。如何在User实体内正确设置关系呢?
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上使用@Entity而不是@MappedSuperclass来解决,但我不确定这是否是一个好的解决方案,是否有更好的解决方案。