问题描述
我正在尝试在Hibernate黑白客户(一个)和订单(许多)的一对多关系中插入记录。但是在插入记录之后,数据库中的Customer表正在更新记录,但是 订单表记录将被覆盖。 也就是说,当我插入2个客户(每个客户有2个订单)时,customer表包含所有客户记录,但是订单表中的记录被覆盖。这是一个特殊的问题 请协助我解决问题。 预先感谢。
Customer.java
public class Customer{
private int customer_id;
private String name;
private long phone;
private String email;
private Set<Orders> orderSet;
//getters and setters
}
Orders.java
public class Orders implements Serializable
{
private int order_id;
private String orderDate;
private int cid;
}
hibernate.cfg.xml
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE hibernate-mapping PUBLIC"-//Hibernate/Hibernate Mapping DTD//EN""http://www.hibernate.org/dtd/hibernate-mapping-3.0.dtd">
<hibernate-mapping>
<class name="entity.Customer">
<id name="customer_id" type="int">
<column name="customer_id" not-null="true"/>
<!-- Gets the max customer id from db and increment while inserting -->
<generator class="increment"/>
</id>
<property name="name" type="java.lang.String" column="customer_name" length="70"/>
<property name="phone" type="long" column="customer_no"/>
<property name="email" type="java.lang.String" column="email" length="40" />
<set name="orderSet" cascade="all" inverse="true">
<key column="customer_id"/>
<one-to-many class="entity.Orders"/>
</set>
</class>
</hibernate-mapping>
Customer.hbm.xml
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE hibernate-mapping PUBLIC"-//Hibernate/Hibernate Mapping DTD//EN" "http://www.hibernate.org/dtd/hibernate-mapping-3.0.dtd">
<hibernate-mapping>
<class name="entity.Customer">
<id name="customer_id" type="int">
<column name="customer_id" not-null="true"/>
<!-- Gets the max customer id from db and increment while inserting -->
<generator class="increment"/>
</id>
<property name="name" type="java.lang.String" column="customer_name" length="70"/>
<property name="phone" type="long" column="customer_no"/>
<property name="email" type="java.lang.String" column="email" length="40" />
<set name="orderSet" cascade="all" inverse="true">
<key column="customer_id"/>
<one-to-many class="entity.Orders"/>
</set>
</class>
</hibernate-mapping>
Orders.hbm.xml
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE hibernate-mapping PUBLIC"-//Hibernate/Hibernate Mapping DTD//EN""http://www.hibernate.org/dtd/hibernate-mapping-3.0.dtd">
<hibernate-mapping>
<class name="entity.Orders">
<id name="order_id" type="int">
<column name="order_id"/>
</id>
<property name="orderDate" column="orderDate" type="java.lang.String" length="20"/>
<property name="cid" column="customer_id" />
</class>
</hibernate-mapping>
Client.java
public class ClientImpl {
@SuppressWarnings("unused")
public static void main(String[] args) {
BusinessImpl business=new BusinessImpl();
Customer c= new Customer();
c.setName("FirstCustomer");
c.setPhone(1324567890l);
c.setEmail("hibernate1_2_many@abin.com");
HashSet<Orders> orderHSet=new HashSet<Orders>();
Orders o1,o2;
o1=new Orders(1,"C1O1_date");
o2=new Orders(2,"C1O2_date");
orderHSet.add(o1);
orderHSet.add(o2);
c.setOrderSet(orderHSet);
business.insertCustomer(c);
Customer c2= new Customer();
c2.setName("SecondCustomer");
c2.setPhone(1324567890l);
c2.setEmail("hibernate1_2_many@abin.com");
HashSet<Orders> orderHSet2=new HashSet<Orders>();
Orders o21=new Orders(1,"C2O1_date");
Orders o22=new Orders(2,"C2O2_date");
orderHSet2.add(o21);
orderHSet2.add(o22);
c2.setOrderSet(orderHSet2);
business.insertCustomer(c2);
}
}
BusinessImpl.java
public class BusinessImpl{
static DaoImpl dao=new DaoImpl();
public void insertCustomer(Customer c) {
dao.createCustomerRecord(c);
}
}
DaoImpl.java
public class DaoImpl implements DaoInterface {
public void createCustomerRecord(Customer c) {
Session session= UtilImplement.getSessionFactory().getCurrentSession();
session.beginTransaction();
session.save(c);
session.flush();
session.clear();
}
}
解决方法
暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!
如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。
小编邮箱:dio#foxmail.com (将#修改为@)