-->
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.  [ 2 posts ] 
Author Message
 Post subject: Execute insert with already got ID
PostPosted: Thu Feb 03, 2011 2:33 pm 
Newbie

Joined: Thu Feb 03, 2011 9:55 am
Posts: 1
Hi,

I'm trying to make hibernate execute an insert with an already queried key. I have the GeneratedValue. But in this specific useCase I get the sequenceValue before the insert, then when I try to insert using this ID I got this error:
org.hibernate.PersistentObjectException: detached entity passed to persist

Is there any way to indicate to hibernate when it should execute an insert or an update?

The code is something like this:
Code:
Entity:
@Entity
public class Content implements Serializable, EntityIF {
   private static final long serialVersionUID = 1L;

   @Id
   @SequenceGenerator(name="CONTENT_CONTENTID_GENERATOR", sequenceName="S_CONTENT_ID")
   @GeneratedValue(strategy=GenerationType.SEQUENCE, generator="CONTENT_CONTENTID_GENERATOR")
   @Column(name="content_id")
   private Long id;
}


Code:
public Business () {
   public Save() {
        Long id = getNewId();
        //do some logic

        Content content = new Content();
        content.setId(id);         
        entityManager.getCurrentSession().persist(content);
   }
}


Top
 Profile  
 
 Post subject: Re: Execute insert with already got ID
PostPosted: Sat Feb 05, 2011 1:38 pm 
Newbie

Joined: Fri Jan 07, 2011 7:23 am
Posts: 19
Hi,

I think there is no official way to circumvent the generation of a @GeneratedValue. But creating the instance, setting the
ID manually and calling em.merge() may work.

Robin.


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