-->
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: Persistence a child object with existing parent one
PostPosted: Wed Mar 30, 2011 6:29 am 
Newbie

Joined: Wed Mar 30, 2011 6:23 am
Posts: 3
Assume thre are two entities with ManyToOne relation:

Code:
@Entity
public class A {
   private long code;

   @Id
   @GeneratedValue(strategy=GenerationType.AUTO)   
   public long getCode() {
      return code;
   }
   
   public viod setCode(long code) {
      this.code = code;
   }
   
   // additional setters and getters
}

@Entity
public class B {
   private long code;
   private A a;
   
   @Id
   @GeneratedValue(strategy=GenerationType.AUTO)   
   public long getCode() {
      return code;
   }
   
   public viod setCode(long code) {
      this.code = code;
   }   
   
   @ManyToOne(fetch=FetchType.LAZY)
   @JoinColumn(name="acode", nullable=false)   
   public A getA() {
      return a;
   }

   public void setA(A a) {
      this.a = a
   }
   
   // additional setters and getters   
}


As part of the business flow I have code value of the existing A object and I need to insert new B object and relate it to the existing A.
This code works fine:

Code:
// inject em
A a = em.find(A.class, code);
B b = new B();
b.setA(a);
em.persist(b);


My problem is with "A a = em.find(A.class, code)" line. This looks like for me as a redundant query because the B table contains a foreign key (acode).
In order to improve performance I tried to create A object with an existing code value:

Code:
A a = new A();
a.setCode(code);
B b = new B();
b.setA(a);
em.persist(b);


but it doesn't work.
Is there any way to avoid unnecessary query?


Top
 Profile  
 
 Post subject: Re: Persistence a child object with existing parent one
PostPosted: Wed Mar 30, 2011 9:25 am 
Regular
Regular

Joined: Fri Jan 28, 2011 11:44 am
Posts: 117
Use em.getReference instead of em.find to get the existing A.
em.getReference will return a proxy for the corresponding A object without actually fetching it in the database!


Top
 Profile  
 
 Post subject: Re: Persistence a child object with existing parent one
PostPosted: Wed Mar 30, 2011 10:52 am 
Newbie

Joined: Wed Mar 30, 2011 6:23 am
Posts: 3
Thanks, that exactly what I was looking for


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.