在用hibernate写one-to-one relationship时, mapping file里有几个点比较容易出错, 在这里特别说明下
Example:
Database Relationship:
java entities:
对应account table:
package tanghao.xmlmapping.one2one.entity;
public class Account implements java.io.Serializable {
/**
*
*/
private static final long serialVersionUID = 7661590583678153237L;
private String email;
private String accountName;
private AccountParticulars particulars;
private Integer accountId;
public String getEmail() {
return email;
}
public void setEmail(String email) {
this.email = email;
}
public String getAccountName() {
return accountName;
}
public void setAccountName(String accountName) {
this.accountName = accountName;
}
public AccountParticulars getParticulars() {
return particulars;
}
public void setParticulars(AccountParticulars particulars) {
this.particulars = particulars;
}
public Integer getAccountId() {
return accountId;
}
public void setAccountId(Integer accountId) {
this.accountId = accountId;
}
}
对应 account_particulars table:
package tanghao.xmlmapping.one2one.entity;
public class AccountParticulars implements java.io.Serializable {
/**
*
*/
private static final long serialVersionUID = -4346433899011070419L;
private Integer accountId;
private String email;
private String ownerName;
private String gender;
private Account account;
public String getEmail() {
return email;
}
public void setEmail(String email) {
this.email = email;
}
public String getOwnerName() {
return ownerName;
}
public void setOwnerName(String ownerName) {
this.ownerName = ownerName;
}
public String getGender() {
return gender;
}
public void setGender(String gender) {
this.gender = gender;
}
public Account getAccount() {
return account;
}
public void setAccount(Account account) {
this.account = account;
}
public Integer getAccountId() {
return accountId;
}
public void setAccountId(Integer accountId) {
this.accountId = accountId;
}
}
Mapping files:
account table mapping file:
<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC
"-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
<hibernate-mapping package="tanghao.xmlmapping.one2one.entity">
<class name="Account" table="account">
<id name="accountId" type="java.lang.Integer">
<column name="ACCOUNT_ID"/>
<generator class="identity"/>
</id>
<property name="email" type="string">
<column name="EMAIL" length="50" not-null="true" />
</property>
<property name="accountName" type="string">
<column name="ACCOUNT_NAME" length="50" not-null="true" />
</property>
<one-to-one name="particulars" class="tanghao.xmlmapping.one2one.entity.AccountParticulars" cascade="save-update"></one-to-one>
</class>
</hibernate-mapping>
这个里边注意 one-to-one 里的cascade 需要: save-update, 以保证关联表格可以同时save 和 update
account_particulars mapping file:
<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC
"-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
<hibernate-mapping package="tanghao.xmlmapping.one2one.entity">
<class name="AccountParticulars" table="account_particulars">
<id name="accountId" type="java.lang.Integer">
<column name="ACCOUNT_ID"/>
<generator class="foreign">
<param name="property">account</param>
</generator>
</id>
<property name="email" type="string">
<column name="EMAIL" length="50" not-null="true" />
</property>
<property name="ownerName" type="string">
<column name="OWNER_NAME" length="50" not-null="true"/>
</property>
<property name="gender" type="string">
<column name="GENDER" length="10" not-null="true"/>
</property>
<one-to-one name="account" class="tanghao.xmlmapping.one2one.entity.Account" constrained="true"/>
</class>
</hibernate-mapping>
这个里边有几个点很需要注意:
1. generator type是 foreign, foreign 是说从其他表格里边拿数据
2 . generator 中定义的param 一定要是 one-to-one 里的name attribute的值, 以此来map到主表的primary key!!
3. 两张表id的type 一定要一致。
只要注意的地方做好了, one-to-one mapping 才可以正常工作