hibernate一对一

10 篇文章 0 订阅

SQL:

CREATE TABLE TB_HUSBAND
(
    ID INTEGER PRIMARY KEY,
    NAME VARCHAR2(20) NOT NULL
);

CREATE SEQUENCE SQ_HUSBAND
INCREMENT BY 1
START WITH 1
NOMAXVALUE
NOCYCLE
CACHE 10;

CREATE TABLE TB_WIFE
(
    ID INTEGER PRIMARY KEY,
    NAME VARCHAR2(20) NOT NULL,
    HUSBAND_ID INTEGER NOT NULL REFERENCES TB_HUSBAND(ID)
);

CREATE SEQUENCE SQ_WIFE
INCREMENT BY 1
START WITH 1
NOMAXVALUE
NOCYCLE
CACHE 10;

java:

package com.one2one.pojo;

public class Husband {
	private int id;
	private String name;
	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;
	}

}

package com.one2one.pojo;

public class Wife {
	private int id;
	private String name;
	private Husband husband;

	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 Husband getHusband() {
		return husband;
	}

	public void setHusband(Husband husband) {
		this.husband = husband;
	}

}

hbm:

<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC
      "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
          "http://www.hibernate.org/dtd/hibernate-mapping-3.0.dtd">

<hibernate-mapping package="com.one2one.pojo">

	<class name="Husband" table="tb_husband">
		<id name="id" column="id" type="integer">
			<generator class="sequence">
				<param name="sequence">SQ_HUSBAND</param>
			</generator>
		</id>
		<property name="name" column="name" type="string" />
		<one-to-one name="wife" class="Wife" cascade="save-update" property-ref="husband" />
	</class>

</hibernate-mapping>

<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC
      "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
          "http://www.hibernate.org/dtd/hibernate-mapping-3.0.dtd">

<hibernate-mapping package="com.one2one.pojo">

	<class name="Wife" table="tb_wife">
		<id name="id" column="id" type="integer">
			<generator class="sequence">
				<param name="sequence">SQ_WIFE</param>
			</generator>
		</id>
		<property name="name" column="name" type="string" />
		<many-to-one name="husband" column="husband_id" class="Husband" cascade="save-update" unique="true" />
	</class>

</hibernate-mapping>

配置one-to-one或者many-to-one,要和SQL对应,many-to-one的unique要是true。

oracle.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>

		<!-- Database connection settings -->
		<property name="connection.driver_class">oracle.jdbc.driver.OracleDriver</property>
		<property name="connection.url">jdbc:oracle:thin:@localhost:1521:ORCL</property>
		<property name="connection.username">HIBERNATE</property>
		<property name="connection.password">HIBERNATE</property>

		<property name="connection.pool_size">1</property>

		<property name="dialect">org.hibernate.dialect.Oracle10gDialect</property>

		<property name="show_sql">true</property>

		<property name="hibernate.show_sql">true </property>
		
		<property name="jdbc.fetch_size">50</property>
		
		<mapping resource="com/one2one/pojo/Husband.hbm.xml"/>
		<mapping resource="com/one2one/pojo/Wife.hbm.xml"/>

	</session-factory>

</hibernate-configuration>

测试代码:

package com.one2one.test;

import java.util.Random;

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

import com.one2one.pojo.Husband;
import com.one2one.pojo.Wife;

import junit.framework.TestCase;

public class TestOne2One extends TestCase {
	private Session session = null;
	
	private static final String cfgName = "/oracle.hibernate.cfg.xml";
	
	private static final SessionFactory factory = new Configuration().configure(cfgName).buildSessionFactory();

	private Transaction tran = null;

	@Override
	protected void setUp() throws Exception {
		session = factory.openSession();
		tran = session.beginTransaction();
	}

	public void testInsert() {
		for (int i = 0; i < 5; i++) {
			Husband husband = new Husband();
			husband.setName("husband_" + i);
			
			Wife wife = new Wife();
			wife.setName("wife_" + i);
			
			husband.setWife(wife);
			wife.setHusband(husband);
			
			session.save(husband);
		}
		
		tran.commit();
	}
	
	@Override
	protected void tearDown() throws Exception {
		tran = null;
		session.close();
		session = null;
	}

}

testInsert后,数据库如下:

SQL> SELECT * FROM TB_HUSBAND;

        ID NAME
---------- --------------------
         1 husband_0
         2 husband_1
         3 husband_2
         4 husband_3
         5 husband_4

SQL> SELECT * FROM TB_WIFE;

        ID NAME                 HUSBAND_ID
---------- -------------------- ----------
         1 wife_0                        1
         2 wife_1                        2
         3 wife_2                        3
         4 wife_3                        4
         5 wife_4                        5

SQL>


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值