简体   繁体   中英

Save Hibernate entity with parend child relationships fails with foreign key error

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;

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM