Hibernate JPA注解一对一


下面演示JPA注解方式的一对一关系映射。以公民 和 身份证 为例。

更多一对一的需求可以参考XML版本的一对一映射:

http://www.yiidian.com/hibernate/hibernate-one2one.html

一、实体类

Person:

/**
 * 公民(一方)
 * @author http://www.yiidian.com
 */
@Entity
@Table(name="t_person")
public class Person {
	
	@Id
	@GeneratedValue(strategy=GenerationType.IDENTITY)
	@Column(name="id")
	private Integer id;
	
	@Column(name="name")
	private String name;
	
	//关联身份证
	@OneToOne(mappedBy="person")
	private Card card;

	public Integer getId() {
		return id;
	}

	public void setId(Integer id) {
		this.id = id;
	}

	public String getName() {
		return name;
	}

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

	public Card getCard() {
		return card;
	}

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

Card:

/**
 * 身份证(一方)
 * @author http://www.yiidian.com
 */
@Entity
@Table(name="t_card")
public class Card {
	
	@Id
	@GeneratedValue(strategy=GenerationType.IDENTITY)
	@Column(name="id")
	private Integer id;
	
	@Column(name="card_no")
	private String cardno;
	
	//关联公民
	@OneToOne
	@JoinColumn(name="person_id")
	private Person person;

	public Integer getId() {
		return id;
	}

	public void setId(Integer id) {
		this.id = id;
	}

	public String getCardno() {
		return cardno;
	}

	public void setCardno(String cardno) {
		this.cardno = cardno;
	}

	public Person getPerson() {
		return person;
	}

	public void setPerson(Person person) {
		this.person = person;
	}
}

二、配置hibernate.cfg.xml

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE hibernate-configuration PUBLIC
	"-//Hibernate/Hibernate Configuration DTD 3.0//EN"
	"http://www.hibernate.org/dtd/hibernate-configuration-3.0.dtd">
	
<hibernate-configuration>	
	<!-- 连接数据库的参数 -->
<session-factory>
	<!-- 1.连接数据库参数 -->
	<property name="hibernate.connection.driver_class">
		com.mysql.jdbc.Driver
	</property>
	<property name="hibernate.connection.url">
		jdbc:mysql://localhost:3306/hibernate
	</property>
	<property name="hibernate.connection.username">root</property>
	<property name="hibernate.connection.password">root</property>

	<!-- 整合c3p0 -->
	<property name="hibernate.connection.provider_class">
		org.hibernate.c3p0.internal.C3P0ConnectionProvider
	</property>
	<!-- c3p0详细配置 -->
	<property name="c3p0.min_size">10</property>
	<property name="c3p0.max_size">20</property>

	<!-- hibernate方言 -->
	<property name="hibernate.dialect">
		org.hibernate.dialect.MySQLDialect
	</property>

	<!-- 2.hibernate扩展参数 -->
	<property name="hibernate.show_sql">true</property>
	<property name="hibernate.format_sql">true</property>
	<property name="hibernate.hbm2ddl.auto">update</property>

	<!-- 让session被TheadLocal管理 -->
	<property name="hibernate.current_session_context_class">
		thread
	</property>
	<property name="dialect"></property>
	<mapping class="com.yiidian.domain.Card" />
	<mapping class="com.yiidian.domain.Person" />

</session-factory>
</hibernate-configuration>	

三、编写测试类

/**
 * 演示一对一的映射
 * @author lenovo
 *
 */
public class Demo {
	
	@Test
	public void test1(){
		Session session = HibernateUtil.getSession();
		Transaction tx = session.beginTransaction();
		
		
		Person p = new Person();
		p.setName("老王");
		
		Card c = new Card();
		c.setCardno("44333222");
		
		p.setCard(c);
		c.setPerson(p);
		
		session.save(p);
		session.save(c);
		
		tx.commit();
	}
	
}

执行程序,控制台输出:

Hibernate: 
    insert 
    into
        t_person
        (name) 
    values
        (?)
Hibernate: 
    insert 
    into
        t_card
        (card_no, person_id) 
    values
        (?, ?)

一共生成两张表:

源码下载:https://pan.baidu.com/s/1dE1Mqch