Hibernate应用One2One

Hibernate应用One2One

1、一对一(Husband&Wife 单向)
Husband类
package com.sean.model.one2one;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;

/**
 * 一对一:丈夫对象
 * 
 * @author Sean 2013-2-26
 * 
 */
@Entity
public class Husband {

	private int id;
	private String name;
	private int age;
	private String work;

	@Id
	@GeneratedValue
	public int getId() {
		return id;
	}

	public String getName() {
		return name;
	}

	public int getAge() {
		return age;
	}

	public String getWork() {
		return work;
	}

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

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

	public void setAge(int age) {
		this.age = age;
	}

	public void setWork(String work) {
		this.work = work;
	}

}


Wife类
package com.sean.model.one2one;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import javax.persistence.OneToOne;

/**
 * 一对一:丈夫对象
 * 
 * @author Sean 2013-2-26
 * 
 */
@Entity
public class Wife {

	private int id;
	private String name;
	private int age;
	private String work;
	private Husband dear;

	@OneToOne
	public Husband getDear() {
		return dear;
	}

	public void setDear(Husband dear) {
		this.dear = dear;
	}

	@Id
	@GeneratedValue
	public int getId() {
		return id;
	}

	public String getName() {
		return name;
	}

	public int getAge() {
		return age;
	}

	public String getWork() {
		return work;
	}

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

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

	public void setAge(int age) {
		this.age = age;
	}

	public void setWork(String work) {
		this.work = work;
	}

}


2、一对一(Husband&Wife 双向)
Husband类, Wife类不变
package com.sean.model.one2one;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import javax.persistence.OneToOne;

/**
 * 一对一:丈夫对象
 * 
 * @author Sean 2013-2-26
 * 
 */
@Entity
public class Husband {

	private int id;
	private String name;
	private int age;
	private String work;
	private Wife dear;
	
	@OneToOne
	public Wife getDear() {
		return dear;
	}

	public void setDear(Wife dear) {
		this.dear = dear;
	}

	@Id
	@GeneratedValue
	public int getId() {
		return id;
	}

	public String getName() {
		return name;
	}

	public int getAge() {
		return age;
	}

	public String getWork() {
		return work;
	}

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

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

	public void setAge(int age) {
		this.age = age;
	}

	public void setWork(String work) {
		this.work = work;
	}

}


hibernate.cfg.xml配置

<?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>

		<!-- Database connection settings -->
		<property name="connection.driver_class">com.microsoft.sqlserver.jdbc.SQLServerDriver</property>
		<property name="connection.url">jdbc:sqlserver://localhost:1433;DatabaseName=test</property>
		<property name="connection.username">sa</property>
		<property name="connection.password">1234567890</property>

		<!-- JDBC connection pool (use the built-in) <property name="connection.pool_size">1</property> -->

		<!-- SQL dialect -->
		<property name="dialect">org.hibernate.dialect.SQLServerDialect</property>

		<!-- Enable Hibernate's automatic session context management <property 
			name="current_session_context_class">thread</property> -->

		<!-- Disable the second-level cache -->
		<property name="cache.provider_class">org.hibernate.cache.NoCacheProvider</property>

		<!-- Echo all executed SQL to stdout -->
		<property name="show_sql">true</property>

		<!-- Drop and re-create the database schema on startup -->
		<property name="hbm2ddl.auto">update</property>

		<!-- <mapping resource="com/sean/model/Student.hbm.xml" /> -->
		
		<!-- Annotation -->
		<mapping class="com.sean.model.one2one.Husband" />
		<mapping class="com.sean.model.one2one.Wife" />

	</session-factory>

</hibernate-configuration>


数据库对象一对一的关系,在代码中主要体现在Annotation注解标签上,因此只需要看@注解标签的地方就行了!初学者,如有错误,请您指正!

你可能感兴趣的:(java,web开发,one2one,SSH应用)