-->
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.  [ 10 posts ] 
Author Message
 Post subject: Overriding the generator class tag in a mapping file
PostPosted: Mon Feb 11, 2008 11:59 am 
Newbie

Joined: Mon Feb 11, 2008 11:43 am
Posts: 5
I am currently trying to write an interface to a system using existing hbm.xml files. We are using Hibernate 2.1 currently trying to transition to Hibernate 3. Below is an example of one of the hbm files. The problem is that the programmer had set a Java Class for the generator tag and every time i try to insert a new row into this table the Java class is overwriting the data that i am entering in for the column (which happens to be the PK for the table). Is there a way to override the class that is specified for the generator class tag?

<?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>
<class
name="com.dsicdiiti.tnt.datasource.BookingData"
table="BOOKING"
dynamic-update="true"
>

<id
name="sysID"
type="java.lang.Long"
column="SYSID"
>
<generator class="com.dsicdiiti.tnt.common.util.HibernateGuidBridge" />
</id>


-java code in the EJB for inserting the new row into the BOOKING table

sess = HibernateSession.getSession(dataSource, false);
BookingData inmate = new BookingData();
if (booking!=null) {
inmate.setSysID(booking.getSysID());
...
...
sess.save(inmate);
sess.flush();


Top
 Profile  
 
 Post subject:
PostPosted: Mon Feb 11, 2008 7:19 pm 
Regular
Regular

Joined: Wed Aug 15, 2007 7:37 am
Posts: 73
When you call Save with a generated ID hibernate will always generate one.

If you set the generator class to 'assigned' you'll be forced to assign an identifier to the object.

However, if BookingData.Id is equal to something on Booking though I'd suggest checking out the 'foreign' generator and making sure BookingData objects are appropriately connected to Bookings.


Top
 Profile  
 
 Post subject:
PostPosted: Tue Feb 12, 2008 12:06 am 
Newbie

Joined: Mon Feb 11, 2008 11:43 am
Posts: 5
Here's the kicker i can't change the existing hbm.xml files because it is a system that is currently live in many sites. So going from a guid generator class in the hbm file to the "assigned" type in the generator class tag isn't going to happen any time soon. I wasn't sure if this was something that could be done in hibernate 3 or if there is a current work around via 2. 1


Top
 Profile  
 
 Post subject:
PostPosted: Tue Feb 12, 2008 11:43 am 
Regular
Regular

Joined: Wed Aug 15, 2007 7:37 am
Posts: 73
Ah, I see. There is nothing built into Hibernate as far as generators go ([url]http://forum.hibernate.org/viewtopic.php?t=983325&highlight=generator[/url]], but have you tried calling Save(object, id) ? That should override any generated id.


Top
 Profile  
 
 Post subject:
PostPosted: Tue Feb 12, 2008 5:25 pm 
Newbie

Joined: Mon Feb 11, 2008 11:43 am
Posts: 5
Thank you very much for the info. That has saved me from keeping dual hbm files for the same tables in so many places. Everything is working great now.


Top
 Profile  
 
 Post subject: Re: Overriding the generator class tag in a mapping file
PostPosted: Wed May 27, 2009 5:09 pm 
Beginner
Beginner

Joined: Thu May 18, 2006 2:58 pm
Posts: 28
Did this work?

I see the method declaration in the documentation is different from what is described in this post:
Code:
Serializable save(String entityName, Object object)
          Persist the given transient instance, first assigning a generated identifier.


Is the entityName the property of the id?
Will it use the value of that property in object?

I'm trying to do exactly the same thing, provide a primary key instead of having the generated one saved (in this case generated from a trigger).

Can the generator be overridden?

Thanks.


Top
 Profile  
 
 Post subject: Re: Overriding the generator class tag in a mapping file
PostPosted: Thu May 28, 2009 9:46 am 
Newbie

Joined: Mon Feb 11, 2008 11:43 am
Posts: 5
Yes, this did work out great for me and is currently running live on few sites. Are you still having problems with this?


Top
 Profile  
 
 Post subject: Re: Overriding the generator class tag in a mapping file
PostPosted: Thu May 28, 2009 2:20 pm 
Beginner
Beginner

Joined: Thu May 18, 2006 2:58 pm
Posts: 28
Yes. I'm still having trouble.

I see the save method in this post is has the declaration below:
Code:
Save(object, id)


The method declration for the current api (https://www.hibernate.org/hib_docs/v3/api/index.html) is:
Code:
Serializable save(String entityName, Object object)


Is this the same method? Do you know how to make this (save(String entityName, Object object)) work?

Thanks.


Top
 Profile  
 
 Post subject: Re: Overriding the generator class tag in a mapping file
PostPosted: Thu May 28, 2009 2:53 pm 
Newbie

Joined: Mon Feb 11, 2008 11:43 am
Posts: 5
Ok, if you look at the example code that i had in the first post. Instead of the

sess.save(inmate);
sess.flush();

I have:

sess.save(inmate, booking.getSysID());
sess.flush();

The booking.getSysID() call is just returning a Long data type and when I look at the database it has the Long that i assigned to it rather than the one that would have been created by the Generator class tag. I missed any reference to this when i looked at the documentation but SteveMc helped me out big when he gave me the suggestion.


Top
 Profile  
 
 Post subject: Re: Overriding the generator class tag in a mapping file
PostPosted: Thu May 28, 2009 5:10 pm 
Beginner
Beginner

Joined: Thu May 18, 2006 2:58 pm
Posts: 28
Thanks for your help. I see the issue.

The method that you are describing has been Deprecated. You must be implementing org.hibernate.classic.Session for your code to work.

See http://www.hibernate.org/250.html - Hibernate3 Migration Guide

The latest version is in org.hibernate.Session - which has the method declaration I am implementing.

Apparently, when all the fields are filled in, use session.replicate to insert the values.

See here for details.
https://forum.hibernate.org/viewtopic.php?f=1&t=959631&start=0

and here for more information. https://forum.hibernate.org/viewtopic.php?f=1&t=957045&view=previous


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