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>



                
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

94甘蓝

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值