Need help with Hibernate? Read this first:
http://www.hibernate.org/ForumMailingli ... AskForHelp
Hibernate version:
Mapping documents: Annotation
Name and version of the database you are using: MySQL 5.0.x
Hi Guy,
I have a basic problem in bidirectional one-to-many association for transitive persistence when both parent and child are new object.
I have following classes:
Parent Code:
Code:
@Entity
public class Parent implements java.io.Serializable
{
@Id
@GeneratedValue
private Long id;
@oneToMany(mappedBy = "parent", fetch = FetchType.EAGER, cascade = javax.persistence.CascadeType.ALL)
private Set<Child> children = new HashSet<Child>();
public Long getId() {return id;}
public void setId(Long id) {this.id = id;}
public Set<Child> getChildren() {return children;}
public void setChildren(Set<Child> children) {this.children = children;}
public void addChild(Child c) {
c.setParent(this);
children.add(c);
}
....
}
And the Child class looks like this:
Code:
@Entity
public class Child implements java.io.Serializable
{
@Id
private String id;
@ManyToOne
@JoinColumn(name="parentId", insertable=false, updatable=false, nullable = false )
private Parent parent ;
@OneToOne //unidirectional oneToone
private Item item;
public Long getId() {return id;}
public void setId(String id) {this.id = id;}
public Parent getParent() {return parent;}
public void setParent(Parent parent) {this.parent = parent;}
public Item getItem() {return this.item;}
public void setItem(Item item) {this.item = item;}
....
}
Please, note that Item is an Entity with no reference to the Child object. Since the relation is only unidirectional and hold by the Child class.
In my Facet class, I am then doing something like this:
//create a new transient Parent
Parent parent = new Parent();
//create a new transient Child
Child child = new Child();
//Load an aleady persistent Item
Item item = itemManager.findItem(itemId);
child.setItem(item); // I am wondering what happens here, does child try to go automatically to persist state???
//add to parent, save parent and expecte to have a transitive persistence
parent.addChild(child);
manager.saveParent(parent);
//manager.saveParent calls a dao instance which performs entityManager.merge(parent)
Please note that the FlushMode is AUTO.
During the flush I am getting something like following errors:
Caused by: java.sql.SQLException: Field 'parentId' doesn't have a default value .....
I don't understand this error, since I am trying to do the transitive persistence throw the Parent/Child. I was expected a cacade merge.
I will appreciate any suggestion to make me understand what I am missing.
Thx