概述
Hibernate:级联新增
案例讲解: 订单和订单项
1.1:订单类:
public class Order {
private Integer orderId;
private String orderNo;
//定义一对多的关系时一定需要采用接口方式,不许使用实现类
//implements java.util.Set 代理实现的是set接口
private Set<OrderItem> orderItems=new HashSet<OrderItem>();
public Set<OrderItem> getOrderItems() {
return orderItems;
}
public void setOrderItems(Set<OrderItem> orderItems) {
this.orderItems = orderItems;
}
public Integer getOrderId() {
return orderId;
}
public void setOrderId(Integer orderId) {
this.orderId = orderId;
}
public String getOrderNo() {
return orderNo;
}
public void setOrderNo(String orderNo) {
this.orderNo = orderNo;
}
}
1.2:订单项类:
public class OrderItem {
private Integer orderItemId;
private Integer productId;
private Integer quaneity;
private Integer oid;
private Order order;
public Order getOrder() {
return order;
}
public void setOrder(Order order) {
this.order = order;
}
public Integer getOrderItemId() {
return orderItemId;
}
public void setOrderItemId(Integer orderItemId) {
this.orderItemId = orderItemId;
}
public Integer getProductId() {
return productId;
}
public void setProductId(Integer productId) {
this.productId = productId;
}
public Integer getQuaneity() {
return quaneity;
}
public void setQuaneity(Integer quaneity) {
this.quaneity = quaneity;
}
public Integer getOid() {
return oid;
}
public void setOid(Integer oid) {
this.oid = oid;
}
}
1.3:order.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>
<class table="t_hibernate_order" name="com.crud.four.entity.Order">
<id name="orderId" type="java.lang.Integer" column="order_id ">
<generator class="increment"></generator>
</id>
<property name="orderNo" type="java.lang.String" column="order_no "></property>
<!--
name:实体类的类属性
cascade:用来配置维护实体类之间的关系所用
inverse:(inverse="true")关系交由反方控制 即订单项 因为现在配置的是订单,所以反方是订单项
(inverse="true") 关系由自己控制 订单
lazy="false":立即加载
-->
<set name="orderItems" cascade="save-update" inverse="true">
<!-- 订单项外键 -->
<key column="oid"></key>
<!-- 一个订单对应多个订单项 -->
<one-to-many class="com.crud.four.entity.OrderItem"></one-to-many>
</set>
</class>
</hibernate-mapping>
1.4:oederItem.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>
<class table="t_hibernate_order_item" name="com.crud.four.entity.OrderItem">
<id name="orderItemId" type="java.lang.Integer" column="order_item_id ">
<generator class="increment"></generator>
</id>
<property name="productId" type="java.lang.Integer" column="product_id "></property>
<property name="quaneity" type="java.lang.Integer" column="quantity "></property>
<!--解决报错方案:-->
<!-- 第一种加insert=false,update=false -->
<property name="oid" type="java.lang.Integer" column="oid" insert="false" update="false"></property>
<!-- 第二种加insert=false,update=false -->
<!-- <property name="oid" type="java.lang.Integer" column="oid"></property> -->
<!-- 多对一
order:orderItem里的属性,也是实体类
oid:实体类的属性所对应的后台的字段
-->
<!-- 会报错 :解决方法:第一种加,第二种加-->
<!-- 第一种加insert=false,update=false -->
<many-to-one name="order" class="com.crud.four.entity.Order" column="oid"></many-to-one>
<!-- 第二种加insert=false,update=false -->
<!-- <many-to-one name="order" class="com.crud.four.entity.Order" column="oid" insert="false" update="false"></many-to-one> -->
</class>
</hibernate-mapping>
1.5: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>
<!-- 1. 数据库相关 -->
<property name="connection.username">root</property>
<property name="connection.password">123</property>
<property name="connection.url">jdbc:mysql://localhost:3306/test?useUnicode=true&characterEncoding=UTF-8</property>
<property name="connection.driver_class">com.mysql.jdbc.Driver</property>
<!-- 连接MySQL数据库 -->
<property name="dialect">org.hibernate.dialect.MySQLDialect</property>
<!-- 连接Oracle数据库 -->
<!-- <property name="dialect">org.hibernate.dialect.Oracle10gDialect</property> -->
<!-- 2.配置本地事务(No CurrentSessionContext configured!) -->
<property name="hibernate.current_session_context_class">thread</property>
<!-- 3. 调试相关 -->
<property name="show_sql">true</property>
<property name="format_sql">true</property>
<!-- 4.hibernate需要管理的数据库表对应的实体类映射文件 -->
<!-- 级联查询 -->
<mapping resource="com/crud/four/entity/order.hbm.xml"></mapping>
<mapping resource="com/crud/four/entity/orderItem.hbm.xml"></mapping>
</session-factory>
</hibernate-configuration>
1.6:dao方法:
public class OrderDao {
//新增订单项
public Integer addOrderItem(OrderItem orderItem) {
Session session = SessionFactoryUtils.getSession();
Transaction transaction = session.beginTransaction();
Integer otid =(Integer) session.save(orderItem);
transaction.commit();
session.close();
return otid;
}
}
1.7:junit测试:
public class OrderDaoTest {
private OrderDao orderDao=new OrderDao();
private OrderItem orderItem=new OrderItem();
private Order order=new Order();
@Test
public void testAddOrderItem() {
//第一种加insert="false" update="false"
orderItem.setProductId(45);
orderItem.setQuaneity(99);
order.setOrderId(1);
orderItem.setOrder(order);
this.orderDao.addOrderItem(orderItem);
//第二种加insert="false" update="false"
// orderItem.setOid(1);
// orderItem.setProductId(54);
// orderItem.setQuaneity(91);
// this.orderDao.addOrderItem(orderItem);
}
}
当 property 没有设置insert和update属性时会报错:
<property name="oid" type="java.lang.Integer" column="oid" ></property>
报错:
Repeated(重复) column in mapping for entity: com.zking.four.entity.OrderItem column: oid (should be mapped with insert=“false” update=“false”)
- 原因:同一个字段(外键oid在orderItem.hbm.xml中)被映射了两次
- 解决方案:
- 1、删除从表对应的实体类中的外键属性 (把order里的oid删除)
2、在配置的xml中外键属性上添加 insert=false,update=false的设置。
3、在配置的xml中的manyToOne标签中添加insert=false,update=false的设置。 - orderItem.hbm.xml里的字段重复了 oid
hibernate:普通删除
1.hibernate.cfg.xml,order.hbm.xml,orderItem.hbm.xml与上面相同
2.dao方法:
//普通删除
public void deleteOrder(Order order) {
Session session = SessionFactoryUtils.getSession();
Transaction transaction = session.beginTransaction();
Order o = session.get(Order.class, order.getOrderId()); //找到数据
for (OrderItem ot : o.getOrderItems()) {
session.delete(ot); //删除订单项
}
session.delete(o); //删除订单
transaction.commit();
session.close();
}
3:junit测试:
@Test
public void testDeleteOrder() {
order.setOrderId(3);
this.orderDao.deleteOrder(order);
}
最后
以上就是善良彩虹为你收集整理的Hibernate:级联新增与普通删除的全部内容,希望文章能够帮你解决Hibernate:级联新增与普通删除所遇到的程序开发问题。
如果觉得靠谱客网站的内容还不错,欢迎将靠谱客网站推荐给程序员好友。
本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
发表评论 取消回复