-->
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.  [ 6 posts ] 
Author Message
 Post subject: JPA Hibernate vs EclipseLink: Merge() differs for lazy rel
PostPosted: Thu Sep 03, 2009 12:23 pm 
Newbie

Joined: Thu Apr 16, 2009 9:39 am
Posts: 4
Hi,

we experience a huge difference in behaviour between Hibernate JPA 3.2.5 and EclipseLink 1.2.2 when merging objects with "lazy" relations.

Suppose you have a Master class with one-to-many relation to Detail like

Code:
@Entity
public class Master implements Serializable {
    @Id
    private Long id;

    @OneToMany(mappedBy = "master", cascade={CascadeType.MERGE, CascadeType.PERSIST})
    private List<Detail> details;
...
}

@Entity
public class Detail implements Serializable {
    @Id
    private Long id;

    @ManyToOne
    private Master master;
...
}


Suppose further we load the Master from the database without fetching all the Details and close the entityManager. Now Master is in detached state. In a Unit test it would look something like

Code:
        //Merge with no changes, hibernate should not load the detail object
        EntityManager em = emf.createEntityManager();
        Master master;

        master = em.find(Master.class, 1L);
        em.close();
       
        em = emf.createEntityManager();
        em.getTransaction().begin();
//        ((Session)em.getDelegate()).saveOrUpdate(master);
        em.merge(master);
        em.getTransaction().commit();


If you run this code with Hibernate (3.2.5, 3.3.1) and Hibernate EntityManager 3.4.0 you will find out that a merge on Master causes Hibernate JPA to load the Details collection(!) although it was in a lazy state and nothing has changed. This is not compliant with JPA spec which states that merges are ignored for lazy relations and can cause a tremendous performance impact.

EclipseLink on the other side behaves as expected: it selects the Master from the database without fetching the Details. If you uncomment
Code:
//        ((Session)em.getDelegate()).saveOrUpdate(master);
and rerun the test, Hibernate behaves fine.

Can someone from the Dev Team reveal if this is a Hibernate bug? Thank you.

squib


Top
 Profile  
 
 Post subject: Re: JPA Hibernate vs EclipseLink: Merge() differs for lazy rel
PostPosted: Sun Apr 18, 2010 7:15 pm 
Newbie

Joined: Fri Apr 16, 2010 3:50 pm
Posts: 3
I know this is a really old topic, but I am having the EXACT same problem... and just wondering if this is a bug? or intentional? and if its intentional.. can someone please explain why..

when I merge my entity in to a new Entity Manager, its loading thousands of records... for no reason


Derek


Top
 Profile  
 
 Post subject: Re: JPA Hibernate vs EclipseLink: Merge() differs for lazy rel
PostPosted: Mon Apr 19, 2010 8:54 am 
Newbie

Joined: Thu Apr 16, 2009 9:39 am
Posts: 4
kingsob wrote:
I know this is a really old topic, but I am having the EXACT same problem... and just wondering if this is a bug? or intentional? and if its intentional.. can someone please explain why..

when I merge my entity in to a new Entity Manager, its loading thousands of records... for no reason


Derek


Hi Derek,

I filed this as a bug and it has been surprisingly closed without much further investigation.. The explanation of Steve is quite interesting: See http://opensource.atlassian.com/project ... issue-tabs


Top
 Profile  
 
 Post subject: Re: JPA Hibernate vs EclipseLink: Merge() differs for lazy rel
PostPosted: Mon Apr 19, 2010 7:06 pm 
Newbie

Joined: Fri Apr 16, 2010 3:50 pm
Posts: 3
that's very surprising to me as well...

I wonder why the following does not load the LAZY collections...

Code:
        EntityManager em = emf.createEntityManager();
        Master master;
        master = em.find(Master.class, 1L);
        em.getTransaction().begin();
        em.merge(email);
        em.getTransaction().commit();
        em.close();


why does it behave differently if detached?

and for me, it goes one step further.. it loads not only the collections in Master, but loads the ones in Detail as well.. which has more collections... causing hundreds of thousands of records to be loaded, it literally sits at my em.merge() for a good 5 minutes

this is my code

Code:
@Entity
@Table(name = "email")
public class Email implements Serializable
{
    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    @Column(name = "emailid", nullable = false)
    private Long emailid;

    @JoinColumn(name = "userid", referencedColumnName = "userid", nullable = false)
    @ManyToOne
    private Users user;

    ...
}

@Entity
@Table(name = "users")
public class Users implements Serializable
{
    @Id
    @Column(name = "userid", nullable = false)
    private Integer userid;

    @JoinColumn(name = "companyid", referencedColumnName = "companyid")
    @ManyToOne(fetch = FetchType.EAGER)
    private Companies companyid;

    ...
}

@Entity
@Table(name = "companies")
public class Companies implements Serializable
{
    @Id
    @Column(name = "companyid", nullable = false)
    private Integer companyid;

    @OneToMany(cascade = CascadeType.ALL, mappedBy = "companyid")
    private Collection<Activity> activityCollection;

    ...
}

@Entity
@Table(name = "activity")
public class Activity implements Serializable
{
    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    @Column(name = "id", nullable = false)
    private Long id;

    ...
}


and the following code causes the activity collection to be loaded..

Code:
       
    EntityManager em = emf.createEntityManager();
    Email email = em.find(Email.class, 13436L);

    em.clear();

    email.setStatus("test");
    em.getTransaction().begin();
    em.merge(email);
    em.getTransaction().commit();


I am switching from EclipseLink, and as you said, it behaves as I would expect


Derek


Top
 Profile  
 
 Post subject: Re: JPA Hibernate vs EclipseLink: Merge() differs for lazy rel
PostPosted: Tue Apr 20, 2010 6:27 am 
Newbie

Joined: Thu Apr 16, 2009 9:39 am
Posts: 4
kingsob wrote:
why does it behave differently if detached?
Derek


According to Steve it is the "propagation of state":

The merge operation allows for the propagation of state from detached entities onto persistent entities
managed by the entity manager.

Hibernate's impl obviously goes this way. So any detached object has to be loaded otherwise state could not be propagated..

I think from this point of view it makes even sense. However, I do not know how EclipseLink handles it but it does better. The ignorance of the Hibernate developer (see my bug report) makes us thinking to use EclipseLink for all new projects..


Top
 Profile  
 
 Post subject: Re: JPA Hibernate vs EclipseLink: Merge() differs for lazy rel
PostPosted: Wed Feb 01, 2012 4:43 pm 
Newbie

Joined: Wed Sep 09, 2009 6:49 pm
Posts: 2
Grettings all. I must say that I admire the developers from Hibernate, people who are putting effort from their own time to develop such a nice application.
Still we have to remember that the specification is to communicate better so if either devs or users have a misunderstanding of the specification then things can go wrong to either devs forcing users to develop in some way (Internet Explorer rings a bell) or users using the tool incorrectly, afecting their critical environments and therefore saying is not good just because they are not using correctly.

I believe one good way is to speak with the ones in charge of the specification directly so they would give a hint on this matter.

Also its important to remember that the end does not justify the means. :).

Here is more a question of performance vs integrity.

ALso thats one of the reasons to use JPA standard code in your implementation so if one does not work for users, they can move freely and without issues to other one.


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