• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

id value should be sequence or asssigned

 
Charan kumar sarvepalli
Ranch Hand
Posts: 40
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hi,

i have an entity which will have the PK is configured with has sequence generator
Code:
<generator class="sequence">
<param name="sequence">ACC_SEQ</param>
</generator>


now some times i can define my own value for this PK but the hibernate is overriding my value with sequence value while adding new.
i want to generate the value from sequence when i was not assigned any value to it. If have assigned value then it should not override with the sequence value.

Is this possible..?

please tell me how to configure in hbm file to satisfy my requirement or any other way.

Thanks You.
 
Paul Sturrock
Bartender
Posts: 10336
Eclipse IDE Hibernate Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If you are going to assign PK values at all you will need to use the "assigned" generation strategy, you can't mix two strategies.

Why do you need to mix the two? As a general rule well designed PKs are normally meaningless (except to the database).
 
Charan kumar sarvepalli
Ranch Hand
Posts: 40
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks for your quick reply.

but my requirement is like that only,
while loading data to data base some times we mention id , if the id is already existing then update, if not existing then we can insert with the given id.
id is not mentioned we need to generate the id and insert data.
 
Sridhar Santhanakrishnan
Ranch Hand
Posts: 317
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
try using session.saveOrUpdate(object) instead of save()
 
Paul Sturrock
Bartender
Posts: 10336
Eclipse IDE Hibernate Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Charan kumar sarvepalli wrote:Thanks for your quick reply.

but my requirement is like that only,
while loading data to data base some times we mention id , if the id is already existing then update, if not existing then we can insert with the given id.
id is not mentioned we need to generate the id and insert data.


What is your reason for doing this? Why does the ID matter?
 
Charan kumar sarvepalli
Ranch Hand
Posts: 40
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,
I am using method persist().
it is because previously we implemented as assigned, now we are trying to implement using sequence.which is breaking previous implemented code, i need to change lot of classes to make effect of this.

 
Charan kumar sarvepalli
Ranch Hand
Posts: 40
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
session.saveOrUpdate() not doing for me, it is firing update query not inserted the value.
 
Paul Sturrock
Bartender
Posts: 10336
Eclipse IDE Hibernate Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Charan kumar sarvepalli wrote:Hi,
I am using method persist().
it is because previously we implemented as assigned, now we are trying to implement using sequence.which is breaking previous implemented code, i need to change lot of classes to make effect of this.



Yes, you will have to change any code that tries to assign a value to a generated ID. There is no way around this, like I said before youcan't mix key generation strategies.
 
Sridhar Santhanakrishnan
Ranch Hand
Posts: 317
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If the flow is part of an insert operation. then you will have change your code to use the generated id and remove all lines which assign the ID.
 
Charan kumar sarvepalli
Ranch Hand
Posts: 40
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,

Thanks for all your replies

I think only possible option is to implement the "IdentifierGenerator" interface.
so i have implemented but it is working fine on save(), it is failing on persist() with the same exception "org.hibernate.PersistentObjectException: detached entity passed to persist".

 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic