-->
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.  [ 4 posts ] 
Author Message
 Post subject: Hibernate with JPA entity manager merge() question/problem?
PostPosted: Sun Jun 15, 2008 3:19 am 
Newbie

Joined: Sun Jun 15, 2008 2:07 am
Posts: 4
Location: Malaysia
Dear all,

I have come across a problem which I was puzzled and need some help. I have a biz transaction table name "A" that reference a master table e.g. Country.


@Entity
public class A {
@ManyToOne(fetch=FetchType.LAZY)
@JoinColumn(name="COUNTRY_CODE",insertable=false,updatable=false)
private Country country;

@Column(name="COUNTRY_CODE")
private String countryCode;
...

@Entity
public class Country {
// no one to many association for A defined
...


Using entityManager.find(A.class, pk) to retrieve A entity instance allow me to lazy load my Country reference simply with a.getCountry().toString();

Whenever i have a detached A entity instance to be updated, I simply do:

A managedA = entityManager.merge(detachedA).

However, the instance returned: managedA doesn't trigger lazy loading of my country reference when i do managedA.getCountry().toString(); The country reference will always return null.

I have tried with entityManager.flush(); after the merge() operation and yet it still return null country reference. I was even puzzled further when i replaced flush() with entityManager.find() after the merged() operation, result remained the same with further disappointment.

I am not sure whether this is a problem of hibernate or is my approach to tackle this problem wasn't correct. Need some advice from fellow experts. Thanks in advance.


Hibernate version: 3.2.6.ga

_________________
Andy


Top
 Profile  
 
 Post subject:
PostPosted: Thu Aug 28, 2008 4:20 am 
Newbie

Joined: Mon May 26, 2008 6:36 am
Posts: 5
Did you find solution about that ? I'm facing to the same problem, i can't load the entity B in relation with the entity A when the fetch type is LAZY, when it's EAGER i can obtain it, but I don't wan't to put all my relation on EAGER...

So someone has a link to redirect for the solution ? (I'm using Spring which manage my entities jpa)

thanks


Top
 Profile  
 
 Post subject:
PostPosted: Thu Aug 28, 2008 5:10 pm 
Newbie

Joined: Sat Aug 02, 2008 12:09 pm
Posts: 12
Hi,

If country of detached A is null, merge will delete this association. Did you load country before A became detached? If A is a detached entity, its state is copied into existing managed instance A.

Cheers

G


Top
 Profile  
 
 Post subject:
PostPosted: Mon Sep 22, 2008 11:06 am 
Newbie

Joined: Fri Jul 11, 2008 10:45 am
Posts: 5
Try changing the annotation for country

from

@ManyToOne(fetch=FetchType.LAZY)

to

@ManyToOne(fetch=FetchType.LAZY, cascade=CascadeType.MERGE)


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