Person实体
public class Person {
private int id;
private String name;
private int age;
private Address address;
..........
}
Address实体
public class Address {
private int id;
private String addr;
.....
}
xml配置文件
Person.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.hibernate.model">
<class name="Person" table="t_person">
<id name="id" column="id">
<generator class="native" />
</id>
<property name="name" column="name" length="20" />
<property name="age" column="age" />
<many-to-one name="address" column="addressId" unique="true" not-null="true" />
</class>
</hibernate-mapping>
Address.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.hibernate.model">
<class name="Address" table="t_address">
<id name="id" column="id">
<generator class="native" />
</id>
<property name="addr" column="addr"/>
</class>
</hibernate-mapping>
测试示例:
保存
@Test
public void testSave(){
session = sessionFactory.openSession();
session.beginTransaction();
Address address = new Address();
address.setAddr("beijing");
Person p = new Person();
p.setName("lisi");
p.setAge(33);
p.setAddress(address);
session.save(address);
session.save(p);
session.getTransaction().commit();
session.close();
}
加载数据,单向映射,通过Person加载Address
@Test
public void testLoad(){
session = sessionFactory.openSession();
session.beginTransaction();
Person p = (Person)session.load(Person.class, 1);
System.out.println(p.getAddress().getAddr());
session.getTransaction().commit();
session.close();
}
hibernate.cfg.xml文件
<?xml version="1.0" encoding="utf-8" ?>
<!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>
<!-- Database connection settings -->
<property name="connection.driver_class">com.mysql.jdbc.Driver</property>
<property name="connection.url">jdbc:mysql://localhost:3306/hibernate
</property>
<property name="connection.username">root</property>
<property name="connection.password">admin</property>
<!-- JDBC connection pool (use the built-in) -->
<property name="connection.pool_size">1</property>
<!-- SQL dialect -->
<property name="dialect">org.hibernate.dialect.MySQLDialect</property>
<!-- Enable Hibernate's automatic session context management -->
<property name="current_session_context_class">thread</property>
<!-- Disable the second-level cache -->
<property name="cache.provider_class">org.hibernate.cache.internal.NoCacheProvider</property>
<!-- Echo all executed SQL to stdout -->
<property name="show_sql">true</property>
<property name="format_sql">true</property>
<!--
<property name="hibernate.id.new_generator_mappings">false</property>
-->
<!-- Drop and re-create the database schema on startup -->
<property name="hbm2ddl.auto">update</property>
<mapping resource="com/hibernate/model/Person.hbm.xml"/>
<mapping resource="com/hibernate/model/Address.hbm.xml"/>
</session-factory>
</hibernate-configuration>
hibernate实体到数据库工具:
public class HibernateUtils {
public static void main(String[] args) {
Configuration cfg = new Configuration().configure();
new SchemaExport(cfg).create(false, true);
}
}