0

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.

Community
  • 1
  • 1
Mr.Mountain
  • 863
  • 8
  • 32
  • 1
    Are you trying to save the `Parent` or the `Child` object? From what I can see, you're trying to save the `Child` object BEFORE saving the `Parent` object (which explains the `null not allowed for column PARENT_ID`). Try persisting the `Parent` first, and then the `Child`. – rion18 Jun 16 '14 at 20:16
  • 1
    Can you please show the code where you have the logic to save the child entity? – Zeus Jun 16 '14 at 22:25
  • Remove the insertable=false, updatable = false and try it once. – Zeus Jun 16 '14 at 22:34
  • Double check your classes against the example I have written here: http://stackoverflow.com/questions/24257449/how-do-i-use-annotations-to-define-x-relationship-in-hibernate-4-and-spring – JamesENL Jun 18 '14 at 07:54
  • Ok - i will do that and give feedback. Thank you. – Mr.Mountain Jun 18 '14 at 07:57
  • There's your problem, you need to be adding the child to the parent and saving the parent. The child will be created by cascade. – JamesENL Jun 18 '14 at 07:58

0 Answers0