newbie q: problem deleting record with hibernate/seam
henderson_mk Oct 27, 2005 5:28 AMHi folks,
I have a little application that goes like this: a property has a number of offers on it. an offer has a person who placed the offer and the person has an address.
so:
@Entity @Name("property") public class Property implements Serializable { /** * Default Constructor. */ public Property() { } /** * Get the property identifier. */ @Id(generate=GeneratorType.AUTO) public Integer getId() { return id; } /** * Set the property identifier. */ public void setId(Integer id) { this.id = id; } /** * Get the list of offers on <code>this</code> property. */ @OneToMany(cascade = CascadeType.ALL) public List<Offer> getOffers() { return offers; } /** * Set an offer on this property. * * @pre offer != null * @post An offer has been placed on this property. */ public void setOffers(List<Offer> offers) { this.offers = offers; } /** * Add an offer on this property. * * @pre offer != null * @post An offer has been placed on this property. */ public void addOffer(Offer offer) { this.offers.add(offer); } /** * The property attributes */ private int id; private List<Offer> offers = new ArrayList<Offer>(); }
the offer.
@Entity @Name("offer") public class Offer implements Serializable { /** * Default Constructor. */ public Offer() { person = new Person(); } /** * Get the offer identifier. */ @Id(generate=GeneratorType.AUTO) public Integer getId() { return id; } /** * Set the offer identifier. */ public void setId(Integer id) { this.id = id; } /** * Get the offer amount. */ public double getAmount() { return amount; } /** * Set the offer amount. * * @pre amount != null * @post The offer amount for <code>this</code> has been set. */ public void setAmount(double amount) { this.amount = amount; } /** * Get the date the offer was placed on. */ @Basic(temporalType=TemporalType.DATE) @NotNull public Date getDate() { return date; } /** * Set the date the offer was placed on. */ public void setDate(Date date) { this.date = date; } /** * Get the person who placed the offer on the house. */ @OneToOne(cascade = CascadeType.ALL) @NotNull(message="Please supply the person who placed this offer.") public Person getPerson() { return person; } /** * Set the person who made the offer. * * @pre person != null * @post The person for <code>this</code> has been set. */ public void setPerson(Person person) { this.person = person; } /** * The offer attributes */ private int id; private double amount; private Date date; private Person person; }
the code to delete an offer:
@Scope(EVENT) @Name("editOffer") @LoggedIn public class EditOfferAction { /** * Update an offer on a property. */ @IfInvalid(outcome=REDISPLAY) public String update() { log.info(">>> editing offer"); propertyDatabase.persist(offer); propertyDatabase.flush(); log.info("<<< edited offer"); return "updateOfferSuccess"; } /** * Remove the specified offer from the database. */ @IfInvalid(outcome=REDISPLAY) public String delete() { log.info(">>> deleting offer"); log.info("session isDirty=" + propertyDatabase.isDirty()); log.info("**** DELETING OFFER"); propertyDatabase.delete(offer); log.info("**** FLUSHING SESSION"); propertyDatabase.flush(); log.info("session isDirty=" + propertyDatabase.isDirty()); log.info("<<< deleted offer"); return "deleteOfferSuccess"; } /** * Is the offer object to update. */ @In private Offer offer; /** * Is the property database. */ @In(create=true) private Session propertyDatabase; /** * Is the JSF Context. */ @In private FacesContext facesContext; /** * Is logger for this class. */ private static final Logger log = Logger.getLogger(EditOfferAction.class); }
I can't seem to get the delete to work. I'm getting TransientObjectExceptions on the reference to the Person class. So I then tried to persist the person before that and then after the delete hibernate is inserting rows? I'm a bit confused to be honest. I thought the annotation for CascadeType.ALL would have managed this for me?
Hope someone can point me in the right direction.
Thanks,
Marty