Hibernate version: 3.2.6.ga
Hibernate entitymanager version: 3.3.1.ga
Hibernate annotations version: 3.3.0.ga
Mapping documents:
Hello everybody,
Here is my problem :
I have 2 entities : Parent and Child with a OneToMany (bi-directional)
In the parent class i declare a @PrePersist callback method that throw an RunTimeException when the number of childs is
little than 2 or if the reference of the list is
null.
In a testng class, I create a new object Parent and add it 2 childs[/b].
When I execute em.merge on the parent, the runtime exception is throwed by the PrePersist callback method.
=> the reference of the childs' list is
null !!.
My question : "Is It normal ?"
Here is the Parent class :
Code:
package be.philou.test;
import java.io.Serializable;
import java.util.List;
import javax.persistence.CascadeType;
import javax.persistence.Entity;
import javax.persistence.Id;
import javax.persistence.OneToMany;
import javax.persistence.PrePersist;
import javax.persistence.PreUpdate;
@Entity
public class Parent implements Serializable {
private static final long serialVersionUID = 4076893988337190107L;
private int id;
private String name;
private String firstName;
private List<Child> childs;
// -------------------------------------------------------------------------
// getters & setters
// -------------------------------------------------------------------------
@Id
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getFirstName() {
return firstName;
}
public void setFirstName(String firstName) {
this.firstName = firstName;
}
@OneToMany(mappedBy="parent", cascade=CascadeType.ALL)
public List<Child> getChilds() {
return childs;
}
public void setChilds(List<Child> childs) {
this.childs = childs;
}
// -------------------------------------------------------------------------
// callbacks
// -------------------------------------------------------------------------
@PrePersist
@PreUpdate
public void beforeCreate() {
// Parent must have minimum 2 childs
if (getChilds() == null || getChilds().size() < 2) {
throw new RuntimeException("Parent must have minimum 2 childs");
}
}
}
Here is the Child class :
Code:
package be.philou.test;
import java.io.Serializable;
import javax.persistence.Entity;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.ManyToOne;
@Entity
public class Child implements Serializable {
private static final long serialVersionUID = 5120375761727811864L;
private int id;
private String info;
private Parent parent;
// -------------------------------------------------------------------------
// getters & setters
// -------------------------------------------------------------------------
@Id
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getInfo() {
return info;
}
public void setInfo(String info) {
this.info = info;
}
@ManyToOne
@JoinColumn(name="FK_PARENT")
public Parent getParent() {
return parent;
}
public void setParent(Parent parent) {
this.parent = parent;
}
}