我用 EclipseLink 的 JPA 2.0 来处理2个实体,一个是 Attribute,静态参数数据,另一个 Account 将会选用多个 Attribute,关联信息将会保存在第三张表中。表:Attribute: ATR0100.
表:Account: ACC0100.
表:Account-Attribute 关系:ACC0110.自动生成的 ACC0110 (关系表)中只有两个字段,分别是前面两个表的外键,这可以工作,但现在我希望在这个 ACC0110 关联表中增加一个 SEQ 字段,用来描述这些 Attribute 被 Account 选用之后的次序,不清楚怎么配置,我用了 <order-by /> 但这个字段它要求是在 target-entity (也就是加上 Attribute) 上,但不是我要的结果,因为 Attribute 是静态参数数据,不能加 SEQ 字段,加上也没有意义,应该加在关联表中,但不清楚怎么配置达到这个要求。如果我创建另一个实体 AccountAttribute 来表达这种关系并加上 SEQ 列的话,那等于是手工操作这个关系,而且每次选用一个 Attribute 后,我要把它复制成 AccountAttribute 这样的实体,里面的 IPKEY 之类的关键字段都得手工填进去,而不是直接向集合里添加一个 Attribute 就能达到;这种做法很怪异,还不如不用一对多关联。JPA 为我生成的一对多关联更像是我们把主表和从表的数据一同更新的那种,而不是引用一个 PK。JPA 有可能在关联表中增加一个 SEQ 来描述这种次序吗?数据: Attribute (ATR0010)
IPKEY, CODE
1 COLOR
2 SIZE
Account (ACC0100)
IPKEY, CODE
1 AAA-COLOR-SIZE
JPA 生成的关联表:
Account Attribute(ACC0110)
ACC_IPKEY, ATR_IPKEY
1 2
1 1
我期望的关联表:
ACC_IPKEY, ATR_IPKEY, SEQ
1 2 2
1 1 1
我现在的配置,它达不到我的期望:<?xml version="1.0" encoding="UTF-8"?>
<entity-mappings xmlns="http://java.sun.com/xml/ns/persistence/orm" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/persistence/orm http://java.sun.com/xml/ns/persistence/orm_2_0.xsd"
version="2.0"> <package>persistence</package>
<schema>repository</schema> <entity class="Attribute">
<table name="ATR0100" />
<attributes>
<id name="ipkey">
<column name="IPKEY" />
<generated-value strategy="IDENTITY" />
</id>
<basic name="CODE">
<column name="CODE" column-definition="varchar(100)" />
</basic>
<basic name="sequence">
<column name="SEQ" column-definition="integer" />
</basic>
</attributes>
</entity>
<entity class="Account">
<table name="ACC0100" />
<attributes>
<id name="ipkey">
<column name="IPKEY" />
<generated-value strategy="IDENTITY" />
</id>
<basic name="CODE">
<column name="CODE" column-definition="varchar(100)" />
</basic>
<many-to-many name="attributes" target-entity="Attribute">
<order-by>sequence</order-by>
<join-table name="ACC0110" />
</many-to-many>
</attributes>
</entity>
</entity-mappings>
表:Account: ACC0100.
表:Account-Attribute 关系:ACC0110.自动生成的 ACC0110 (关系表)中只有两个字段,分别是前面两个表的外键,这可以工作,但现在我希望在这个 ACC0110 关联表中增加一个 SEQ 字段,用来描述这些 Attribute 被 Account 选用之后的次序,不清楚怎么配置,我用了 <order-by /> 但这个字段它要求是在 target-entity (也就是加上 Attribute) 上,但不是我要的结果,因为 Attribute 是静态参数数据,不能加 SEQ 字段,加上也没有意义,应该加在关联表中,但不清楚怎么配置达到这个要求。如果我创建另一个实体 AccountAttribute 来表达这种关系并加上 SEQ 列的话,那等于是手工操作这个关系,而且每次选用一个 Attribute 后,我要把它复制成 AccountAttribute 这样的实体,里面的 IPKEY 之类的关键字段都得手工填进去,而不是直接向集合里添加一个 Attribute 就能达到;这种做法很怪异,还不如不用一对多关联。JPA 为我生成的一对多关联更像是我们把主表和从表的数据一同更新的那种,而不是引用一个 PK。JPA 有可能在关联表中增加一个 SEQ 来描述这种次序吗?数据: Attribute (ATR0010)
IPKEY, CODE
1 COLOR
2 SIZE
Account (ACC0100)
IPKEY, CODE
1 AAA-COLOR-SIZE
JPA 生成的关联表:
Account Attribute(ACC0110)
ACC_IPKEY, ATR_IPKEY
1 2
1 1
我期望的关联表:
ACC_IPKEY, ATR_IPKEY, SEQ
1 2 2
1 1 1
我现在的配置,它达不到我的期望:<?xml version="1.0" encoding="UTF-8"?>
<entity-mappings xmlns="http://java.sun.com/xml/ns/persistence/orm" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/persistence/orm http://java.sun.com/xml/ns/persistence/orm_2_0.xsd"
version="2.0"> <package>persistence</package>
<schema>repository</schema> <entity class="Attribute">
<table name="ATR0100" />
<attributes>
<id name="ipkey">
<column name="IPKEY" />
<generated-value strategy="IDENTITY" />
</id>
<basic name="CODE">
<column name="CODE" column-definition="varchar(100)" />
</basic>
<basic name="sequence">
<column name="SEQ" column-definition="integer" />
</basic>
</attributes>
</entity>
<entity class="Account">
<table name="ACC0100" />
<attributes>
<id name="ipkey">
<column name="IPKEY" />
<generated-value strategy="IDENTITY" />
</id>
<basic name="CODE">
<column name="CODE" column-definition="varchar(100)" />
</basic>
<many-to-many name="attributes" target-entity="Attribute">
<order-by>sequence</order-by>
<join-table name="ACC0110" />
</many-to-many>
</attributes>
</entity>
</entity-mappings>
把原来的Many-to-Many分解开,变成2个一对多关联
我的配置中, Account 会包括 Attribute,但 Attribute 不会引用到 Account,所以生成的模型是多对多。Account 会选择若干 Attribute 并保存下来,楼上说的两个一对多是怎么配置的,能否给个样例?
@JoinColumn(name="i_state")
public Set<TfAdvertisment> getTfAdvertisments() {
return tfAdvertisments;
} public void setTfAdvertisments(Set<TfAdvertisment> tfAdvertisments) {
this.tfAdvertisments = tfAdvertisments;
}
<?xml version="1.0" encoding="UTF-8"?>
<entity-mappings xmlns="http://java.sun.com/xml/ns/persistence/orm" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/persistence/orm http://java.sun.com/xml/ns/persistence/orm_2_0.xsd"
version="2.0"> <package>persistence</package>
<schema>repository</schema> <entity class="Attribute">
<table name="ATR0100" />
<attributes>
<id name="ipkey">
<column name="IPKEY" />
<generated-value strategy="IDENTITY" />
</id>
<basic name="CODE">
<column name="CODE" column-definition="varchar(100)" />
</basic>
<basic name="sequence">
<column name="SEQ" column-definition="integer" />
</basic>
</attributes>
</entity>
<entity class="Account">
<table name="ACC0100" />
<attributes>
<id name="ipkey">
<column name="IPKEY" />
<generated-value strategy="IDENTITY" />
</id>
<basic name="CODE">
<column name="CODE" column-definition="varchar(100)" />
</basic>
<many-to-many name="attributes" target-entity="Attribute">
<order-column name="SEQ" /> <!-- 只是这里不同。-->
<join-table name="ACC0110" />
</many-to-many>
</attributes>
</entity>
</entity-mappings>