Hi All,
I'm trying to get started with Hibernate and JPA. I want to create a ManyToOne relationship between two Java objects. I'm not using any configuration XML files. I'm doing this all programmatically.
I'm using the following code for configuring my Hibernate:
Code:
AnnotationConfiguration configuration = new AnnotationConfiguration();
configuration.setProperty("hibernate.connection.driver_class", "com.mysql.jdbc.Driver");
configuration.setProperty("hibernate.connection.url", "jdbc:mysql://localhost:3306/product");
configuration.setProperty("hibernate.connection.username", "root");
configuration.setProperty("hibernate.connection.password", "test");
configuration.setProperty("hibernate.connection.dialect", "org.hibernate.dialect.MySQLDialect");
configuration.setProperty("hibernate.hbm2ddl.auto", "create"); // create tables when not exist
configuration.addAnnotatedClass(nl.product.core.cms.domain.ChildImpl.class);
configuration.addAnnotatedClass(nl.product.core.cms.domain.ParentImpl.class);
configuration.buildSessionFactory();
Here's my code:
Parent class:
Code:
@Entity
@Table(name = "parents")
public class ParentImpl implements Parent {
// The id
@Id
@GeneratedValue
@Column(name = "id")
private Integer myId;
// The children
@JoinTable(name = "children", joinColumns = { @JoinColumn(name = "parent_id") })
@OneToMany(targetEntity=nl.product.core.cms.domain.ChildImpl.class, mappedBy = "myParent", cascade = {CascadeType.ALL})
private List<Child> myChildren;
/**
* {@inheritDoc}
*/
public Integer getId() {
return myId;
}
/**
* {@inheritDoc}
*/
public void set(Integer id) {
myId = id;
}
/**
* {@inheritDoc}
*/
public List<Child> getChildren() {
return myChildren;
}
/**
* {@inheritDoc}
*/
public void setChildren(List<Child> children) {
myChildren = children;
}
}
Then my Child class:
Code:
@Entity
@Table(name = "children")
public class ChildImpl implements Child {
// The ID for this website
@Id
@GeneratedValue
@Column(name = "id")
private Integer myId;
// The parent
@JoinColumn(name = "parent_id", nullable = false)
@ManyToOne(targetEntity=nl.product.core.cms.domain.ParentImpl.class)
private Parent myParent;
/**
* {@inheritDoc}
*/
public Integer getId() {
return myId;
}
/**
* {@inheritDoc}
*/
public void set(Integer id) {
myId = id;
}
/**
* {@inheritDoc}
*/
public Parent getParent() {
return myParent;
}
/**
* {@inheritDoc}
*/
public void setParent(Parent parent) {
myParent= parent;
}
}
Then I try to create some data:
Code:
Session session = getSessionFactory().openSession();
Transaction tx = session.beginTransaction();
Parent parent = new ParentImpl();
Child child = new ChildImpl();
List<Child> list = new ArrayList<Child>();
list.add(child);
parent.setChildren(list);
session.persist(parent);
tx.commit();
session.close();
When I execute this I get the following error:
Exception in thread "main" org.hibernate.PropertyValueException: not-null property references a null or transient value: nl.product.core.cms.domain.ChildImpl.myParent
It looks like the parent inside the child is empty. When I add the following before persisting:
Code:
child.setParent(parent)
I get the following error:
Caused by: com.mysql.jdbc.exceptions.MySQLIntegrityConstraintViolationException: Cannot add or update a child row: a foreign key constraint fails (`product/children`, CONSTRAINT `FKD642333853899F2E` FOREIGN KEY (`id`) REFERENCES `children` (`id`))
This looks like a FK referencing to itself?
Can someone help me with this?