Hi all
I met a strange question. I have two tables poheader and poitem. Relationship is one-to-many. I have maintained bidirectional relationship and do save them to my mysql database.
Insert is OK. But when I open the database, I found the insert sequence is reverse. For example, there are two items 10 and 20, I want hibernate do insert 10 first then to insert 20. But the actual situation is reverse, hibernate insert 20 firstly. I am sure I add items orderly.
Could anyone tell me what had happend?
Hibernate version:2.1.3
Database MySQL version: 4.0.18
Operate System: WindowXP SP1
Develop Environment: Eclipse 3.0
Mapping documents:
<hibernate-mapping package="com.npb.hibernate">
<class name="Poheader" table="poheader">
<id name="id" column="ID" type="java.lang.Long" unsaved-value="0">
<generator class="native"/>
</id>
<property name="poNumber" column="PO_NUMBER" unique="true" type="java.lang.String" />
<property name="docDate" column="DOC_DATE" type="java.lang.String" />
<property name="docType" column="DOC_TYPE" type="java.lang.String" />
<property name="purchOrg" column="PURCH_ORG" type="java.lang.String" />
<property name="purGroup" column="PUR_GROUP" type="java.lang.String" />
<property name="vendor" column="VENDOR" type="java.lang.String" />
<set name="poitems" lazy="true" inverse="true" cascade="all" >
<key column="PO_NUMBER"/>
<one-to-many class="Poitem" />
</set>
</class>
<hibernate-mapping package="com.npb.hibernate">
<class name="Poitem" table="poitem">
<id name="id" column="ID" type="java.lang.Long" unsaved-value="0">
<generator class="native" />
</id>
<property name="poItem" column="PO_ITEM" type="java.lang.String" />
<property name="material" column="MATERIAL" type="java.lang.String" />
<property name="plant" column="PLANT" type="java.lang.String" />
<property name="storeLoc" column="STORE_LOC" type="java.lang.String" />
<property name="netPrice" column="NET_PRICE" type="java.lang.Long" />
<property name="delivDate" column="DELIV_DATE" type="java.lang.String" />
<property name="quantity" column="QUANTITY" type="java.lang.Long" />
<many-to-one name="poheader" property-ref="poNumber" column="PO_NUMBER" class="Poheader" not-null="true"/>
</class>
</hibernate-mapping>
I am working on legacy system, so I use "property-ref" that is not encouraged in Hibernate Reference.
This is code fragment about maintain parent and child:
............
public void setPoitems(Set poitems){
this.poitems = poitems;
}
public Set getPoitems(){
return poitems;
}
public void add(Poitem poitem){
poitem.setPoheader(this);
poitems.add(poitem);
}
...................
My test program:
public class test {
public static void main(String[] args) {
Poheader header = new Poheader();
header.setDocDate("20041120");
header.setDocType("NB");
header.setPoNumber("ponumber");
header.setPurchOrg("1000");
header.setPurGroup("001");
header.setVendor("1000");
Poitem item1 = new Poitem();
item1.setDelivDate("20041120");
item1.setPoItem("10");
item1.setMaterial("test1");
item1.setNetPrice(new Long(9));
item1.setPlant("1000");
item1.setQuantity(new Long(10));
item1.setStoreLoc("0001");
header.add(item1);
Poitem item2 = new Poitem();
item2.setDelivDate("20041120");
item2.setPoItem("20");
item2.setMaterial("test2");
item2.setNetPrice(new Long(39));
item2.setPlant("1000");
item2.setQuantity(new Long(20));
item2.setStoreLoc("0002");
header.add(item2);
Session session = HibernateUtil.getSession();
HibernateUtil.beginTransaction();
try{
session.save(header);
}
catch (HibernateException e){
System.out.println("HibernateException " + e.getMessage());
}
HibernateUtil.commitTransaction();
HibernateUtil.closeSession();
System.out.println("Completed!");
}
}
SQL generate by hibernate: Hibernate: insert into poheader (PO_NUMBER, DOC_DATE, DOC_TYPE, PURCH_ORG, PUR_GROUP, VENDOR) values (?, ?, ?, ?, ?, ?) Hibernate: insert into poitem (PO_ITEM, MATERIAL, PLANT, STORE_LOC, NET_PRICE, DELIV_DATE, QUANTITY, PO_NUMBER) values (?, ?, ?, ?, ?, ?, ?, ?) Hibernate: insert into poitem (PO_ITEM, MATERIAL, PLANT, STORE_LOC, NET_PRICE, DELIV_DATE, QUANTITY, PO_NUMBER) values (?, ?, ?, ?, ?, ?, ?, ?)
|