-->
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: How to remove() entity with Many-to-one association
PostPosted: Wed Nov 05, 2008 12:20 pm 
Newbie

Joined: Wed Nov 05, 2008 12:12 pm
Posts: 1
Hi,

I have the following entity fields:

...
@Id
@GeneratedValue
@Column(name = "ID", nullable = false)
private Short id;
@Column(name = "NAME")
private String name;
@Column(name = "CATEGORY", nullable = false)
private int category;
@Column(name = "JPAQLSTATEMENT", nullable = false)
private String jpaqlstatement;
@OneToMany(cascade = CascadeType.ALL, mappedBy = "tblAdvFilter")
private List<TblAdvFilterRow> tblAdvFilterRowCollection =
new ArrayList<TblAdvFilterRow>();
@ManyToOne(cascade=CascadeType.ALL)
@JoinTable(name = "TBLADVFILTERUSER", joinColumns =
{@JoinColumn(name = "FILTERID")}, inverseJoinColumns =
{@JoinColumn(name = "USERID")})
private TblUser owner;

....

with this method I want to delete this entity instance from the database:

public void deleteAdvFilter(TblAdvFilter p_filter) throws SystemException {
try {
utx.begin();
TblAdvFilter filter = em.find(TblAdvFilter.class, p_filter.getId());
filter.getTblAdvFilterRowCollection().remove(filter);
em.remove(filter);
em.flush();
utx.commit();
} catch (Exception ex) {
utx.rollback();
logger.severe(MessageUtil.getMessage("error.advfilter.errorDeletingFilter") + ex.getMessage());
}
}


but the result is the following erro message:

deleted entity passed to persist: [......TblUser#<null>]

How do I correctly drop this instance, whats wrong.


Thanks for your advice!


Top
 Profile  
 
 Post subject:
PostPosted: Thu Nov 06, 2008 9:12 am 
Beginner
Beginner

Joined: Wed Apr 23, 2008 2:00 pm
Posts: 20
you could add the annotation
@Cascade ({@Cascade({CascadeType.ALL,CascadeType.DELETE_ORPHAN}) })
to your @OneToMany and @ManyToOne attributes and then simply do :


@Id
@GeneratedValue
@Column(name = "ID", nullable = false)
private Short id;
@Column(name = "NAME")
private String name;
@Column(name = "CATEGORY", nullable = false)
private int category;
@Column(name = "JPAQLSTATEMENT", nullable = false)
private String jpaqlstatement;
@OneToMany(cascade = CascadeType.ALL, mappedBy = "tblAdvFilter")
@Cascade ({@Cascade({CascadeType.ALL,CascadeType.DELETE_ORPHAN}) })
private List<TblAdvFilterRow> tblAdvFilterRowCollection =
new ArrayList<TblAdvFilterRow>();
@ManyToOne(cascade=CascadeType.ALL)
@JoinTable(name = "TBLADVFILTERUSER", joinColumns =
{@JoinColumn(name = "FILTERID")}, inverseJoinColumns =
{@JoinColumn(name = "USERID")})
@Cascade ({@Cascade({CascadeType.ALL,CascadeType.DELETE_ORPHAN}) })
private TblUser owner;

....


public void deleteAdvFilter(TblAdvFilter p_filter) throws SystemException {
try {
utx.begin();
TblAdvFilter filter = utx.get(TblAdvFilter.class, p_filter.getId());
utx.delete(filter);
utx.commit();
} catch (Exception ex) {
utx.rollback();
logger.severe(MessageUtil.getMessage("error.advfilter.errorDeletingFilter") + ex.getMessage());
}


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.