Hibernate一对一映射

Hibernate一对一映射

              一对一映射关系在我们的项目中用到的不是太多,但是也是值得学些的一种映射方式,框架的学习在于会用,再会用的情况下理解其思想。

             比如人和一个身份证,这个关系就是一对一关系了,那么就拿这个例子举例好了。一对一关系在hibernate中有基于外键的方式和基于主键的方式。

public class User {
	private Integer userId;
	private String name;
	private IDCard card;

	public User() {
		// TODO Auto-generated constructor stub
	}



	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}


	

	public Integer getUserId() {
		return userId;
	}


	public void setUserId(Integer userId) {
		this.userId = userId;
	}



	public IDCard getCard() {
		return card;
	}



	public void setCard(IDCard card) {
		this.card = card;
	}



	/* (non-Javadoc)
	 * @see java.lang.Object#toString()
	 */
	@Override
	public String toString() {
		return "User [userId=" + userId + ", name=" + name + ", card=" + card
				+ "]";
	}
	
}
public class IDCard {
	private Integer cardId;
	private String number;
	private User user;

	public IDCard() {
		// TODO Auto-generated constructor stub
	}

	public Integer getCardId() {
		return cardId;
	}

	public void setCardId(Integer cardId) {
		this.cardId = cardId;
	}

	public String getNumber() {
		return number;
	}

	public void setNumber(String number) {
		this.number = number;
	}

	public User getUser() {
		return user;
	}

	public void setUser(User user) {
		this.user = user;
	}

}

     基于外键的一对一的配置文件:

            

<hibernate-mapping>
	<class name="com.zdx.domain.IDCard" table="idcard">
		<id name="cardId" type="integer">
			<column name="cardId" />
			<generator class="native" />
		</id>
		<property name="number"></property>
		<!-- 基于外键的一对一 -->
		<many-to-one name="user" class="com.zdx.domain.User" column="userId"  unique="true" ></many-to-one>

	</class>
</hibernate-mapping>
<hibernate-mapping>
	<class name="com.zdx.domain.User" table="user">
		<id name="userId" type="integer">
			<column name="userId" />
			<generator class="native" />
		</id>
		<property name="name"></property>
		<!-- 基于外键的一对一 property-ref是指在IDCard类中User对象的名称 -->
		<one-to-one name="card" class="com.zdx.domain.IDCard"
			property-ref="user"></one-to-one>

	</class>
</hibernate-mapping>

  基于主键的一对一的配置文件:

<hibernate-mapping>
	<class name="com.zdx.domain.IDCard" table="idcard">
		<id name="cardId" type="integer">
			<column name="cardId" />
			<!-- 采用基于主键的一对一映射时,要把主键生成策略改为foreign
				其中的property参数是表示要使用哪个对象的主键值,
				user的主键值作为自己的主键值
			 -->
			<generator class="foreign">
				<param name="property">user</param>
			</generator>
		</id>
		<property name="number"></property>
		
		 <!-- user属性,表达IDCard与User的一对一。
			采用基于主键的一对一映射方式。
			constrained属性:默认为false,表示不在主键列上加上外键约束,true表示要加上外键约束。
		 -->
		 <one-to-one name="user" class="com.zdx.domain.User" constrained="true"></one-to-one>
	

	</class>
</hibernate-mapping>
<hibernate-mapping>
	<class name="com.zdx.domain.User" table="user">
		<id name="userId" type="integer">
			<column name="userId" />
			<generator class="native" />
		</id>
		<property name="name"></property>
		<!-- 基于主键的一对一 -->
		<one-to-one name="card" class="com.zdx.domain.IDCard"></one-to-one>

	</class>
</hibernate-mapping>

 测试代码:

package com.zdx.test;

import org.junit.Test;

import com.zdx.dao.IDCardDao;
import com.zdx.dao.UserDao;
import com.zdx.dao.impl.IDCardDaoImpl;
import com.zdx.dao.impl.UserDaoImpl;
import com.zdx.domain.IDCard;
import com.zdx.domain.User;

public class UserDaoTest {

	UserDao userDao = new UserDaoImpl();
	IDCardDao cardDao = new IDCardDaoImpl();

	@Test
	public void testSelect() throws Exception {

		User user = userDao.getUserByName("zdx");
		if (user != null) {
			System.out.println("User--->" + user.toString());
		} else {
			System.out.println("无此用户");
		}
	}

	@Test
	public void testSave() throws Exception {
		User user = new User();
		user.setName("sfeewf");

		IDCard card = new IDCard();
		card.setNumber("0023423234354");

		// 关联
		user.setCard(card);
		card.setUser(user);
		userDao.insertUser(user);
		cardDao.insertIDCard(card);
		
	}
}

相关推荐