简体   繁体   English

保存具有休眠子关系的Hibernate实体失败,并出现外键错误

[英]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): 和子实体看起来像(在数据库中,它在Parents表中的parent_id字段上具有外键):

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 创建父对象和childAssignments对象后,我将childAssignment添加到父对象

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? 预期的结果是使用ID保存新的父项,然后使用该ID保存子项,但是在保存时似乎休眠状态不知道parent_id,我应该如何建立关联使其起作用? or some annotations on parent_id field? 或对parent_id字段的一些注释?

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, 我试图删除childAssignment实体上的parent_id或将其设置为@Transient,并且出现新错误,无法将NULL插入表中,很明显,Hibernate尝试插入parent_id,但未填充它,

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 我解决了更改childAssignment实体的问题

@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. 如果指定双向关系,则必须在Java代码中分配双方,就像纯Java一样。

Secondly, your declaration of a 'parentId' column is redundant. 其次,您对“ parentId”列的声明是多余的。 Hibernate will create that for you automatically. Hibernate会自动为您创建。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 无法添加或更新子行:尝试保存实体及其关系实体时出现外键约束失败错误 - Cannot add or update a child row: a foreign key constraint fails error when trying to save an entity and its relational entity 无法保存实体“外键约束失败” - Unable to save an entity "a foreign key constraint fails " 具有外键关系的Hibernate子类 - Hibernate subclass with foreign key relationships 错误:无法添加或更新子行:使用mysql在hibernate中出现外键约束失败 - ERROR: Cannot add or update a child row: a foreign key constraint fails in hibernate using mysql 休眠级联保存错误(无法添加或更新子行:外键约束失败) - Hibernate cascade saving error (Cannot add or update a child row: a foreign key constraint fails) 无法添加或更新子行:外键约束失败Hibernate - Cannot add or update a child row: a foreign key constraint fails Hibernate 无法添加或更新子行:外键约束在休眠状态下失败 - Cannot add or update a child row: a foreign key constraint fails in hibernate 休眠:无法添加或更新子行:外键约束失败 - Hibernate:Cannot add or update a child row: a foreign key constraint fails 无法添加或更新子行:外键约束失败 - Hibernate - Cannot add or update a child row: a foreign key constraint fails - Hibernate 当我使用 hibernate 保存实体时,外键不会始终保存为 null - When I save an Entity with hibernate the foreign key is not saved always an null
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM