hibernate one-to-one mapping 的 mapping file 怎么写

在用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 才可以正常工作

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值