Adam Lesiak
Adam Lesiak

Reputation: 501

Hibernate OneToMany in one Entity

I try to create relationship in one entity (OneToMany in one table, parent - children relation) and I have a class:

public class MenuItem {
@Id
@GeneratedValue
private long Id;
private String content;
private String name;

@ManyToOne(cascade = { CascadeType.ALL })
@JoinColumn(name="parent")
private MenuItem parent;

@OneToMany(cascade = CascadeType.ALL, mappedBy="parent")
private Set<MenuItem> childrens  = new HashSet<MenuItem>();

//getters and setters

}

Loading data works good but when I try to save MenuItem, i have following error:

"Field 'childrens_id' doesn't have a default value"

Hibernate tries save in MenuItem correst values (I see it in logs), but, I think, its try to create another, Join table and finnaly transaction is failed.

This is exacly shows at: http://www.roseindia.net/hibernate/hibernate4/OnetoManySelfJoin.shtml but not working. I used Hibernate 4.1.1 and Tomcat server 7.

Solved: Drop and create database again.

Upvotes: 1

Views: 425

Answers (1)

rapha&#235;λ
rapha&#235;λ

Reputation: 6523

guess best thing is to drop you database and run again. It seems you have hibernate/jpa impl setup to generate your database scheme, but the code and tables got too much out of synch. There is probably a field "dangling" in your table.

As a side note: "Wild" guess: you had @JoinColumn(name="childrens_id") as a try before you changed it to the code above? (being confused with mappedBy values?)

Upvotes: 1

Related Questions