-->
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: confusion about the need and use of assigned id's...
PostPosted: Mon Aug 28, 2006 3:54 am 
Beginner
Beginner

Joined: Thu Aug 03, 2006 3:43 am
Posts: 45
Hibernate version: 3.1.2

Hello,

This is more of a general question that I can't seem to find an answer for anywhere: can someone explain to me the whole deal with ID generation ?... Let me elaborate on that... I worked with databases before so I know what an ID is and I know the issues with java collections and changing objects... So to be more specific what I don't really understand is:

- is it really that necessary in every day work to have the ID generated before saving ? do I really need to add an entity to any collections before saving it (as a hibernate constraint or something like that)?

- if this issue is so important how come there's so little stuff about it on the forum ? i mean there are allot of threads about sequences or making composite ID generators (or something like that).. but I don't have support in MySQL for sequences (so the only choice I see is emulating that in some way) and so far I haven't found an example that's not based sequences and that is able to generate ID's before saving...

- and finally I might be too much of a noob, and I want to apologies for that, so if this discussion doesn't go anywhere, can someone point me in the right direction so that I can add ID generation before saving(for a mySQL database ) to my project ?

thank you for your time...


Last edited by sorin7486 on Mon Aug 28, 2006 5:52 am, edited 1 time in total.

Top
 Profile  
 
 Post subject: Re: ID's generation and MySQL database
PostPosted: Mon Aug 28, 2006 4:10 am 
Hibernate Team
Hibernate Team

Joined: Tue Aug 26, 2003 6:10 am
Posts: 8615
Location: Neuchatel, Switzerland (Danish)
sorin7486 wrote:
- is it really that necessary in every day work to have the ID generated before saving ?


when do you want it generated then ?
After insertion ? then you need "identity" or "select" id generator because an id is needed at some point; right ? :)

Quote:
do I really need to add an entity to any collections before saving it (as a hibernate constraint or something like that)?


this has nothing to do with id generation, at least not upfront. it is important to keep your java model consistent with what you have mapped it. so if it is a bidirectional relationship then you need to have it that way. if it is *not* a bidirectional relationship you don't need to put in a collection.

Quote:
- if this issue is so important how come there's so little stuff about it on the forum ?


because it is well documented in the docs and it normally makes sense to have an id for persistent objects ? :)

Quote:
i mean there are allot of threads about sequences or making composite ID generators (or something like that).. but I don't have support in MySQL for sequences (so the only choice I see is emulating that in some way) and so far I haven't found an example that's not based sequences and that is able to generate ID's before saving...


did you read the docs ? did you try other id generators than sequence ? e.g. hilo ?

_________________
Max
Don't forget to rate


Top
 Profile  
 
 Post subject:
PostPosted: Mon Aug 28, 2006 4:30 am 
Beginner
Beginner

Joined: Thu Aug 03, 2006 3:43 am
Posts: 45
you have obviously misunderstood me... I am talking about generating an id when the entity is created and assigning it as opposed to letting hibernate do the work and generate the thing at insertion...

I don't intend to use sequences.... Things worked pretty good for me with the "native" id generator (although I haven't started with the service layer yet).. The thing is someone with more experience than me in using hibernate told me that's somehow wrong and that i need the ID before saving, and that's what I want to clear up.... I don't want anything fancy, just a simple solution so I can get on with the project...

And no, I haven't read the docs.. I started with "Manning Hibernate in Action" because I always prefer reading from a book...


thank you for the quick response...


Top
 Profile  
 
 Post subject:
PostPosted: Mon Aug 28, 2006 4:36 am 
Hibernate Team
Hibernate Team

Joined: Tue Aug 26, 2003 6:10 am
Posts: 8615
Location: Neuchatel, Switzerland (Danish)
then read Hibernate In Action again ;) it covers id generation pretty vividly.

In any case what you are asking for is what is good/bad with using "assigned" ids.

And the essence of that is that if the id is assigned *before* it is handed to hibernate how can hibernate know that the object is new or needs updating ?

You can then either use save and update explicitly but it is so much easier
normally to use saveOrUpdate or merge but with assigned id's that is not possible out-of-the-box because of the above problem.

So that is the essence and the "way out" is to implement Interceptor.isTransient(o) assuming you somehow can look at an object and say - hey that is new.

_________________
Max
Don't forget to rate


Top
 Profile  
 
 Post subject:
PostPosted: Mon Aug 28, 2006 4:55 am 
Beginner
Beginner

Joined: Thu Aug 03, 2006 3:43 am
Posts: 45
hmm.. close,

Quote:
In any case what you are asking for is what is good/bad with using "assigned" ids.


but no, I was told something like "you should use assigned ids because that's the way it's done" or something like that... I don't really want to go that way but the people I work for are very uncomfortable with the idea that you don't have an id in the object (entity) until you save it in the database... They usually have more experience so I took their word for fact so to speak... But I think they never encountered this problem (which might not really be a problem after all.. in my opinion) because they are using oracle databases with sequences for their projects...

so to put this to rest: I don't "need to know" the ID right after the creation of the object, that can wait until "saving time"... so I don't need to use "assigned", i can go on with "native"... ?

i know all this sounds really strange but it can be very confusing when you get conflicting information from trust wordy sources....


Top
 Profile  
 
 Post subject:
PostPosted: Mon Aug 28, 2006 5:30 am 
Hibernate Team
Hibernate Team

Joined: Tue Aug 26, 2003 6:10 am
Posts: 8615
Location: Neuchatel, Switzerland (Danish)
it all depends on how your system works; but i just explaind you what hibernate can work with and it can work with both "philosophies", but "assigned" has issues in context of being able to see if an object is actually new or old/needs saving or need updating.

And just to make the story complete, the biggest problem with assigned is not mainly their "time of assignment" but that they more often that not indicates a system where the id's are natural id's instead of being synthetic id's and the latter is definitly recommended to use instead of natural ids.

_________________
Max
Don't forget to rate


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.