-->
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: Duplicated items in One-To-Many collection
PostPosted: Sun Mar 20, 2011 2:49 pm 
Newbie

Joined: Sun Oct 31, 2010 7:45 am
Posts: 3
Hi everybody,

I've faced with some interesting bug, when querying for an object
in two different ways affects the one-to-many association.
I have an Order entity with one-to-many relationship with Ticket entity.

Code:
@OneToMany(cascade = {CascadeType.ALL}, fetch = FetchType.EAGER, mappedBy = "parentOrder")
    @org.hibernate.annotations.Cascade(org.hibernate.annotations.CascadeType.DELETE_ORPHAN)
    public Collection<Ticket> getTickets()
    {
        return tickets;
    }


Using the following method to find an Order by primary key:
Code:
@Override
    public Order findById(Serializable id)
    {
        return jpaTemplate.find(Order.class, id);
    }

results in fetching the Order object with 2(!) tickets in tickets collection while there is only one(!) in database !
I was playing a bit and changed the above method to query for Order entity by id differently:
Code:
@Override
    public Order findById(Serializable id)
    {
        List results = jpaTemplate.find("select o from Order o where o.id = ?1", id);
        return results.size() > 0 ? (Order) results.get(0) : null;
    }

That did a trick, now everything is fine. I'm getting 1 ticket object in tickets collection.

Can anybody tell what was that ? How could duplicates items appear in collection ?


Top
 Profile  
 
 Post subject: Re: Duplicated items in One-To-Many collection
PostPosted: Mon Mar 21, 2011 3:57 am 
Expert
Expert

Joined: Tue Jun 16, 2009 3:36 am
Posts: 990
Do you override the hashkey() and/or equals() method of tickets entity ?


Top
 Profile  
 
 Post subject: Re: Duplicated items in One-To-Many collection
PostPosted: Mon Mar 21, 2011 4:08 am 
Newbie

Joined: Sun Oct 31, 2010 7:45 am
Posts: 3
Sure, I override it.
I just want to understand how come Hibernate have managed to insert two items to collection (PersistenceBag) when there is only really one in the database ?! And this issue is just fixed by changing
entity querying approach...


Top
 Profile  
 
 Post subject: Re: Duplicated items in One-To-Many collection
PostPosted: Mon Mar 21, 2011 4:32 am 
Expert
Expert

Joined: Tue Jun 16, 2009 3:36 am
Posts: 990
Quote:
Sure, I override it.


I asked, because it many realities of using Hibernate it is absolutely not necessary to override hashkey() and/or equals() methods of entities.
Only if you compare detached entities which derive from different persistent contexts, you need to do that.
In most realities indeed the identity scope guarantee provided by hibernate is sufficient.

Anyway overriding hashkey() and/or equals() methods is a delicate thing, as wrong implementations easy lead to
strange results as you report here for example.


1. Do you reallly need to override hashkey() and equals() methods?
2. Does the duplicated item still appear if you, for test purpose only, disable the hashkey() and equals() methods?


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.