-->
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.  [ 5 posts ] 
Author Message
 Post subject: How to assign PK manually when generator specified?
PostPosted: Tue Oct 27, 2009 8:32 am 
Newbie

Joined: Tue Oct 27, 2009 7:37 am
Posts: 5
Hi all,
I'm using envers to audit my data and in some cases, I need to restore records that have been deleted. I want them to be restored exactly as they were. This means that the primary key has to be the same too.

I tried extending base entity to remove @GeneratedValue, but I got the following error:
-Unable to define/override @Id(s) on a subclass

I'd rather not create a completely different class that I would need to keep synchronized with the current one.

How would you go about it? Any suggestions? Pointers?

This question has been asked several times and not yet answered.

Thanks

Sylvain Catudal


Top
 Profile  
 
 Post subject: Re: How to assign PK manually when generator specified?
PostPosted: Tue Oct 27, 2009 9:12 am 
Regular
Regular

Joined: Thu Sep 06, 2007 2:22 am
Posts: 108
Location: Noida,India
Could you pleas post your code and elaborate you problem ?


Top
 Profile  
 
 Post subject: Re: How to assign PK manually when generator specified?
PostPosted: Tue Oct 27, 2009 9:26 am 
Newbie

Joined: Tue Oct 27, 2009 7:37 am
Posts: 5
I forgot to include what is the problem in the first place.

If you try to insert an entity that has the @Id field set, a value for that field will still be generated and the value that was there will be ignored.

Let's look at the following scenario:

I have a user defined like this :

id = 1
username = joe

This user is deleted. It is later found that this user has to be restored. How do we go about restoring him with the same PK.

If I create an instance of user on which I set id = 1 and username = joe, Hibernate will generate a new id when the entity is persisted ignoring the one that I set. Here is how the id field is annotated:
Code:
@Id
@GeneratedValue( generator = "ids" )
@Column( name = "ID", unique = true, nullable = false)
public Long getId() {
   return this.id;
}

public void setId( Long id ) {
   this.id = id;
}


I'd like hibernate not to generate an Id when it is already set.

Hope this clarifies it...


Top
 Profile  
 
 Post subject: Re: How to assign PK manually when generator specified?
PostPosted: Tue Oct 27, 2009 9:59 am 
Regular
Regular

Joined: Thu Sep 06, 2007 2:22 am
Posts: 108
Location: Noida,India
Write Custom Identifier Generator, which use current generator to create to set new id if no id has set, otherwise, don't generate the id (as it is already assign).

you can take help from here als https://www.hibernate.org/296.html


Top
 Profile  
 
 Post subject: Re: How to assign PK manually when generator specified?
PostPosted: Sat Dec 29, 2012 3:11 pm 
Newbie

Joined: Thu Jan 26, 2012 11:56 am
Posts: 9
lol, wth! are for real? i have to do that just to ignore ONCE the dm idGenerator?!
i dont even have a secuence!
@org.hibernate.annotations.GenericGenerator(name = "hibernate-generator", strategy = "increment")

_________________
-------------------------------------------------------------
http://javahelp.redsaltillo.net


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