Hi,
I have an error message when using @Size ... here is the test (using hibernate 3.2.4.ga with entitymanager 3.3.1.GA and validator 3.0.0.GA) :
A boat has a crew of person, a person has one to many shoes.
Code:
@Entity
public class Boat {
@Id @GeneratedValue
private Long id;
@OneToMany( cascade=CascadeType.ALL )
@Size( min = 1 )
@NotNull
private List< Person > crew;
private boolean sink;
public Long getId() {
return id;
}
public boolean isSink() {
return sink;
}
public void setSink(boolean sink) {
this.sink = sink;
}
public List<Person> getCrew() {
if( crew == null )
crew = new ArrayList< Person >();
return crew;
}
public void setCrew(List<Person> crew) {
this.crew = crew;
}
}
Code:
@Entity
public class Person {
@Id @GeneratedValue
private Long id;
@OneToMany( cascade=CascadeType.ALL )
@Size( min = 1 )
@NotNull
private List< Shoe > shoes;
public List<Shoe> getShoes() {
if( shoes == null )
shoes = new ArrayList< Shoe >();
return shoes;
}
public void setShoes(List<Shoe> shoes) {
this.shoes = shoes;
}
}
Code:
@Entity
public class Shoe {
@Id @GeneratedValue
private Long id;
}
Now i'm doing 2 basic transactions :
first creating a boat with a person owning a shoe then I try to sink the boat
Code:
EntityManagerFactory emf = Persistence.createEntityManagerFactory( "myDatabase" );
// first transaction, persisting entities
EntityManager em = emf.createEntityManager();
em.getTransaction().begin();
Boat b = new Boat();
Person p = new Person();
Shoe s = new Shoe();
p.getShoes().add( s );
b.getCrew().add( p );
em.persist( b );
em.getTransaction().commit();
em.close();
// second transaction trying to sink the boat
em = emf.createEntityManager();
em.getTransaction().begin();
b = em.find( Boat.class, b.getId() );
b.setSink( true );
em.getTransaction().commit();
em.close();
emf.close();
I get :
Code:
SEVERE: 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: collection [Person.shoes] was not processed by flush()
at org.hibernate.engine.CollectionEntry.postFlush(CollectionEntry.java:205)
If I remove the @Size constraint then everything work fine.
If I fetch the collection before commiting the second transaction it's fine as well :
Code:
b.getCrew().get( 0 ).getShoes();
Do I miss something ?