-->
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: OneToMany mapping using Map. Problem with merge
PostPosted: Sun Oct 31, 2010 12:36 am 
Newbie

Joined: Fri Jun 04, 2010 8:57 pm
Posts: 19
I have a OneToMany relationship between a Donor entity and Phone entity. Each Donor entity has a many phones. I have mapped this relationship using java.util.Map with shared primary key. Here is what the mapping looks like.
In Donor entity
Code:
    @OneToMany(mappedBy="donor", fetch=FetchType.EAGER,cascade=CascadeType.ALL)
    @MapKey(name="phoneType")
    private Map<PhoneType, Phone> phonesMap = new HashMap<PhoneType,Phone>();

In Phone entity
Code:
    @Id
    @Column(name="Phone_Type")
    @Enumerated
    private PhoneType phoneType;

    @Id
    @ManyToOne
    @JoinColumn(name="Donor_Id")
    private Donor donor;

This relation works fine if I try to persist a new Donor entity. But if I try to modify the existing donor entity using detachment and merging I get following exception.
Code:
javax.persistence.PersistenceException: org.hibernate.PersistentObjectException: detached entity passed to persist: com.arpanjyoti.model.Donor

Here is the code that I am using for modifying the Donor entity.
First read the Donor entity
Code:
            tran.begin();
            Donor managedDonor = em.find(Donor.class, donorId);
            tran.commit();

Now modify the Donor entity and try to merge
Code:
            tran.begin();
            Donor managedDonor = em.merge(donor);
            tran.commit();

The merge operation gives the above mentioned exception. The strange part is that I don't even modify the Donor to Phone relationships prior to merge. Not only that, if I don't modify Donor object and just try to merge as it was before, I still get the above said exception.
Any suggestions why merge fails with exception.

regards,
Nirvan


Top
 Profile  
 
 Post subject: Re: OneToMany mapping using Map. Problem with merge
PostPosted: Mon Nov 01, 2010 11:24 am 
Hibernate Team
Hibernate Team

Joined: Tue Aug 11, 2009 2:26 am
Posts: 29
you want to use phoneType and donor as ids?
Code:
@Id
    @Column(name="Phone_Type")
    @Enumerated
    private PhoneType phoneType;

    @Id
    @ManyToOne
    @JoinColumn(name="Donor_Id")
    private Donor donor;


Top
 Profile  
 
 Post subject: Re: OneToMany mapping using Map. Problem with merge
PostPosted: Mon Nov 01, 2010 2:16 pm 
Newbie

Joined: Fri Jun 04, 2010 8:57 pm
Posts: 19
Yes I wan't to use combination of donor and phone type as ids. That way there will be only one record for given donor and phone type. The donor can have multiple records, but they will differ in Phone Type. For example a donor will have a Mobile phone number, Office Phone number and residence phone number. Do you think this design is not proper ?

I am really stuck at this problem. I am not able to update the Donor record via merge.

regards,
Nirvan.


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.