I'v searched the web, but I did not find any hint for my problem. Assuming we have two classes
@Entity
@Table(name="childTable"
public class Child{
@Id
@GeneratedValue(strategy=GenerationType.AUTO)
@Column(name="child_id", unique=true, nullable=false)
public String id;
@ManyToOne(fetch = FetchType.LAZY, optional=false)
@JoinColumn(name="child2parent", insertable=false, updatable=false)
public Parent parent;
// some other data
// getter and setter
}
And:
@Entity
@Table(name="parentTable"
public class Parent{
@Id
@GeneratedValue(strategy=GenerationType.AUTO)
@Column(name="parent_id", unique=true, nullable=false)
public String id;
@OneToMany(fetch=FetchType.LAZY, cascade=CascadeType.ALL, mappedBy="parent")
private Set<Child> children;
// some other data
// getter and setter
}
I'm trying to insert the Child into a local h2 repository but as I try to save it, a contraint violation exception is thrown: "NULL not allowed for column "PARENT_ID";
The parent is set via setter and has an id. I also tried to add the child to the set of children and save it to the repository. But saving the child throws the same error.
Can anybody help me? I hope it is enough code to understand my problem.
[Q&A]
I've tried to persist the parent before the child. Additionally the parent was already stored and the model class is not empty.
I've tried to remove insertable / updateable, but there is not effect
Here's the code :
// parent already present in DB
Child child = new Child();
child.setParent(parent);
childRepository.save(child);
I've tried to save the parent using the cascade to save the children, but the system still tells me that the id of my parent is null. I also tried to change the code according to How do I use annotations to define different types of relationships in Hibernate 4 and Spring? but there is no effect.
[EDIT] I've deleted the Fetchtype and the optional flag. But now the system logs that the referential integrity is violated.
[SOLVED] It was an undeleted column. This thread can be deleted.