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.  [ 8 posts ] 
Author Message
 Post subject: need mapping file help
PostPosted: Sun Jun 10, 2007 2:32 pm 
Newbie

Joined: Sat Nov 25, 2006 6:57 am
Posts: 12
Location: india
Hibernate version: 3.2

Mapping documents:
UserInfo mapping
<hibernate-mapping>
<class name="com.beans.Userinfo" table="userinfo">
<comment></comment>
<id name="userid">
<column name="userid" />
<generator class="assigned" />
</id>
.
.
.
</class>
</hibernate-mapping>

TeamInfo mapping
<class name="com.Teaminfo" table="teaminfo">
<comment></comment>
<id name="teamid">
<column name="teamid" />
<generator class="assigned" />
</id>

<many-to-one name="userinfo" unique="true" column="userid" not-null="true"/>
<set name="rosterinfos" inverse="true">
<key>
<column name="teamid" not-null="true">
<comment></comment>
</column>
</key>
<one-to-many class="com.beans.Rosterinfo" />
</set>
</class>

RosterInfo mapping.
<class name="com.beans.Rosterinfo" table="rosterinfo">
<comment></comment>
<id name="rosterid">
<column name="rosterid" />
<generator class="assigned" />
</id>
</class>


I have a generated DAO for each entity UserInfo, TeamInfo, RosterInfo.

It follows below relationship
UserInfo 1-1 TeamInfo
TeamInfo 1-* RosterInfo

Here i wanted to populate only TeamInfo object and call the persist method which will internally save the corresponding record in UserInfo table and RosterInfo table.

How to achieve this, will there be any change in the mapping files too..

Thanks in advance for the help...

Naresh


Last edited by nareshpillai on Mon Jun 11, 2007 2:04 pm, edited 1 time in total.

Top
 Profile  
 
 Post subject:
PostPosted: Mon Jun 11, 2007 12:09 am 
Senior
Senior

Joined: Sat Aug 19, 2006 6:31 pm
Posts: 139
You want to use "cascade".

<many-to-one name="userinfo" unique="true" column="userid" not-null="true" cascade="persist"/>

<set name="rosterinfos" inverse="true" cascade="persist">

Here's more info about cascading.
http://www.hibernate.org/hib_docs/v3/re ... transitive

_________________
Don't forget to rate the reply if it helps..:)

Budyanto


Top
 Profile  
 
 Post subject:
PostPosted: Wed Jun 13, 2007 1:49 am 
Newbie

Joined: Sat Nov 25, 2006 6:57 am
Posts: 12
Location: india
Thanks for the reply..

I merged first 2 tables to one. Now i made only TeamInfo and RosterInfo.

TeamInfo 1...*RosterInfo.

Here is the persistence code...
TeamInfo team = new TeamInfo();
team.setName("test");
.
.
.
ArrayList list = new ArrayList();
list.add(new Roster("firstname".......));

team.setRoster(list);
session.save(team);;

Below is the mapping file

TeamInfo mapping files as

<bag name="rosters" inverse="true" cascade="all">
<key column="teamid"/>
<one-to-many class="com.beans.Rosterinfo"/>
</bag>

RosterInfo as normal properties no reference to teamInfo..

When i save using persistence code above...

Caused by: java.sql.BatchUpdateException: General error message from server: "Field 'teamid' doesn't have a default value"..

If i use cascade="persist", it save only the teaminfo not the rosterinfo...

Let me know what is wrong over here..
Your help will be appreciated.

Thanks,
Naresh


Top
 Profile  
 
 Post subject:
PostPosted: Wed Jun 13, 2007 2:10 am 
Newbie

Joined: Sat Nov 25, 2006 6:57 am
Posts: 12
Location: india
Just to update one more thing over here.
If i update my table rosterinfo (allow null value for teamid - which is a foreign key to teamInfo table)
Then it works fine.
But, here 2 insert is called and 3rd an update is called on rosterinfo to update the teamid.

How to solve this?

Thanks in advance,
Naresh


Top
 Profile  
 
 Post subject:
PostPosted: Fri Jun 15, 2007 12:35 am 
Senior
Senior

Joined: Sat Aug 19, 2006 6:31 pm
Posts: 139
If you use cascade="persist" it's only going to save team because you use session.save(). You need to cascade on "save-update".

The id problem is because your generator class was set to assigned. That means you have to set the id manually before calling save.

Try setting it to "increment" or "uuid". It'll be auto-generated.

http://www.hibernate.org/hib_docs/v3/re ... -generator

_________________
Don't forget to rate the reply if it helps..:)

Budyanto


Top
 Profile  
 
 Post subject:
PostPosted: Sat Jun 16, 2007 5:09 pm 
Newbie

Joined: Sat Nov 25, 2006 6:57 am
Posts: 12
Location: india
I had already changed the id generate to increment. But it is not working with that. Still i can see the additional update statement fired against the database.

Let me know if anything else i need to change..

Thanks,
Naresh


Top
 Profile  
 
 Post subject: Hi
PostPosted: Sat Jun 16, 2007 5:26 pm 
Newbie

Joined: Tue Jun 12, 2007 6:44 am
Posts: 13
Location: Brazil
Hi naresh

I had been trying to use mapping files for a while. Then I tried annotations.
Much easier.

Just download hibernate annotations and hibernate core then add to your classpath

bye


Top
 Profile  
 
 Post subject:
PostPosted: Sun Jun 17, 2007 1:31 am 
Newbie

Joined: Sat Nov 25, 2006 6:57 am
Posts: 12
Location: india
Thanks for the information....I will try to go through hibernate annotation..
But i need to get it working using the current logic...

Here, currently i have

Single Team object ---> One or more Roster object

Now first time saving, works properly one team object is inserted in Team table and roster record gets inserted into roster table...

But if i want to modify the roster object... then how to go with it...

Say i have object Team and it has collection of Roster object due one-to-many relationship. So when the form is submitted with the update. I will be populating the Team object with modified team information and also update the collection with the updated roster info...
So if i call saveorupdate with Team object. Here, if i remove any roster info, it should delete from roster table, or modify some roster info, it should get updated in the roster table as well if i add more roster to the collection new record should be inserted into the roster table for the corresponding team....

Here i am using cascade="all,delete-orphan". But if i remove any roster from the collection. the record still remains in the roster table, but the teamid is made null...

How to get it working....

Thanks in advance..
Naresh


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