0

I am getting violated - parent key not found while trying to save Hibernate Entity

I have parent entity:

@Entity
@Table(name = "parents")
public class Parent implements Serializable {
    private static final long serialVersionUID = 1246376778314918671L;

    @Id
    @GeneratedValue(strategy = GenerationType.SEQUENCE, generator = "seq")
    @SequenceGenerator(name = "seq", sequenceName = "PARENT_ID_SEQ", allocationSize = 1)
    @Column(name = "parent_id")
    private long parentId;

    @Column(name = "display_name")
    @Size(min = 1, max = 128)
    @NotBlank   
    private String displayName;

    @JsonManagedReference("childAssignments")
    @OneToMany(mappedBy = "parent", fetch = FetchType.LAZY, cascade = CascadeType.ALL, orphanRemoval=true)
    private Set<ChildAssignment> childAssignments = new HashSet<ChildAssignment>(0);

    //regular getters and setters here
}

and child entity looks like (in database it has foreign key on parent_id field from parents table):

Entity
@Table(name = "child_assignments")
public class ChildAssignment implements Serializable {
    private static final long serialVersionUID = -5949955576511639261L;

    @Id
    @GeneratedValue(strategy = GenerationType.SEQUENCE, generator = "seq")
    @SequenceGenerator(name = "seq", sequenceName = "CHILD_ASSIGNMENT_ID_SEQ", allocationSize = 1)
    @Column(name = "child_assignment_id")
    private long childAssignmentId;

    @Column(name = "parent_id")  
    private long parentId;   // getting error because after creating new parent it has not been set

    @Column(name = "site_id")
    private long siteId;

    @ManyToOne
    @JoinColumn(name = "parent_id", insertable = false, updatable = false)
    private Parent parent;

    // regular getters and setters here

}

After parent object and childAssignments object have been created I am adding childAssignment to parent

ChildAssignment ca = new ChildAssignment();
ca.setSiteId(1);
// I do not set parent_id here since I do not know it and expecting Hibernate to figure it out

parent.getChildAssignments().add(childAssignment);
session.save(parent);

Expected result is to save new parent entry with ID and after use this id to save child but seems like hibernate does not know about parent_id at the time of saving, how should I build my association to make it work? or some annotations on parent_id field?

UPDATED

I tried to remove parent_id or set it to @Transient on childAssignment entity, and get new error cannot insert NULL into table, it's obvious that Hibernate is trying to insert parent_id but do not populate it,

setting parent on a child does not help either

ChildAssignment ca = new ChildAssignment();
   ca.setSiteId(1);
   ca.setParent(parent)

   parent.getChildAssignments().add(childAssignment);
   session.save(parent);

What I am missing?

SOLVED

I solved a problem changing my childAssignment entity

@Transient -- add transient (just to have it)
@Column(name = parent_id", insertable = false, updatable = false) -- add insertable and updatable  both equals to false
private parentId; 


.....

 @ManyToOne
 @JoinColumn(name = "parent_id") -- remove  insertable = false, updatable = false options
 private Parent parent;
3
  • Please check example in stackoverflow.com/questions/11961071/…
    – JagKum
    Aug 27, 2014 at 0:14
  • how it would look like with my child entity class?
    – antohoho
    Aug 27, 2014 at 0:41
  • I do not think I have right solution in provided example, I should not change any DB settings to accomplish it.
    – antohoho
    Aug 27, 2014 at 16:07

1 Answer 1

0

You need to do:

ca.setParent(parent);

as well.

If you specify a two sided relationship then you must assign both sides in Java code, just as you would if it was pure Java.

Secondly, your declaration of a 'parentId' column is redundant. Hibernate will create that for you automatically.

4
  • Hiya, this may well solve the problem... but it'd be good if you could provide a little explanation about how and why it works :) Don't forget - there are heaps of newbies on Stack overflow, and they could learn a thing or two from your expertise - what's obvious to you might not be so to them.
    – Taryn East
    Aug 27, 2014 at 0:29
  • no, it did not ca.setNotification(parent); parent.getchildAssignments().add(ca);
    – antohoho
    Aug 27, 2014 at 0:33
  • the problem is child parent_id has not been set, how would it know how to set it?
    – antohoho
    Aug 27, 2014 at 16:04
  • It knows how to set it because you should call ca.setParent(parent). The ability to do this is part of the Hibernate/JPA raison d'être. You should almost never be concerned with key values, particularly assigning them to a property.
    – Steve C
    Aug 29, 2014 at 2:03

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.