我面临一种典型的问题。想象一下对象之间的典型1-N关系。确切地说,用户(U)和房间(R):[U] * --- 1 [R]。
问题来了,Room应该是抽象基类,并带有实现,例如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上使用@Entity而不是@MappedSuperclass来解决此问题,但是我不确定这是否是一种好的解决方案,是否还有更好的解决方案。
最佳答案
根据评论下发。将 super class 声明为@Entity是解决方案。
关于jpa - JPA与ManyToOne映射的父类(super class),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8772395/