场景:多个用户,对应一个地址。
1、核心配置文件hibernat.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>
<!-- 方言 -->
<property name="hibernate.dialect">org.hibernate.dialect.MySQLDialect</property>
<!-- 驱动 -->
<property name="hibernate.connection.driver_class">com.mysql.jdbc.Driver</property>
<!-- URL -->
<property name="hibernate.connection.url">jdbc:mysql://127.0.0.1:3306/hb</property>
<!-- 用户名 -->
<property name="hibernate.connection.username">root</property>
<!-- 密码 -->
<property name="hibernate.connection.password">1921</property>
<!-- 在控制台显示SQL语句 -->
<property name="show_sql">true</property>
<!-- 对控制台语句序列化 -->
<property name="format_sql">true</property>
<!-- ddl语句自动建表,update:无表是创建,有表时插入新数据 -->
<property name="hbm2ddl.auto">update</property>
<!--注册实体类User映射文件 -->
<mapping resource="com/caokaiyuan/domain/User.hbm.xml"/>
<!-- 注册实体类Address映射文件 -->
<mapping resource="com/caokaiyuan/domain/Address.hbm.xml"/>
</session-factory>
</hibernate-configuration>
2、User类和User.hbm.xml
package com.caokaiyuan.domain;
public class User
{
private int id;
private String username;
private String password;
private Address address;
public User()
{
}
public int getId()
{
return id;
}
public void setId(int id)
{
this.id = id;
}
public String getUsername()
{
return username;
}
public void setUsername(String username)
{
this.username = username;
}
public String getPassword()
{
return password;
}
public void setPassword(String password) {
this.password = password;
}
public Address getAddress()
{
return address;
}
public void setAddress(Address address)
{
this.address = address;
}
}
User.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="com.caokaiyuan.domain">
<class name="User" table="tb_user">
<!-- 配置主键映射 -->
<id name="id" column="id">
<generator class="increment"/>
</id>
<property name="username"/>
<property name="password"/>
<!-- 配置多对一映射 -->
<many-to-one name="address" class="Address" column="address_id">
</many-to-one>
</class>
</hibernate-mapping>
3、地址类
Address.java文件
package com.caokaiyuan.domain;
public class Address
{
private int addressId;
private String addressDetail;
public Address()
{
}
public int getAddressId()
{
return addressId;
}
public void setAddressId(int addressId)
{
this.addressId = addressId;
}
public String getAddressDetail()
{
return addressDetail;
}
public void setAddressDetail(String addressDetail)
{
this.addressDetail = addressDetail;
}
}
Address.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="com.caokaiyuan.domain">
<class name="Address" table="tb_address">
<!-- 配置主键映射 -->
<id name="addressId" column="address_id">
<generator class="increment"/>
</id>
<property name="addressDetail"/>
</class>
</hibernate-mapping>
3、测试类
package com.caokaiyuan.test;
import org.hibernate.Session;
import org.hibernate.Transaction;
import com.caokaiyuan.domain.Address;
import com.caokaiyuan.domain.User;
import com.caokaiyuan.util.HibernateUtil;
public class ManyToOne
{
public static void main(String[] args)
{
//获得Session对象
Session session = HibernateUtil.getSessionfactory().openSession();
//获得事务对象
Transaction tc = session.getTransaction();
//开始事务
tc.begin();
//创建地址对象
Address address = new Address();
address.setAddressDetail("广东");
session.save(address);
//创建用户对象
User user1 = new User();
user1.setUsername("tom1");
user1.setPassword("tom1");
user1.setAddress(address);
session.save(user1);
User user2 = new User();
user2.setUsername("tom2");
user2.setPassword("tom2");
user2.setAddress(address);
session.save(user2);
//提交事务
tc.commit();
//关闭Session对象
session.close();
}
}
4、结果
Hibernate:
select
max(address_id)
from
tb_address
Hibernate:
select
max(id)
from
tb_user
Hibernate:
insert
into
tb_address
(addressDetail, address_id)
values
(?, ?)
Hibernate:
insert
into
tb_user
(username, password, address_id, id)
values
(?, ?, ?, ?)
Hibernate:
insert
into
tb_user
(username, password, address_id, id)
values
(?, ?, ?, ?)