hibernate关系映射

时间:2023-03-08 15:39:05

多对一:比如多个订单对应同一个用户,需要在订单表中添加一个用户的属性

订单类:

private Integer orderId;
private Date createTime;
private User user;

用户类:

private int userId;
private String userName;
private String userPsw;

一的方配置xml(user):

<?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>
<class name="ni.jun.yang.bean.User" table="t_user">
<id name="userId">
<!-- 主键自动增长 -->
<generator class="native"></generator>
</id>
<property name="userName"></property>
<property name="userPsw"></property>
</class>
</hibernate-mapping>

多的一方配置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>
<class name="ni.jun.yang.bean.Orders" table="t_order">
<id name="orderId" column="orderId">
<generator class="native"></generator>
</id>
<property name="createTime" type="date"></property> <!-- 多个订单对应一个用户,配置多对一的映射 cascade级联-->
<many-to-one name="user" class="ni.jun.yang.bean.User" column="userId" cascade="all">
</many-to-one>
</class>
</hibernate-mapping>
 <many-to-one name="user" class="ni.jun.yang.bean.User" column="userId"  cascade="all">  
name:类中的成员变量名字
class:对应类的全路径,配了包名只需要写类名即可
column:数据库里面的字段名字
cascade:级联 其值有“all”,“save-update”,“delete”等 测试代码:由于配置级联 cascade=‘all’,保存订单会首先自动保存级联的用户对象
Session session =  HibernateUtil.getSessionFactory().openSession();
Transaction ts = session.beginTransaction();
User user = new User();
user.setUserName("不知3");
user.setUserPsw("4563"); Orders orders1 =new Orders();
orders1.setCreateTime(new Date());
orders1.setUser(user); session.save(orders1);
ts.commit();
session.close();

一对多:比如一个人可以有多张银行卡

一的一方需要设置一个 集合来存放多的一方 使用list 或者set集合,一般多用set集合

如:一个人有多个地址,list集合存放

private List<String> addresses;

xml配置:list用list标签,要配置索引 list-index

<list name="addresses" table="address">
<key column="customerId"></key>
<!-- 用list需要配list索引,相当于下标从0开始 -->
<list-index column="ListIndex"></list-index>
<element type="string" column="addressName"></element>
</list>

set集合存放:

private Set<String> addresses;

xml配置:set用set标签

<set name="addresses" table="address" sort="natural">
<key column="customerId"></key>
<element type="string" column="addressName"></element>
</set>

双向一对多:如一个人有多套房,他就有多个地址

customer:

private Integer customerId;
private String customerName;
private Set<Address> adresses;

address:

private Customer customer;
private Integer addressId;
private String address;

customer  xml配置:

<!-- inverse=true 放弃关联关系维护,默认false,关系维护-->
<set name="adresses" table="_Address" inverse="true" cascade="save-update">
<key column="customerId"></key>
<one-to-many class="ni.jun.yang.bean.Address" />
</set>

address  xml配置

<many-to-one name="customer" class="Customer" column="customerId"></many-to-one>

测试代码:

Session session =  HibernateUtil.getSessionFactory().openSession();
Transaction ts = session.beginTransaction();
Address address1 = new Address();
address1.setAddress("青羊区");
Address address2 = new Address();
address2.setAddress("武侯区");
Set<Address> set = new HashSet<Address>();
set.add(address1);
set.add(address2); Customer customer = new Customer();
customer.setCustomerName("zhubajie");
customer.setAdresses(set);
session.save(customer);
ts.commit();
session.close();

多对多,需要第三张表来建立关联关系,必须有一方放弃关联关系维护

类里面两边互相有对方作为成员变量

items:

private Set<Orders> orders=new HashSet<Orders>();

orders:

private Set<Items> items = new HashSet<>();

xml配置里面要设置第三张表来关联:orders_r_items  第三张表

items xml:

<set name="orders" table="orders_r_items">
<key column="itemsId"></key>
<many-to-many class="Orders" column="orderId"></many-to-many>
</set>

orders xml:

<!-- items的集合 多对多,必须有一方放弃关联关系维护 -->
<set name="items" table="orders_r_items" inverse="true">
<key column="orderId"></key>
<many-to-many class="Items" column="itemsId"></many-to-many>
</set>

测试代码:

Session session = HibernateUtil.getSession();
Transaction tx = session.beginTransaction(); Items item = new Items();
item.setPrice(new Double(100));
Items item1 = new Items();
item1.setPrice(new Double(200)); Orders o = new Orders();
o.setCreateTime(new Date());
Orders o1 = new Orders();
o1.setCreateTime(new Date()); o.getItems().add(item);
o.getItems().add(item1); o1.getItems().add(item);
o1.getItems().add(item1); item.getOrders().add(o);
item.getOrders().add(o1); item1.getOrders().add(o);
item1.getOrders().add(o1); session.save(item);
session.save(item1); session.save(o);
session.save(o1); tx.commit();
session.close();