-->
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.  [ 3 posts ] 
Author Message
 Post subject: merge an object graph
PostPosted: Wed Jul 08, 2009 7:54 am 
Beginner
Beginner

Joined: Thu Jun 30, 2005 1:04 pm
Posts: 26
Hi,

I hava a simple mapping :

Code:
@Entity @Table(name = "BAR")
@org.hibernate.annotations.Entity(dynamicUpdate=true, dynamicInsert=true,selectBeforeUpdate=false)
public class Bar implements java.io.Serializable {

   @Id
   private String id;

   @ManyToOne(cascade={CascadeType.PERSIST}, fetch=FetchType.LAZY, optional=true)
   @JoinColumn(nullable=false)
   private Foo foo;

   @Temporal(TemporalType.TIME)
   private Date timeAttribut;

   @Version
   private Integer version;

Code:
@javax.persistence.Entity()
@javax.persistence.Table(name = "FOO")
public class Foo implements java.io.Serializable {


   @Id
   private String id;

        private Long cpt;

   @OneToMany(fetch = FetchType.LAZY, mappedBy = "foo")
   private Collection<Bar> bars = new ArrayList<Bar>();



If i get a detached instance of Bar from my ihm with the Foo id associated (this Foo id exist in DB), the code bellow works perfectly (my Bar object is updated in DB) :
Code:
   @Transactional()
   public void updateBar(Bar bar, String fooId ) {
      Foo foo = new Foo(fooId );
      bar.setFoo(foo);
      em.merge(bar);
   }


If i add a @Version to Foo class, the same code doesn't work anymore. I get "object references an unsaved transient instance - save the transient instance before merging: com.enterpriselayer.bo.Foo"

I want to avoid a get() to load the Foo instance from DB. How can i do that ?

Thanks


Last edited by Francky on Thu Jul 09, 2009 4:09 am, edited 1 time in total.

Top
 Profile  
 
 Post subject: Re: merge an object graph
PostPosted: Wed Jul 08, 2009 5:54 pm 
Newbie

Joined: Wed Jun 24, 2009 12:24 pm
Posts: 5
The following is from the Hibernate reference that you might find useful:
Quote:
A version or timestamp property should never be null for a detached
instance, so Hibernate will detect any instance with a null version or
timestamp as transient, no matter what other unsaved-value strategies are
specified. Declaring a nullable version or timestamp property is an easy way
to avoid any problems with transitive reattachment in Hibernate, especially
useful for people using assigned identifiers or composite keys!


Top
 Profile  
 
 Post subject: Re: merge an object graph
PostPosted: Thu Jul 09, 2009 4:44 am 
Beginner
Beginner

Joined: Thu Jun 30, 2005 1:04 pm
Posts: 26
Hello an thanks for your response.

Sometime it's good to re-read base documentation...

I just hoped that a cool option could by-pass the version attribute for a many-to-one where only the id is used as foreign key.


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