我的程序中有订单和项目实体类。在order class中有一个Set 项目。当我插入订单时,订单详细信息必须插入订单表。并且必须将Set中的项插入到Items表中,该表在Order表中具有order_id的外键。因此,我想要的查询是:
insert into ordertb (cName) values (?);
insert into item (id, order_id, qty) values(?,?,?);
insert into item (id, order_id, qty) values(?,?,?);
insert into item (id, order_id, qty) values(?,?,?);
// until all the items are inserted
但是hibernate会生成以下查询:
Hibernate: insert into ordertb (cName) values (?)
Hibernate: update item set qty=?, order_id=? where id=?
Hibernate: update item set qty=?, order_id=? where id=?
Hibernate: update item set qty=?, order_id=? where id=?
Hibernate: update item set order_id=? where id=?
Hibernate: update item set order_id=? where id=?
Hibernate: update item set order_id=? where id=?
这是我的映射文件
<?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="Order" table="ordertb">
<meta attribute="class-description">
This class contains the Order detail.
</meta>
<id name="order" type="int" column="order_id">
<generator class="native"/>
</id>
<property name="cName" column="cName" type="string"/>
<set name="items" table="item" fetch="select" cascade="all">
<key>
<column name="order_id" not-null="true"></column>
</key>
<one-to-many class="Item"/>
</set>
</class>
</hibernate-mapping>
TestComp.java,我调用方法
import java.util.ArrayList;
import java.util.HashSet;
import java.util.Set;
import org.hibernate.HibernateException;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;
import org.hibernate.cfg.Configuration;
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
/**
*
* @author Deepan
*/
public class TestComp {
private static SessionFactory factory;
public static void main(String args[]){
try{
factory = new Configuration().configure().buildSessionFactory();
}catch (Throwable ex) {
System.err.println("Failed to create sessionFactory object." + ex);
System.out.println("Error");
throw new ExceptionInInitializerError(ex);
}
TestComp ME = new TestComp();
Order od=new Order();
Set<Item> items=new HashSet<Item>();
Item i1=new Item(1, 5, od);
items.add(i1);
Item i2=new Item(2, 20, od);
items.add(i2);
Item i3=new Item(3, 15, od);
items.add(i3);
od.setOrder(200);
od.setcName("Deepan");
od.setItems(items);
/* Add few employee records in database */
Integer car1 = ME.addOrder(od);
System.out.println(od.getOrder()+" : "+od.getcName()+" : "+od.getItems());
//Integer car2 = ME.addOrder(6, "Benz", "gtr");
//Integer car3 = ME.addOrder(7, "Audi", "sdf");
}
public Integer addOrder(Order order) {
Session session = factory.openSession();
Transaction tx = null;
Integer carVIN = null;
try {
tx = session.beginTransaction();
carVIN = (Integer) session.save(order);
tx.commit();
} catch (HibernateException e) {
if (tx != null) {
tx.rollback();
}
e.printStackTrace();
} finally {
session.close();
}
return carVIN;
}
}
有什么问题?如果您需要在评论中提及的任何其他信息。由于我对冬眠很新,请给我一个详细的答案。提前谢谢。
答案 0 :(得分:1)
将i1,i2,i3作为参数输入方法。然后在carVIN =(Integer)session.save(order)之后添加以下代码;
session.save(i1);
session.save(i2);
session.save(i3);
希望有所帮助