1.组件是指:持久化类的属性并不是基本数据类型,也不是字符串、日期等标量类型的变量,而是一个复合类型的对象。
参考:http://www.tutorialspoint.com/hibernate/hibernate_component_mappings.htm
2.新建一个maven项目
3.编写pom.xml文件
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.meterbox</groupId>
<artifactId>HibernateDemo</artifactId>
<packaging>war</packaging>
<version>0.0.1-SNAPSHOT</version>
<name>HibernateDemo Maven Webapp</name>
<url>http://maven.apache.org</url>
<!-- 属性配置 -->
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<log4j.version>1.2.17</log4j.version>
<mysql.version>5.1.30</mysql.version>
</properties>
<dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.12</version>
<scope>test</scope>
</dependency>
<!-- 添加Hibernate依赖 -->
<dependency>
<groupId>org.hibernate</groupId>
<artifactId>hibernate-core</artifactId>
<version>3.6.10.Final</version>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>${mysql.version}</version>
</dependency>
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-api</artifactId>
<version>1.6.1</version>
</dependency>
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-nop</artifactId>
<version>1.6.4</version>
</dependency>
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-log4j12</artifactId>
<version>1.7.2</version>
</dependency>
<!-- 添加javassist -->
<dependency>
<groupId>javassist</groupId>
<artifactId>javassist</artifactId>
<version>3.11.0.GA</version>
</dependency>
<!-- jsp页面的jar -->
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>javax.servlet-api</artifactId>
<version>3.0.1</version>
</dependency>
<!-- 日志 -->
<dependency>
<groupId>log4j</groupId>
<artifactId>log4j</artifactId>
<version>${log4j.version}</version>
</dependency>
<dependency>
<groupId>commons-logging</groupId>
<artifactId>commons-logging</artifactId>
<version>1.2</version>
</dependency>
</dependencies>
<build>
<finalName>HibernateDemo</finalName>
</build>
</project>
4.编写Hibernate配置文件
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE hibernate-configuration PUBLIC "-//Hibernate/Hibernate Configuration DTD 3.0//EN"
"http://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://localhost:3306/zhjdata?useUnicode=true&characterEncoding=UTF-8</property>
<property name="connection.username">root</property>
<property name="connection.password">123456</property>
<!-- 显示SQL语句 -->
<property name="show_sql">true</property>
<property name="format_sql">true</property>
<!-- 根据需要自动创建数据库 -->
<property name="hbm2ddl.auto">update</property>
<!-- 定义方言 -->
<property name="dialect">
org.hibernate.dialect.MySQL5Dialect
</property>
<!-- 指定连接池里最大连接数 -->
<property name="hibernate.c3p0.max_size">20</property>
<!-- 指定连接池里最小连接数 -->
<property name="hibernate.c3p0.min_size">1</property>
<!-- 指定连接池里连接的超时时长 -->
<property name="hibernate.c3p0.timeout">5000</property>
<!-- 指定连接池里最大缓存多少个Statement对象 -->
<property name="hibernate.c3p0.max_statements">100</property>
<property name="hibernate.c3p0.idle_test_period">3000</property>
<property name="hibernate.c3p0.acquire_increment">2</property>
<property name="hibernate.c3p0.validate">true</property>
<mapping resource="cn/edu/hpu/component/model/Employee.hbm.xml"/>
</session-factory>
</hibernate-configuration>
5.编写Address类和Person类
package cn.edu.hpu.component.model;
public class Address {
private int id;
private String street;
private String city;
private String state;
private String zipcode;
public Address() {
}
public Address(String street, String city, String state, String zipcode) {
this.street = street;
this.city = city;
this.state = state;
this.zipcode = zipcode;
}
//get.. set..
}
package cn.edu.hpu.component.model;
public class Employee implements java.io.Serializable {
private int id;
private String firstName;
private String lastName;
private int salary;
private Address address;
public Employee() {
}
public Employee(String fname, String lname, int salary, Address address) {
this.firstName = fname;
this.lastName = lname;
this.salary = salary;
this.address = address;
}
//get .. set ...
}
6.编写Employee.hbm.xml文件
<?xml version="1.0" encoding="UTF-8"?>
<!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.edu.hpu.component.model">
<class name="Employee" table="EMPLOYEE">
<meta attribute="class-description">
This class contains the employee detail.
</meta>
<id name="id" type="int" column="id">
<generator class="native"/>
</id>
<component name="address" class="Address">
<property name="street" column="street_name" type="string"/>
<property name="city" column="city_name" type="string"/>
<property name="state" column="state_name" type="string"/>
<property name="zipcode" column="zipcode" type="string"/>
</component>
<property name="firstName" column="first_name" type="string"/>
<property name="lastName" column="last_name" type="string"/>
<property name="salary" column="salary" type="int"/>
</class>
</hibernate-mapping>
7.编写HibernateUtil类
package cn.edu.hpu.util;
import org.apache.log4j.Logger;
import org.hibernate.HibernateException;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.cfg.Configuration;
public class HibernateUtil {
/** ThreadLocal Session Map */
public static final ThreadLocal<Session> SESSIONMAP = new ThreadLocal<Session>();
private static final SessionFactory sessionFactory;
private static final Logger LOGGER = Logger.getLogger("appender2");
static {
try {
LOGGER.debug("HibernateUti.static - loading cofig");
sessionFactory = new Configuration().configure("hibernate.cfg.xml")
.buildSessionFactory();
LOGGER.debug("HibernateUtil.static - end");
} catch (Throwable ex) {
ex.printStackTrace();
LOGGER.error("HibernateUti error : ExceptionInInitializerError");
throw new ExceptionInInitializerError(ex);
}
}
private HibernateUtil() {
}
public static Session getSession() throws HibernateException {
Session session = SESSIONMAP.get();
if (session == null) {
session = sessionFactory.openSession();
SESSIONMAP.set(session);
}
return session;
}
public static void closeSession() throws HibernateException {
Session session = SESSIONMAP.get();
SESSIONMAP.set(null);
if (session != null) {
session.close();
}
}
}
8.写测试类
public class PersonManger {
@Test
public void testSaveCompont() {
Session session = HibernateUtil.getSession();
Transaction trans = session.beginTransaction();
Address address = new Address("文化路","深圳","22号","1栋");
Address address2 = new Address("文化路","北京","22号","2栋");
Employee em1 = new Employee("Zhang","Jomes",888888,address);
Employee em2 = new Employee("Zhang2","Jomes2",999999,address2);
session.save(em2);
session.save(em1);
trans.commit();
HibernateUtil.closeSession();
}
}