我有3张桌子:
Account
{
Long account_id; // primary key, alternate key field 1
Long client_id; // alternate key field 2
...
}
Admin
{
Long office_id; // primary key field 1
Long account_id; // primary key field 2
Long client_id;
}
Office
{
Long office_id; // primary key
...
}
因此,Admin表按备用键(account_id,client_id)引用Account表。我尝试为它编写映射,但无法理解。
我现在有:
<class name="Account" table="Account">
<id name="id">
<column name="account_id" not-null="true"/>
</id>
<properties name="account_ak1" unique="true">
<property name="id" column="account_id" insert="false" update="false"/>
<property name="client" column="client_id" insert="false" update="false"/>
</properties>
<many-to-one name="client" class="Client" column="client_id" lazy="false" fetch="join"/>
...
</class>
<class name="Admin">
<composite-id name="id" class="KeyPair">
<key-many-to-one name="t1" class="Office" column="office_id" lazy="false"/>
<key-many-to-one name="t2" class="Account" lazy="false" column="account_id"/>
</composite-id>
<many-to-one name="account" class="Account" not-null="true" insert="false" update="false" lazy="false" fetch="join" property-ref="account_ak1">
<column name="account_id"/>
<column name="client_id"/>
</many-to-one>
</class>
问题是我无法以这种方式在sql update / insert中设置client_id,并且我不能只设置account_id来插入/更新false,既不在复合ID内也不在多对一内。 此外,我试图在主键中包含Account的所有字段。
<composite-id name="id" class="KeyPair">
<key-many-to-one name="t1" class="Office" column="office_id" lazy="false"/>
<key-many-to-one name="t2" class="Account" lazy="false"/>
<column name="account_id"/>
<column name="client_id"/>
</key-many-to-one>
</composite-id>
但是key-many-to-one只能引用Account的主键,不接受property-ref。 怎么解决这个问题?