-->
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.  [ 2 posts ] 
Author Message
 Post subject: Manipulation of null collection in many-to-many
PostPosted: Wed Feb 06, 2008 8:53 am 
Newbie

Joined: Wed Feb 06, 2008 5:42 am
Posts: 1
Hibernate version:3.2.0

Entity:
@Inheritance(strategy = InheritanceType.TABLE_PER_CLASS)
@Table(name = "partner", catalog = "partner", uniqueConstraints = {})
public class PartnerPartner extends PartnerResource
{
/**
*
*/
private static final long serialVersionUID = 1L;

private Set<PartnerSite> sites = new HashSet<PartnerSite>( 0 );

private PartnerOrganization organization;

private PartnerPartnerContact partnerContact;
private PartnerPartnerPayment partnerPayment;

private Set<PartnerCategory> categories = new HashSet<PartnerCategory>( 0 );

private Set<PartnerPartnerContactUs> partnerContactUss = new HashSet<PartnerPartnerContactUs>( 0 );

private Set<PartnerPartnerRate> partnerRates = new HashSet<PartnerPartnerRate>( 0 );

// Constructors

public PartnerPartner()
{
super();
}

public PartnerPartner( Integer id )
{
super( id );
}

public PartnerPartner( String name )
{
super( name );
}

@ManyToMany(cascade = { CascadeType.MERGE, CascadeType.PERSIST, CascadeType.REFRESH }, fetch = FetchType.LAZY)
@JoinTable(name = "partner_site", catalog = "partner", joinColumns = { @JoinColumn(name = "partner_id") }, inverseJoinColumns = @JoinColumn(name = "site_id"))
public Set<PartnerSite> getSites()
{
return sites;
}

public void setSites( Set<PartnerSite> sites )
{
this.sites = sites;
}

@OneToOne(cascade = { CascadeType.MERGE, CascadeType.PERSIST, CascadeType.REFRESH }, fetch = FetchType.EAGER)
@JoinColumn(name = "organization_id", unique = false, nullable = false, insertable = true, updatable = true)
public PartnerOrganization getOrganization()
{
return organization;
}

public void setOrganization( PartnerOrganization organization )
{
this.organization = organization;
}

@ManyToMany(cascade = CascadeType.ALL, fetch = FetchType.EAGER)
@JoinTable(name = "category_partner", joinColumns = { @JoinColumn(name = "partner_id") }, inverseJoinColumns = @JoinColumn(name = "category_id"))
public Set<PartnerCategory> getCategories()
{
return categories;
}

public void setCategories( Set<PartnerCategory> categories )
{
this.categories = categories;
}

@OneToOne(cascade = { CascadeType.MERGE, CascadeType.PERSIST, CascadeType.REFRESH }, fetch = FetchType.LAZY, optional = true)
@PrimaryKeyJoinColumn
public PartnerPartnerContact getPartnerContact()
{
return this.partnerContact;
}

public void setPartnerContact( PartnerPartnerContact partnerContact )
{
this.partnerContact = partnerContact;
}

@OneToOne(cascade = { CascadeType.MERGE, CascadeType.PERSIST, CascadeType.REFRESH }, fetch = FetchType.LAZY, optional = true)
@PrimaryKeyJoinColumn
public PartnerPartnerPayment getPartnerPayment()
{
return partnerPayment;
}

public void setPartnerPayment( PartnerPartnerPayment partnerPayment )
{
this.partnerPayment = partnerPayment;
}

@OneToMany(cascade = { CascadeType.MERGE, CascadeType.PERSIST, CascadeType.REFRESH }, fetch = FetchType.LAZY, mappedBy = "partner")
public Set<PartnerPartnerContactUs> getPartnerContactUss()
{
return partnerContactUss;
}

public void setPartnerContactUss( Set<PartnerPartnerContactUs> partnerContactUss )
{
this.partnerContactUss = partnerContactUss;
}

@OneToMany(mappedBy = "partner", cascade = { CascadeType.MERGE, CascadeType.PERSIST, CascadeType.REFRESH }, fetch = FetchType.LAZY)
public Set<PartnerPartnerRate> getPartnerRates()
{
return partnerRates;
}

public void setPartnerRates( Set<PartnerPartnerRate> partnerRates )
{
this.partnerRates = partnerRates;
}

@Override
public boolean equals( Object other )
{
if( super.equals( other ) )
return true;

if( other instanceof PartnerPartner )
{

}

return false;
}

@Override
public String toString()
{
return "Partner : Id[" + getId() + "], Name[" + getName() + "], Uri[" + getUri() + "], Status[" + getStatus() + "]";
}



}

Entity Manager code:
(em is EntityManager, et is EntityTransaction)

et = em.getTransaction();
et.begin();

partner = em.merge( partner );

et.commit();

Name and version of the database you are using: MySql 5.0.22

Hi,
I have a bean representing a "PARTNER" table with maps to many other tables. a couple of those relations are many-to-many.
When ever I want to update some data in the bean, unless I populate the many-to-many collections, the merge() assumes that the empty collection means no related rows and delete the data in the relation table.
This is a problem since populating some of this collections can be quite large and time consuming. so... sometimes I need to update the relations and sometimes I don't...
further more, sometimes I only want to add one relational row, and again, I need to load the entire collection.
Is there a way to inform hibernate that I do NOT want to cascade the update to null collections?
Is there any smart way to manipulate many-to-many collections without loading the entire collection?

Thanks,
oryanay


Top
 Profile  
 
 Post subject:
PostPosted: Wed Feb 06, 2008 9:57 am 
Beginner
Beginner

Joined: Thu Dec 13, 2007 2:32 pm
Posts: 26
Boss,

Correct me if i am wrong, but i think Merge assumes that there was a detached entity and you performed some operations on it, and now that you are done, you want to merge the changes, so basically using its own versioning system, it will delete the changes. try saveOrUpdate.

Can you paste your persistence.xml and your Table structure ( just give partner's set PartnerSites )


Edit : What i mean by deletion is, that this detached entity was assumed to have some state in db, now when you do an insert it will persist it as an update, and so if you have a null collection it would delete them ( since its a cascade ).

Regards
Vyas, Anirudh

_________________
Regards,
Vyas, Anirudh


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