hibernate一对一

使用人和身份证举例说明一对一关联,一对一有两种方式,一种是基于主键,另一种是基于外键,这里演示的是外键方式

表结构如下:

---------------------------

person表

personId        //主键

personName //姓名

---------------------------

card表

cardId      //主键

number    //身份证号

personId  //外键,关联person表

---------------------------

 

 

代码结构和使用的jar

 

Card.java(省略get set)

package com.orange.demo;
/**
 * 身份证实体类
 */
public class Card {
	private Integer cardId;
	private String number;
	private Person person;
	
}

 

Person.java(省略get set)

package com.orange.demo;

/**
 * 人的实体类
 */
public class Person {
	private Integer personId;
	private String personName;
	private Card card;

}


Card.hbm.xml

<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC
        "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
        "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
<hibernate-mapping package="com.orange.demo">
	<class name="Card" table="t_card">
		<id name="cardId" column="cardId">
			<generator class="native"></generator>
		</id>
		<property name="number" column="number" type="string"></property>
		
		<many-to-one name="person"  class = "com.orange.demo.Person" column="personId"></many-to-one>
	</class>
</hibernate-mapping>


Person.hbm.xml

<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC
        "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
        "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
<hibernate-mapping package="com.orange.demo">
	
	<class name="Person" table="t_person">
		<id name="personId" column="personId">
			<generator class="native"></generator>
		</id>
		<property name="personName" column="personName" type="string"></property>
		<!--property-ref 对方(身份证)外键(personId)对应的属性名(person)  -->
		<one-to-one name="card" class="com.orange.demo.Card" property-ref="person"></one-to-one>
	</class>
	
</hibernate-mapping>


Test.java

package com.orange.demo;

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


public class Test {
	
	private static SessionFactory sessionFactory = null;
	static{
		Configuration cfg = new Configuration();
		cfg.configure("hibernate.cfg.xml");		//在src目录下
		sessionFactory = cfg.buildSessionFactory();
	}
	
	public static void main(String[] args) {
		try {
			save();

//			delete();
		} catch (Exception e) {
			e.printStackTrace();
		}
	}


	public static void save() throws Exception {
		Session session = sessionFactory.openSession();
		Transaction tx =  session.beginTransaction();
		//保存人的信息
		Person person = new Person();
		person.setPersonName("大橙子");
		//保存身份证信息
		Card card = new Card();
		card.setNumber("88888");
		//设置关联关系
		person.setCard(card);
		card.setPerson(person);
		//保存到数据库
		session.save(person);
		session.save(card);
		tx.commit();
		session.close();
	
	}

	
	public static void delete() throws Exception{
		Session session = sessionFactory.openSession();
		Transaction tx =  session.beginTransaction();
		
		//可以删除card
		Card card = (Card) session.get(Card.class, new Integer(1));
		session.delete(card);
		
		//删除person报错,有外键约束不能删除
//		Person person = (Person) session.get(Person.class, new Integer(1));
//		session.delete(person);
		
		tx.commit();
		session.close();
	}
}

 

hibernate.cfg.xml

<!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 name="foo">
			<!-- 数据库方言 --> 
		<property name="hibernate.dialect">org.hibernate.dialect.MySQLDialect</property>
		<property name="connection.url">jdbc:mysql://localhost:3306/t1?characterEncoding=UTF-8</property>
		<property name="connection.driver_class">com.mysql.jdbc.Driver</property>
		<property name="connection.username">root</property>
		<property name="hibernate.connection.password">123456</property>
		<!-- 在控制台打印SQL,但不能打印建表语句 -->
		<property name="hibernate.show_sql">true</property>
		<!-- 打印SQL是否格式化 false:不格式化 -->
		<property name="hibernate.format_sql">false</property>
		<!--自动更新数据库结构 -->
		<property name="hbm2ddl.auto">update</property>
		
		<mapping resource="com/orange/demo/Card.hbm.xml"/>
		<mapping resource="com/orange/demo/Person.hbm.xml"/>
	
	</session-factory>
</hibernate-configuration>




 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值