zde*_*ine 5 entity jpa mappedsuperclass
我面临着一个典型的问题。想象一下对象之间典型的 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
}
Run Code Online (Sandbox Code Playgroud)
我知道这可能可以通过在 RoomSuperclass 上使用 @Entity 而不是 @MappedSuperclass 来解决,但我不确定这是否是一个好的解决方案以及是否有更好的解决方案。
根据帖子下的评论。将超类声明为 @Entity 是解决方案。
| 归档时间: |
|
| 查看次数: |
8477 次 |
| 最近记录: |