I am using Hibernate 3.5.6 + Hibernate JPA 2.0 in a Spring MVC 3.0.3 web application context. I have a one-to-many relationship modeled (similar to below). If all the data passes validations, the parent and associated child instances get saved to the database as expected. However, say I null out a single non-nullable property (e.g.
Child.childName) of a single record to reproduce a database exception, two bad things happen:
1. Inconsistent data set is persisted, up to the point of violating data
2. Following Exception is thrown...
Quote:
2010-10-19 12:07:31,721 ERROR [org.hibernate.AssertionFailure] - an assertion failure occured (this may indicate a bug in Hibernate, but is more likely due to unsafe use of the session)
org.hibernate.AssertionFailure: null id in com.hsemuni.internal.newissues.domain.NewIssuePricing entry (don't flush the Session after an exception occurs)
at org.hibernate.event.def.DefaultFlushEntityEventListener.checkId(DefaultFlushEntityEventListener.java:82)
at org.hibernate.event.def.DefaultFlushEntityEventListener.getValues(DefaultFlushEntityEventListener.java:190)
...
I had to implement a filter to circumvent LazyInitiationException error when trying to get detailed children view for a selected parent instance, based on the following Hibernate Community forum post...
OpenSessionInView & DML operations This worked out well, in that I was able to get around the LazyInitiationException error, but I am wondering if it is somehow responsible for the error I am getting. The reason for my suspicion is because when I try to do the same thing in a console based application, the session seems to rollback and I simply encounter the database violation exception.
Code:
@Entity
@Table(name="parent")
public class Parent implements Serializable {
@Id
@GeneratedValue
@Column(name="parent_id")
private Long parentID;
@Column(name="name")
private String parentName;
@OneToMany(mappedBy="parent", fetch=FetchType.LAZY, cascade = {CascadeType.ALL})
private List<Child> children;
}
@Entity
@Table(name="child")
public class Child implements Serializable {
@Id
@GeneratedValue
@Column(name="child_id")
private Long childID;
@Column(name="name")
private String childName;
@ManyToOne(fetch=FetchType.LAZY, cascade={CascadeType.ALL})
@JoinColumn(name="parent_id")
private Parent parent;
}
My service component issues the DAO's save method as follows:
Code:
@Transactional(readOnly=false, propagation=Propagation.REQUIRES_NEW)
public Parent saveParent(Parent parent) {
return parentDAO.saveParent(parent);
}
Can anyone help with this?
Thanks!
Ashwin