为了将我的域模型与持久性机制分离,我使用XML来配置从我的域模型到数据库实体的映射。我有这个实体:
public class Tenant {
long id;
Map<String, AuthApp> authApps;
...
}
而这个价值对象:
public class AuthApp {
String authCode;
int durationInDays;
...
}
值对象本身没有生命周期,它取决于实体。我在我的RDBMS中创建了两个表,“tenant”和“auth_app”。任何人都可以指导我如何为这种情况编写JPA xml吗?到目前为止我编码的XML是这样的:
<entity class="Tenant">
<table name="tenant"/>
<attributes>
<id name="id"><generated-value strategy="AUTO" /></id>
<element-collection name="authApp">
<map-key name="app_id"/>
<collection-table name="auth_app">
<join-column name="tenant_id" referenced-column-name="id"/>
</collection-table>
</element-collection>
</attributes>
</entity>
我不知道它是否正确,以及如何继续。
顺便说一下,我正在使用hibernate作为JPA提供程序。
解决!感谢DN1的评论,我已经意识到“价值对象”是JPA语义中的“可嵌入”。因此,将“AuthApp”定义为“可嵌入”,使用“map-key-column”而不是“map-key”,完成。整件事情是这样的:
<entity class="Tenant">
<table name="tenant"/>
<attributes>
<id name="id"><generated-value strategy="AUTO" /></id>
<element-collection name="authApp" target-class="AuthApp">
<map-key-column name="app_id"/>
<collection-table name="auth_app">
<join-column name="tenant_id" referenced-column-name="id"/>
</collection-table>
</element-collection>
</attributes>
</entity>
<embeddable class="AuthApp">
<attributes>
......
</attributes>
</embeddable>
......