hibernate 一对多 关系映射
短信预约 -IT技能 免费直播动态提醒
在关系中,或多或少的表之间有复杂的关联关系
这里测试 一对多 关联关系的配置
开发步骤就不多说了。直接看一个示例。
users 用户 和 shop商品订单表
分析:一个用户可以有多个商品订单,一个订单只属于一个用户。
这样就构成了一对多的关联关系。
这里用的是oracle 数据库
users 和 shop 数据库表
create table users(
id number(7) primary key,
name nvarchar2(20),
pass nvarchar2(20),
address nvarchar2(50)
)
create table shop(
id number(7) primary key,
price number(7),
sh_name nvarchar2(50),
us_id references users(id)
)
2.users 实体
package com.hib.entity;
import java.util.HashSet;
import java.util.Set;
public class Users {
private Integer id;
private String name;
private String pass;
private String address;
private Set<Shop> shop = new HashSet<Shop>();
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getPass() {
return pass;
}
public void setPass(String pass) {
this.pass = pass;
}
public String getAddress() {
return address;
}
public void setAddress(String address) {
this.address = address;
}
public Set<Shop> getShop() {
return shop;
}
public void setShop(Set<Shop> shop) {
this.shop = shop;
}
public Users(Integer id, String name, String pass, String address) {
super();
this.id = id;
this.name = name;
this.pass = pass;
this.address = address;
}
public Users() {
super();
}
@Override
public String toString() {
return "Users [id=" + id + ", name=" + name + ", pass=" + pass + ", address=" + address + "]";
}
}
shop 实体
package com.hib.entity;
public class Shop {
private Integer id;
private Integer price;
private String sh_name;
private Users users;
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public String getSh_name() {
return sh_name;
}
public void setSh_name(String sh_name) {
this.sh_name = sh_name;
}
public Integer getPrice() {
return price;
}
public void setPrice(Integer price) {
this.price = price;
}
public Users getUsers() {
return users;
}
public void setUsers(Users users) {
this.users = users;
}
public Shop(Integer id, String sh_name, Integer price) {
super();
this.id = id;
this.sh_name = sh_name;
this.price = price;
}
public Shop() {
super();
}
@Override
public String toString() {
return "Shop [id=" + id + ", sh_name=" + sh_name + ", price=" + price + "]";
}
}
3.shop.hbm.xml映射文件配置
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd" >
<hibernate-mapping>
<class name="com.hib.entity.Users" table="users">
<id name="id">
<generator class="increment">
</generator>
</id>
<property name="name"/>
<property name="pass"/>
<property name="address"/>
<!-- 1的一方配置
invese: true 不维护关系
-->
<set name="shop" inverse="true" cascade="all">
<!-- sh_id 是下面多一方的外键表中字段 -->
<key column="us_id"/>
<one-to-many class="com.hib.entity.Shop"/>
</set>
</class>
<class name="com.hib.entity.Shop" table="shop">
<id name="id">
<generator class="increment"></generator>
</id>
<property name="sh_name"/>
<property name="price"/>
<many-to-one name="users" class="com.hib.entity.Users" column="us_id" cascade="none"/>
</class>
</hibernate-mapping>
4. 不要忘记注册。
<!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.connection.driver_class">oracle.jdbc.OracleDriver</property>
<property name="hibernate.connection.username">ssh</property>
<property name="hibernate.connection.password">ssh</property>
<property name="hibernate.connection.url">jdbc:oracle:thin:@127.0.0.1:1521:xe</property>
<!-- 方言 -->
<property name="hibernate.dialect">org.hibernate.dialect.Oracle10gDialect</property>
<!-- 可视化sql -->
<property name="hibernate.show_sql">true</property>
<!-- 格式化sql,自动对可视化sql换行,对齐 -->
<property name="hibernate.format_sql">true</property>
<!-- 为getCurrentSession添加配置,保证事务中公用义个session -->
<property name="current_session_context_class">thread</property>
<!-- 注册 -->
<mapping resource="shop.hbm.xml"/>
</session-factory>
</hibernate-configuration>
5.测试 级联插入测试
@Test
public void insert(){
Session session = HibUtil.getSession();
Transaction tx = session.beginTransaction();
Users users = new Users(null, "苦力强", "kuliqiang", "zg");
Shop shop = new Shop(null, "橙子", 2800);
users.getShop().add(shop);
shop.setUsers(users);
session.save(users);
tx.commit();
}
记得要设置级联关系。
测试 查询
@Test
public void query(){
Session session = HibUtil.getSession();
Transaction tx = session.beginTransaction();
Users users = (Users)session.get(Users.class, 1);
for (Shop shop : users.getShop()) {
System.out.println(shop);
}
System.out.println(users);
tx.commit();
}
更新和删除就不测试了,更新先查询出来,再修改值,然后更新就可以了。
删除直接查询出来,删除就可以了。
如果是单向的1对多 上面的可以修改为
users 实体不改变
shop 中的 users去掉
配置文件中 去掉下面这段配置
<many-to-one name="users" class="com.hib.entity.Users" column="us_id" cascade="none"/>
4. 配置文件中 inverse="true" 去掉,告诉主表,来维护从表
<set name="shop" inverse="true" cascade="all">
其他的还是一样操作。
免责声明:
① 本站未注明“稿件来源”的信息均来自网络整理。其文字、图片和音视频稿件的所属权归原作者所有。本站收集整理出于非商业性的教育和科研之目的,并不意味着本站赞同其观点或证实其内容的真实性。仅作为临时的测试数据,供内部测试之用。本站并未授权任何人以任何方式主动获取本站任何信息。
② 本站未注明“稿件来源”的临时测试数据将在测试完成后最终做删除处理。有问题或投稿请发送至: 邮箱/279061341@qq.com QQ/279061341