码迷,mamicode.com
首页 > Web开发 > 详细

【6.2】hibernate_component

时间:2017-04-07 09:32:15      阅读:171      评论:0      收藏:0      [点我收藏+]

标签:finally   问题:   ppi   .hbm.xml   map   images   rowset   gen   tran   

问题:一个老师有多个家庭地址,如何存储该老师的信息?

1.Address类:

package cn.siggy.pojo;

public class Address {
	private String addr1;
	private String addr2;
	private String addr3;
	public String getAddr1() {
		return addr1;
	}
	public void setAddr1(String addr1) {
		this.addr1 = addr1;
	}
	public String getAddr2() {
		return addr2;
	}
	public void setAddr2(String addr2) {
		this.addr2 = addr2;
	}
	public String getAddr3() {
		return addr3;
	}
	public void setAddr3(String addr3) {
		this.addr3 = addr3;
	}
}

  

 2.Teacher类:

package cn.siggy.pojo;

public class Teacher {
	private int id;
	private String name;
	private String sex;
	private Address address;
	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 String getSex() {
		return sex;
	}
	public void setSex(String sex) {
		this.sex = sex;
	}
	public Address getAddress() {
		return address;
	}
	public void setAddress(Address address) {
		this.address = address;
	}
}

  

3.Teacher.hbm.xml

<?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="cn.siggy.pojo">
	<class name="Teacher">
		<id name="id">
			<generator class="native"></generator>
		</id>
		<property name="name"/>
		<property name="sex"/>
		<!-- 组件 -->
		<component name="address" class="Address">
			<property name="addr1"/>
			<property name="addr2"/>
			<property name="addr3"/>
		</component>
	</class>
</hibernate-mapping>

  

4.测试代码:

package cn.siggy.test;

import java.sql.SQLException;

import javax.sql.rowset.serial.SerialException;

import org.hibernate.HibernateException;
import org.hibernate.Session;
import org.hibernate.Transaction;
import org.hibernate.cfg.Configuration;
import org.hibernate.tool.hbm2ddl.SchemaExport;
import org.junit.Test;

import cn.siggy.pojo.Address;
import cn.siggy.pojo.Teacher;
import cn.siggy.util.HibernateUtil;

public class HibernateTest {
	@Test
	public void testCreateDB(){
		Configuration cfg = new Configuration().configure();
		SchemaExport se = new SchemaExport(cfg);
		//第一个参数 是否生成ddl脚本  第二个参数  是否执行到数据库中
		se.create(true, true);
	}
	@Test
	public void testSave() throws HibernateException, SerialException, SQLException{
		Session session = null;
		Transaction tx = null;
		try{
			session = HibernateUtil.getSession();
			tx = session.beginTransaction();
			Teacher t = new Teacher();
			t.setName("老裴");
			t.setSex("男");
			Address address = new Address();
			address.setAddr1("西三旗");
			address.setAddr2("西直门");
			address.setAddr3("南六环");
			t.setAddress(address);
			
			session.save(t);
			
			tx.commit();
			
		}catch (HibernateException e) {
			if(tx!=null)
				tx.rollback();
			e.printStackTrace();
			throw e;
		}finally{
			HibernateUtil.closeSession();
		}
	}
}

 

5.测试结果:

控制台信息:

技术分享

技术分享

 

数据库表信息:

技术分享

 

 

 

【6.2】hibernate_component

标签:finally   问题:   ppi   .hbm.xml   map   images   rowset   gen   tran   

原文地址:http://www.cnblogs.com/chxbar/p/6676449.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!