1. 程式人生 > >Hibernate註解實現一對一關聯

Hibernate註解實現一對一關聯

本文使用Husband與Wife實現Hibernate的一對一關聯。使用的資料庫為SQL server 2005。

程式碼如下:

Husband.java

/**
 * 
 */
package com.zhang.shun.entity;

import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.OneToOne;
import javax.persistence.Table;

import com.etong.common.hibernate.HibernateUtil;

/**
 * @author shun
 *
 */

@Entity
@Table(name="HUSBAND")
public class Husband {
	
	@Id
	@GeneratedValue
	private int id;
	
	@Column(name="name",length=10)
	private String name;
	
	@OneToOne
	@JoinColumn(name="wid",unique=true)
	private Wife wife;
	
	
	public int getId() {
		return id;
	}
	public void setId(int id) {
		this.id = id;
	}
	public String getName() {
		return name;
	}
	public void setName(String name) {
		this.name = name;
	}
	public Wife getWife() {
		return wife;
	}
	public void setWife(Wife wife) {
		this.wife = wife;
	}
	public static void main(String[] args) {
		HibernateUtil.beginTransaction();
		Husband h = new Husband();
		h.setName("123");
		Wife w = new Wife();
		w.setId(1);
		w.setName("321");
		h.setWife(w);
		HibernateUtil.makePersistent(w);
		HibernateUtil.makePersistent(h);
		HibernateUtil.commitTransaction();
	}
}
Wife.java如下:
/**
 * 
 */
package com.zhang.shun.entity;

import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.Id;
import javax.persistence.Table;

/**
 * @author shun
 *
 */

@Entity
@Table(name="WIFE")
public class Wife {
	
	@Id
	private int id;
	
	@Column(name="name",length=10)
	private String name;
	
	//private Husband husband;
	
	public String getName() {
		return name;
	}
	public void setName(String name) {
		this.name = name;
	}
	public int getId() {
		return id;
	}
	public void setId(int id) {
		this.id = id;
	}
	
	/*
	public Husband getHusband() {
		return husband;
	}
	public void setHusband(Husband husband) {
		this.husband = husband;
	}
	*/

}
下面來進行測試下:

one_one_test.java如下:

package com.zhang.shun.entity;

import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;
import org.hibernate.cfg.AnnotationConfiguration;
import org.hibernate.cfg.Configuration;

public class one_one_test {
	
	public static void main(String[] args) {
		// TODO Auto-generated method stub

		Husband h = new Husband();
		h.setName("zhang");
		
		Wife w = new Wife();
		w.setId(6);
		w.setName("shun");
		
		h.setWife(w);
		
		//例項化Configuration物件
		Configuration configuration = new AnnotationConfiguration();
		//載入hibernate配置檔案
		configuration.configure("/hibernate.cfg.xml");
		configuration.configure("/hibernate.map.xml");
		//建立Sessionfactory
		SessionFactory sessionFactory = configuration.buildSessionFactory();
		//開啟Session
		Session session = sessionFactory.openSession();
		//開始一個事務
		Transaction transaction = session.beginTransaction();
		//持久化事務
		session.save(w); //注意順序!!!因為後者要用到前者,所以前者必須先持久化。
		session.save(h);</span>
		//提交事務
		transaction.commit();
		//關閉Session
		session.close();
	}
	
	
}

Hibernate配置檔案:hibernate.cfg.xml要放在src下面。
<pre name="code" class="java"><?xml version='1.0' encoding='UTF-8'?>
<!DOCTYPE hibernate-configuration PUBLIC
          "-//Hibernate/Hibernate Configuration DTD 3.0//EN"
          "http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd">

<!-- Generated by MyEclipse Hibernate Tools.                   -->
<hibernate-configuration>
	<session-factory>
		<property name="dialect">
			org.hibernate.dialect.SQLServerDialect
		</property>
		<property name="connection.url">
			jdbc:sqlserver://localhost:1433;databaseName=mydb
		</property>
		<property name="connection.profile">db</property>
		<property name="connection.username">sa</property>
		<property name="connection.password">admin</property>
		<property name="connection.driver_class">
			com.microsoft.sqlserver.jdbc.SQLServerDriver
		</property>
		<property name="connection.useUnicode">true</property>
	
		<property name="connection.characterEncoding">GBK</property>
	
		<property name="hbm2ddl.auto">update</property>
		<!-- 可以幫助你實現正向工程,即由java程式碼生成資料庫指令碼,進而生成具體的表結構. -->
	
		<property name="hibernate.cache.use_second_level_cache">
			true
		</property>
	
		<property name="hibernate.cache.use_query_cache">false</property>
	
		<property name="hibernate.cache.provider_class">
			org.hibernate.cache.EhCacheProvider
		</property>
		<property name="show_sql">true</property>
	</session-factory>
</hibernate-configuration>

</pre><pre name="code" class="java">下面再看看對映檔案:hibernate.map.xml
<pre name="code" class="java"><?xml version='1.0' encoding='UTF-8'?>
<!DOCTYPE hibernate-configuration PUBLIC
		"-//Hibernate/Hibernate Configuration DTD 3.0//EN"
		"http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd">

<hibernate-configuration>
<session-factory>  
	<mapping class="com.zhang.shun.entity.Husband" />   <!--需要對映的兩個類 -->
 	<mapping class="com.zhang.shun.entity.Wife" /> 
</session-factory>
</hibernate-configuration>