-->
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.  [ 17 posts ]  Go to page 1, 2  Next
Author Message
 Post subject: Inserting data with a Set
PostPosted: Tue Jul 11, 2006 10:04 am 
Newbie

Joined: Tue Jul 11, 2006 9:47 am
Posts: 19
Hi

I am having some troubles with inserting data into a database using a set.

I have two java objects...

The two java objects look as follows for example

class Part
int id;
String name;
getters and setterrs
......
I have to contructors, one empty one and one which accepts a string and then sets the name to the string passed.



class Car
int id;
String name;
Set<Parts> car_parts;
getters and setters...


My hibernate mapping looks something like this

<class name="Car" table="Cars">
<id column="ID" name="ID">
<generator class="increment"/>
</id>
<property name="recStatus" column="recStatus" not-null="false"/>
<set name="prefixs" lazy="false" inverse="true" >
<key>
<column name="CarRef" />
</key>
<one-to-many class="Part"/>
</set>
</class>

<class name="Part" table="CarParts">
<id column="ID" name="ID">
<generator class="increment"/>
</id>
<property name="name" column="Description"/>
</class>


I am using Spring with my Hibernate so I extend HibernateDaoSupport

Now when I add something to the database like this

Car car = new Car();
car.setName("New Car");
Set parts = new HashSet();
parts.add(new Part("New Part"));
parts.add(new Part("Another Part"));
car.setParts(parts);

myDAO.save(car);

........................................................................................

That adds the Car "New Car" to the database correctly however it doesn't go and add the parts in the HashSet to the parts table for that specific Car

Please could someone explain to me if thats even possible and if so what am I doing wrong?

Thanks
MrJones


Top
 Profile  
 
 Post subject:
PostPosted: Tue Jul 11, 2006 10:31 am 
Expert
Expert

Joined: Fri Aug 19, 2005 2:11 pm
Posts: 628
Location: Cincinnati
in the definition of your set of car parts, you need to tell hibernate to cascade saves to its children.

I don't know your exact business requirements, but for now cascade="save-update" should work. This will persist saves and updates to the children, but not delete. Look up the cascade attribute for collections to find the other options

_________________
Chris

If you were at work doing this voluntarily, imagine what you'd want to see to answer a question.


Top
 Profile  
 
 Post subject:
PostPosted: Wed Jul 12, 2006 2:55 am 
Newbie

Joined: Tue Jul 11, 2006 9:47 am
Posts: 19
Thanks for the reply it seems it worked. I need to change my Set datatype in Java to a List so how would that affect my mapping because I dont want to use that ordered_index. I dont want that extra column in my database as I dont feel its needed.

Thanks
MrJones


Top
 Profile  
 
 Post subject:
PostPosted: Wed Jul 12, 2006 9:18 am 
Expert
Expert

Joined: Fri Aug 19, 2005 2:11 pm
Posts: 628
Location: Cincinnati
you can use a bag, which is an unordered list.

doesn't need an index column

_________________
Chris

If you were at work doing this voluntarily, imagine what you'd want to see to answer a question.


Top
 Profile  
 
 Post subject:
PostPosted: Wed Jul 12, 2006 9:40 am 
Newbie

Joined: Tue Jul 11, 2006 9:47 am
Posts: 19
Thanks that helped but now if I use cascade="save-update" it saves the values in the List which is correct and how I want it, but when I change something it sets my reference column value to null and leaves the exsiting values in my table which I dont want as that table will grow to one HUGE table?


Top
 Profile  
 
 Post subject:
PostPosted: Wed Jul 12, 2006 9:48 am 
Expert
Expert

Joined: Fri Aug 19, 2005 2:11 pm
Posts: 628
Location: Cincinnati
cascade="all-delete-orphan" will delete orphaned children if their parent is deleted. It will also cascade saves, updates, and deletes to the children.

if you are updating just a value and it is setting the reference keys, we have another problem

_________________
Chris

If you were at work doing this voluntarily, imagine what you'd want to see to answer a question.


Top
 Profile  
 
 Post subject:
PostPosted: Wed Jul 12, 2006 10:36 am 
Newbie

Joined: Tue Jul 11, 2006 9:47 am
Posts: 19
What its doing, if I update the parent, like change the name then the List thats associated with that parent gets recreated in the database and its old reference key is set to null.
So everytime I make a change the entire list is resaved and the old one reference keys are simply set to null I would prefer them to just be updated or perhaps delete the null values? In my mapping I set my key to not null like this..
.................
key>
<column name="prefixNameRef" not-null="false"/>
</key>
..................

if not-null="true" an exception is thrown...

that still doesn't do the trick though as this is my database at the moment

id prefix prefixnameRef
-----------------------
1 082 (null)
2 083 2
3 082 (null)
4 072 (null)
5 082 (null)
6 072 (null)
7 082 1
8 072 1


Top
 Profile  
 
 Post subject:
PostPosted: Wed Jul 12, 2006 10:52 am 
Expert
Expert

Joined: Fri Aug 19, 2005 2:11 pm
Posts: 628
Location: Cincinnati
change the cascade="all-delete-orphan"


Secondly, are you updating the primary key of the parent object? Or updating the field that the child objects have their reference key pointing to?

_________________
Chris

If you were at work doing this voluntarily, imagine what you'd want to see to answer a question.


Top
 Profile  
 
 Post subject:
PostPosted: Wed Jul 12, 2006 11:05 am 
Newbie

Joined: Tue Jul 11, 2006 9:47 am
Posts: 19
I am calling the saveOrUpdate on the Primary Key of the Parent object and the parent object stays the same...
The parent object also has a List of the child objects which is how it saves the children objects, here is my mapping...

<class PrefixName
....
<bag name="prefixs" lazy="false" cascade="delete-all-orphan">
<key>
<column name="prefixNameRef" not-null="false"/>
</key>
<one-to-many class="Prefix" />
</bag>
......
</class>


Top
 Profile  
 
 Post subject:
PostPosted: Wed Jul 12, 2006 11:35 am 
Expert
Expert

Joined: Fri Aug 19, 2005 2:11 pm
Posts: 628
Location: Cincinnati
no, I mean what value. are you updating one of hte PK attributes?

_________________
Chris

If you were at work doing this voluntarily, imagine what you'd want to see to answer a question.


Top
 Profile  
 
 Post subject:
PostPosted: Wed Jul 12, 2006 11:46 am 
Newbie

Joined: Tue Jul 11, 2006 9:47 am
Posts: 19
If I change the name on the parent object it resaves the list associated with it and then sets the old values to null, if I add a object to the list and resave the parent object (seeing I altered the list) then it has the same affect


Top
 Profile  
 
 Post subject:
PostPosted: Wed Jul 12, 2006 1:42 pm 
Expert
Expert

Joined: Fri Aug 19, 2005 2:11 pm
Posts: 628
Location: Cincinnati
A couple of things could be happening.

1. I never liked saveOrUpdate. Try just update.

2. If you are changing a primary key value of the parent, hibernate doesn't recognize the object, and will reinsert all the objects into the collection.

3. Is your association bi-directional? If so, are you sure you are setting and/or not clearing out the association on the children side when you saving/loading/updating it?

post the code where it errs, and the mapping files if that didn't help

_________________
Chris

If you were at work doing this voluntarily, imagine what you'd want to see to answer a question.


Top
 Profile  
 
 Post subject:
PostPosted: Thu Jul 13, 2006 2:05 am 
Newbie

Joined: Tue Jul 11, 2006 9:47 am
Posts: 19
My first mapping looks as follows...

<class name="PrefixName" table="costPrefixNames">
<id column="ID" name="ID">
<generator class="increment"/>
</id>
<bag name="prefixs" lazy="false" cascade="delete-all-orphan">
<key>
<column name="prefixNameRef" not-null="false"/>
</key>
<one-to-many class="Prefix" />
</bag>
<property name="name" column="Name" length="50" not-null="true"/>
<property name="recStatus" column="recStatus" not-null="false"/>
</class>

Second mapping ..............................

<class name="Prefix" table="costPrefix">
<id column="ID" name="ID">
<generator class="increment"/>
</id>
<property name="name" column="costPrefix"/>
</class>

Then in my PrefixName class I have a List<Prefix> prefixs which I set and get the "children" for that particular object.
I dont change the primary key value on the parent object...

When I make a change to either the List or to the actualy parent object like changing the name I call the saveOrUpdate using Springs HibernateDaoSupport, getHibernateTemplate().

Any other code you might need?


Top
 Profile  
 
 Post subject:
PostPosted: Thu Jul 13, 2006 5:58 am 
Newbie

Joined: Tue Jul 11, 2006 9:47 am
Posts: 19
Thanks I seem to have sorted this issue out now. My mapping looks as follows which solved some issues but created new ones

<class name="PrefixName" table="costPrefixNames">
<id column="ID" name="ID">
<generator class="increment"/>
</id>
<set name="prefixs" lazy="false" inverse="true" cascade="all-delete-orphan">
<key>
<column name="prefixNameRef"/>
</key>
<one-to-many class="Prefix" />
</set>
<property name="name" column="Name" length="50" not-null="true"/>
<property name="recStatus" column="recStatus" not-null="false"/>

and my other class mapping looks like this

<class name="Prefix" table="costPrefix">
<id column="ID" name="ID">
<generator class="increment"/>
</id>
<property name="name" column="Prefix"/>
<many-to-one name="prefixName" column="prefixNameRef" class="PrefixName" not-null="true"/>
</class>

Now when I add elements to the set of prefixs it saves it to the database correctly howeven if I remove an element later on and call the setPrefixs method for the parent object (PrefixName) with a new set containing different elements it still keeps my OLD elements in the database with the reference to the prefixnameRef, I'd like those old values deleted or should I say replaced with the new set? Any suggestions? I should add that I am using saveOrUpdate and I have tried the update only but no luck with that either.


Top
 Profile  
 
 Post subject:
PostPosted: Thu Jul 13, 2006 9:07 am 
Expert
Expert

Joined: Tue Apr 25, 2006 12:04 pm
Posts: 260
Code:
PrefixName prefixName = ( PrefixName ) session.get( PrefixName.class, "identifer" );
// assuming loaded prefixName already contains 3 prefixes
// first delete one child prefix
// then add two new prefixes
// after this operation, you will have four child prefix'es for a prefixName
Prefix deletePrefix = new Prefix();
// populate primary key to be deleted
if ( prefixName.getPrefixs().contains( deletePrefix ) ) {
   prefixName.remove( deletePrefix );
   deletePrefix.setPrefixName( null );
}

// now add two new child prefix'es
// populate required information in getPrefix() method
Prefix insertPrefixOne = getPrefix();
Prefix insertPrefixTwo = getPrefix();

prefixName.getPrefixs().add( insertPrefixOne );
prefixName.getPrefixs().add( insertPrefixTwo );

// with 'delete-all-orphan' cascade option
// below call with issue one delete and two inserts on Prefix
session.saveOrUpdate( prefixName );


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 17 posts ]  Go to page 1, 2  Next

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.