-->
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.  [ 6 posts ] 
Author Message
 Post subject: Custom ID Generator
PostPosted: Wed Oct 15, 2003 11:28 am 
Newbie

Joined: Sat Oct 11, 2003 1:47 pm
Posts: 17
I have a table that generates IDs for various domain objects in my application. So my T_COUNTER table looks like

Employee 4
Project 10
.....

The problem here is that i have to use an "assigned" generator for all my domain objects(Employee, Project etc.). If i have an association between Employee and Project and want Project to be persisted when an Employee is updated, i do something like

//Get employee
Employee emp = loadEmployee();
Set projects = new HashSet();
projects.add(project1); //Project1 is not persisted yet
projects.add(project2); //Project2 is not persisted yet

saveOrUpdate(employee);

This causes a SQL Exception saying "Row not found for project", which is true as i want the projects to be inserted only at this time. I looked at the Hibernate code and the isUnsavedValue(Serializable id) returns false as my id value is "assigned" and changes every time(Which means i cant set a unsaved-value parameter).
So my solution is to persist every single project before i do a saveorupdate on the employee.

I can get around this if i create my custom generator that looks up on my COUNTER table and generates the next number rather than generating a sequence. Is this a good approach?

Let me know if this is not clear.

Thanks
Raj


Top
 Profile  
 
 Post subject:
PostPosted: Wed Oct 15, 2003 1:55 pm 
Hibernate Team
Hibernate Team

Joined: Tue Aug 26, 2003 3:00 pm
Posts: 1816
Location: Austin, TX
The custom id generator will probably be the best approach.

Another option maybe to implement the Interceptor.isUnsaved method in a custom Interceptor. There you could attempt to ascertain whether the entity's id constitutes an already saved value.

I would definitely recommend the generator strategy, however.


Top
 Profile  
 
 Post subject:
PostPosted: Wed Oct 15, 2003 7:12 pm 
Newbie

Joined: Sat Oct 11, 2003 1:47 pm
Posts: 17
I did the Custom ID generator and now it works like a charm.
Can this be submitted in the Wiki? I think this would be a useful thing as it follows the "Sequence Blocks" patten used in "EJB Design Patterns".
My generator goes like this
<generator class="....">
<param name="table">T_COUNTER</param> <!-- Table that holds all entities -->
<param name="key">Employee</param> <!-- Domain Entity who's primary key should be incremented -->
</generator>

I should admit that ive never used an API thats so easy to use and extend and make it work the first time ;-) Great job guys.

-Raj


Top
 Profile  
 
 Post subject:
PostPosted: Wed Oct 15, 2003 7:31 pm 
Hibernate Team
Hibernate Team

Joined: Tue Aug 26, 2003 12:50 pm
Posts: 5130
Location: Melbourne, Australia
You can submit anything you like to the Wiki. Thats the idea!


Top
 Profile  
 
 Post subject:
PostPosted: Thu Oct 16, 2003 3:24 pm 
Beginner
Beginner

Joined: Thu Oct 09, 2003 3:42 pm
Posts: 22
Quote:
I would definitely recommend the generator strategy, however


Out of curiousity what is it about the interceptor strategy that makes you lean so strongly towards using a custom generator.

IMHO I think that hibernates use of a specific id value to determine object state information is pretty darn hokey.

-marc


Top
 Profile  
 
 Post subject:
PostPosted: Thu Oct 16, 2003 4:15 pm 
Hibernate Team
Hibernate Team

Joined: Tue Aug 26, 2003 3:00 pm
Posts: 1816
Location: Austin, TX
Quote:
what is it about the interceptor strategy that makes you lean so strongly towards using a custom generator


Just the fact that the explicit use of assigned ids causes certain problems with cascading that have to be rectified by either:
1) assuming everything is an insert or everything is an update, and then deal with convergences from that assumption explicilty;
2) writing possibly complicated code merely to determine whether a given instance represents a previously saved state or not.
Especially when you consider that id-generation does not have this limitation and that a custom id-generator can easily be written to perform whatever id-doling your app was previously doing.


Quote:
use of a specific id value to determine object state information is pretty darn hokey


Pray tell how you'd go about it in a framework allowing detached instances?


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