【发布时间】:2014-01-28 14:25:01
【问题描述】:
我有一个问题,我不确定 jpa 是否可行
如果我说表 A
public class A {
Long id;
Long type;
Details details; // can this connect to B or c depends on type?
}
表 B 和表 C 是两个不同的明细表。 (不确定有什么共同点)
我可以将 B 或 C 连接到 A 取决于 A.type 吗?
谢谢
阿隆
编辑:让我试着更准确一些
我有实体
@Entity
@Table(name = "tbl_A")
public class A implements Serializable {
/**
*
*/
private static final long serialVersionUID = 1L;
@Id
private Long id; // seems like spring's jpa has issue hanlde "_"
/*
*can i achive some thinglike
* if detailsTableType== 1 use Details as B table
*/
@OneToOne(mappedBy = "details", cascade = CascadeType.ALL)
@PrimaryKeyJoinColumn
private B details;
/*
*can i achive some thinglike
* if detailsTableType== 2 use Details as C table
*/
@OneToOne(mappedBy = "details", cascade = CascadeType.ALL)
@PrimaryKeyJoinColumn
private C details; //Details
/*
* this field set the from where the join should be
*/
private Long detailsTableType;
}
注意 B、C 不一定有任何共同点
编辑 2:
一种可能的解决方案是使用 getter 进行某种破解。意思是: 在 A 中映射所有可能的连接(将它们标记为惰性)并创建一个 getter,它会根据类型知道它应该使用哪个连接。
【问题讨论】:
-
您真正拥有的是一个类层次结构,其中包含
A的多个子类。一种A具有B详细信息对象,另一种具有C详细信息对象。这就像 hibernate 的 Table-per-sub-class 和 table-per-class-hierarchy 示例的组合。见docs.jboss.org/hibernate/orm/3.3/reference/en-US/html/…
标签: java jpa jpa-2.0 spring-data