-->
These old forums are deprecated now and set to read-only. We are waiting for you on our new forums!
More modern, Discourse-based and with GitHub/Google/Twitter authentication built-in.

All times are UTC - 5 hours [ DST ]



Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 5 posts ] 
Author Message
 Post subject: Insert sequence reversely
PostPosted: Mon Nov 22, 2004 5:03 am 
Newbie

Joined: Mon Nov 22, 2004 3:23 am
Posts: 3
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 (?, ?, ?, ?, ?, ?, ?, ?)


Top
 Profile  
 
 Post subject:
PostPosted: Mon Nov 22, 2004 5:49 am 
Regular
Regular

Joined: Fri Nov 12, 2004 12:07 am
Posts: 57
Location: Beijing,China
Which concrete implemented class you are using now?I propose that you use LinkedHashSet in your Parent class.


Top
 Profile  
 
 Post subject:
PostPosted: Mon Nov 22, 2004 5:59 am 
Newbie

Joined: Mon Nov 22, 2004 3:23 am
Posts: 3
hi amjn
Actually I use HashSet in Poheader(parent).

The code looks like following:
public class Poheader implements Serializable
{
private Set poitems = new HashSet();

/**
* Simple constructor of Poheader instances.
*/
public Poheader()
{
}

..................... omit some code
......................
public void setPoitems(Set poitems){
this.poitems = poitems;
}

public Set getPoitems(){
return poitems;
}

public void add(Poitem poitem){
poitem.setPoheader(this);
poitems.add(poitem);
}

}


Top
 Profile  
 
 Post subject:
PostPosted: Mon Nov 22, 2004 9:25 am 
Regular
Regular

Joined: Fri Nov 12, 2004 12:07 am
Posts: 57
Location: Beijing,China
HashSet is out-of-order,and with a backing HashMap structure.
LinkedHashSet always keeps up insertion order.
TreeSet arranges objects by compareTo() of contained objects.

So you should select LinkedHashSet.


Top
 Profile  
 
 Post subject:
PostPosted: Mon Nov 22, 2004 9:43 pm 
Newbie

Joined: Mon Nov 22, 2004 3:23 am
Posts: 3
Hi amjn
Thank you for your help. I've solved problem after I use LinkedHashSet.


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 5 posts ] 

All times are UTC - 5 hours [ DST ]


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum

Search for:
© Copyright 2014, Red Hat Inc. All rights reserved. JBoss and Hibernate are registered trademarks and servicemarks of Red Hat, Inc.