hibernate组件映射

来源:互联网 发布:电子贺卡软件 编辑:程序博客网 时间:2024/05/16 06:59

我们有时候有一张数据库表,但是这个数据库表中有几列的数据都是类似的,可以使用一个独立的实体类来表示,这个就叫做组件映射,即有个实体类或者多个实体类对应一张数据库表,遇到这个组件映射的情况我们该怎么配置hibernate的实体类配置文件呢?今天就来说说这个问题---组件映射。


新建一个新的java项目,名称为:07hibernate_component

结构如图:



需要的jar可以参见《Hibernate环境搭建和配置


新建一个实体类Teacher,代码如下:

package com.robert.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;}}

Teacher类对应的额组件是Address(也就是Teacher的地址组件),代码如图:

package com.robert.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;}}

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

Teacher.hbm.xml中配置文件和Teacher、Address关系如图:



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><!-- 配置数据库连接信息 --><property name="connection.driver_class">com.mysql.jdbc.Driver</property><property name="connection.url">jdbc:mysql:///hibernate4</property><property name="connection.username">root</property><property name="connection.password">root</property><!-- 数据库方言 --><property name="hibernate.dialect">org.hibernate.dialect.MySQL5Dialect</property><!-- 是否打印sql语句 --><property name="show_sql">true</property><!-- 格式化sql语句 --><property name="format_sql">true</property><!-- 数据库更新方式: 1、create:每次更新都先把原有数据库表删除,然后创建该表;2、create-drop:使用create-drop时,在显示关闭SessionFacroty时(sessionFactory.close()),将drop掉数据库Schema(表) 3、validate:检测;4、update(常用):如果表不存在则创建,如果存在就不创建--><property name="hbm2ddl.auto">update</property><!-- 加载Score实体类对应的配置文件 --><mapping resource="com/robert/pojo/Teacher.hbm.xml" /></session-factory></hibernate-configuration>


HibernateUtil代码如下:

package com.robert.util;import org.hibernate.Session;import org.hibernate.SessionFactory;import org.hibernate.boot.registry.StandardServiceRegistryBuilder;import org.hibernate.cfg.Configuration;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(new StandardServiceRegistryBuilder().applySettings(cfg.getProperties()).build());}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();}}



HIbernateTest测试类的代码如下:

package com.robert.test;import java.io.File;import java.io.FileInputStream;import java.io.IOException;import java.sql.Blob;import java.sql.Clob;import java.sql.SQLException;import javax.sql.rowset.serial.SerialBlob;import javax.sql.rowset.serial.SerialClob;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 com.robert.pojo.Address;import com.robert.pojo.Teacher;import com.robert.util.HibernateUtil;public class HibernateTest {/** * 根据*.hbm.xml文件对应的生成数据库表 */@Testpublic void testCreateDB() {Configuration cfg = new Configuration().configure();SchemaExport se = new SchemaExport(cfg);// 第一个参数:是否生成ddl脚本// 第二个参数:是否执行到数据库中se.create(true, true);}/** * @throws HibernateException * @throws SerialException * @throws SQLException * @throws IOException */@Testpublic void testSave() throws HibernateException, SerialException,SQLException, IOException {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();}}}




使用Junit4执行数据库表生成语句,如图:



使用Junit4实行testSave方法,将数据存入数据库表Teacher中,如图:



数据库表中的数据如图: