Hibernate(七)组件映射

  数据库表中有几列的数据都是类似的,可以使用一个独立的实体类来表示,这个就叫做组件映射,即有个实体类或者多个实体类对应一张数据库表。

User类

package test;
public class User {
	private int id;
	private String name;
	private Address address;		
}

Address类

public class Address {	 
		private String addr1 ;
		private String addr2 ;
		private String addr3 ;
}

User类的User_hbm.xml
在这里插入图片描述
hibernate.cfg.xml(Address不需要hbm.xml

<?xml version="1.0" encoding="UTF-8"?>
<hibernate-configuration>
	<session-factory><!-- serverTimezone=GMT%2B8 -->
		<!-- 连接数据库及hibernate的方言配置 -->
		<property name="hibernate.connection.driver_class">com.mysql.jdbc.Driver</property>
		<property name="hibernate.connection.url">jdbc:mysql://localhost:3306/testconn?serverTimezone=GMT%2B8</property>
		<property name="hibernate.connection.username">root</property>
		<property name="hibernate.connection.password">root</property>
		<property name="hibernate.dialect">org.hibernate.dialect.MySQL5Dialect</property> 
		<!-- hibernate的其他配置 -->
		<property name="hibernate.show_sql">true</property><!--在控制台中打印sql语句 -->
		<property name="hibernate.format_sql">true</property><!-- 打印前格式化sql一句 -->
		<!-- ddl策略 -->
		<property name="hibernate.hbm2ddl.auto">update</property>
		<!-- 设置是否自动提交事务 -->
		<property name="hibernate.connection.autoconmmit">false</property>
		<mapping resource="test/User_hbm.xml"/>				
	</session-factory>
</hibernate-configuration>

HibernateUtil工厂类

public class HibernateUtil {
 
	private static Configuration cfg = null;
	private static SessionFactory factory = null;
	private static Session session = null ;
	
	static {
		init();
	}
 
	public static void init() {
		cfg = new Configuration().configure();
		factory = cfg.buildSessionFactory();
	}
 
	public static Session getSession() {
		if (factory != null){
			return session = factory.openSession();
		}
		
 
		init();
		return session = factory.openSession();
	}
	
	public static void closeSession() {
		if(session!=null && session.isOpen())
			session.close();
	}
 
}

通过Jubit5测试:

public class Test {
 	/**
	 * 根据*.hbm.xml文件对应的生成数据库表
	 */
	@org.junit.Test
	public void testCreateDB() {
		Configuration cfg = new Configuration().configure();

	} 
	/**
	 * @throws HibernateException
	 * @throws SerialException
	 * @throws SQLException
	 * @throws IOException
	 */
	@org.junit.Test
	public void testSave() throws HibernateException, SerialException,
			SQLException, IOException {
		Session session = null;
		Transaction tx = null;
		try {
			session = HibernateUtil.getSession();
			tx = session.beginTransaction();
 
			User user = new User();
			user.setName("Waylon");	
			Address address = new Address();
			address.setAddr1("北京朝阳");
			address.setAddr2("东城区");
			address.setAddr3("西城区");
			user.setAddress(address) ;
			
			session.save(user);
			
			tx.commit();
 
		} catch (HibernateException e) {
			if (tx != null) {
				tx.rollback();
			}
			e.printStackTrace();
			throw e;
		} finally {
			HibernateUtil.closeSession();
		}
	}
 
}

生成的SQL语句
在这里插入图片描述
查看数据库的数据
在这里插入图片描述

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值