-->
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.  [ 3 posts ] 
Author Message
 Post subject: I can't insert the entire collection, just only the last one
PostPosted: Tue Aug 16, 2005 9:36 pm 
Newbie

Joined: Fri Apr 22, 2005 12:34 am
Posts: 4
Need help with Hibernate? Read this first:
http://www.hibernate.org/ForumMailingli ... AskForHelp

Hibernate version:Hibernate2.18

I have two entity (Item Bid), they are One to many relation.

Code:
<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC
"-//Hibernate/Hibernate Mapping DTD 2.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-2.0.dtd" >

<hibernate-mapping package="hibernate.study.beans">
<class name="Item" table="item">

<id name="id" type="java.lang.Integer" column="ITEM_ID">
<generator class="native"/>
</id>

<timestamp name="created" column="CREATED" />

<property name="itemName" type="java.lang.String" column="ITEM_NAME" not-null="true" length="45"/>

<set name="bids" inverse="true" lazy="true" cascade="all-delete-orphan">
<key column="ITEM_ID"/>
<one-to-many class="Bid"/>
</set>

</class>
</hibernate-mapping>



Code:

<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC
"-//Hibernate/Hibernate Mapping DTD 2.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-2.0.dtd" >

<hibernate-mapping package="hibernate.study.beans">
<class name="Bid" table="bid">

<id name="id" type="java.lang.Integer" column="BID_ID">
<generator class="native"/>
</id>


<property name="bidName" type="java.lang.String" column="BID_NAME" not-null="true" length="45"/>

<many-to-one name="item" class="Item" column="ITEM_ID" not-null="true"/>

</class>
</hibernate-mapping>


I use the hbm2class to create the java class.

Now I want to insert a list of bid to the Item which is loaded from the database.

Code:

Session session = HibernateUtil.getSession();
        HibernateUtil.beginTransaction();
        Item i = (Item) session.load(Item.class,new Integer(7));
        Bid b = new Bid();
        b.setBidName("bid");
       
        Bid b1 = new Bid();
        b.setBidName("bid1");
       
        Bid b2 = new Bid();
        b.setBidName("bid2");
       
        Bid b3 = new Bid();
        b.setBidName("bid3");
       
        b.setItem(i);
       
        b1.setItem(i);
       
        b2.setItem(i);
       
        b3.setItem(i);
       
       
        Collection<Bid> bids = new HashSet<Bid>();
        bids.add(b);
        bids.add(b1);
        bids.add(b2);
        bids.add(b3);
       
        i.getBids().addAll(bids);
       
        session.saveOrUpdate(i);
        HibernateUtil.commitTransaction();
       
       

        HibernateUtil.closeSession();


but only the b3 bid can be inserted into database.

must I insert those bids one to one?

the following is the hibernate printed sql.

Hibernate: select item0_.ITEM_ID as ITEM_ID0_, item0_.CREATED as CREATED0_, item0_.ITEM_NAME as ITEM_NAME0_ from item item0_ where item0_.ITEM_ID=?

Hibernate: select bids0_.ITEM_ID as ITEM_ID__, bids0_.BID_ID as BID_ID__, bids0_.BID_ID as BID_ID0_, bids0_.BID_NAME as BID_NAME0_, bids0_.ITEM_ID as ITEM_ID0_ from bid bids0_ where bids0_.ITEM_ID=?

Hibernate: insert into bid (BID_NAME, ITEM_ID) values (?, ?)

Hibernate: update item set CREATED=?, ITEM_NAME=? where ITEM_ID=? and CREATED=?


Top
 Profile  
 
 Post subject:
PostPosted: Tue Aug 16, 2005 9:52 pm 
Senior
Senior

Joined: Wed Jul 13, 2005 4:31 pm
Posts: 142
Location: Seattle, WA
I think this has something to do with the equals/hashcode not defined properly for bid.

http://www.hibernate.org/109.html

Since you are adding the bid, the id is null. So, they all appear to be the same object to hibernate if equals is not implemented to use something other than the id. if bidName is unique implement equals and hashcode based on bidName in the bid class, and it should work.


Top
 Profile  
 
 Post subject:
PostPosted: Tue Aug 16, 2005 10:28 pm 
Newbie

Joined: Fri Apr 22, 2005 12:34 am
Posts: 4
thank you.

the problem is resolved.

but I make a mistake.

Bid b = new Bid();
b.setBidName("bid");

Bid b1 = new Bid();
b.setBidName("bid1"); // b1

Bid b2 = new Bid();
b.setBidName("bid2"); //b2

Bid b3 = new Bid();
b.setBidName("bid3"); //b3

b.setItem(i);

b1.setItem(i);

b2.setItem(i);

b3.setItem(i);

so the fist one should be inserted into database, not the last one.


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 3 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.