简体   繁体   English

调用父persist方法时,JPA子节点不会持久存在

[英]JPA child doesn't persist when parent persist method is called

This child entity is not persisted along with the parent when em.persist is called 调用em.persist时,此子实体不会与父项一起em.persist

@Entity
@Table(name="Z_PARENT")
public class Parent {

@Id
 @TableGenerator(name="parentIDGen", table="Z_JPA_ID_GEN",
 pkColumnName="ID_STRING", valueColumnName="ID_GEN",
 pkColumnValue="Z_PARENT")
 @GeneratedValue(strategy=GenerationType.TABLE, generator="parentIDGen")
private int id;

@Column(name="NAME")
private String name;


@OneToMany(mappedBy="parent",cascade={CascadeType.ALL})
Set<Child> children;

public int getId() {
    return id;
}

public void setId(int id) {
    this.id = id;
}

public String getName() {
    return name;
}

public void setName(String name) {
    this.name = name;
}



@Override
public int hashCode() {
......
}

@Override
public boolean equals(Object obj) {
........
}

public Set<Child> getChildren() {
    return children;
}

public void setChildren(Set<Child> children) {
    this.children = children;
}

}

Child Table 儿童表

@Entity
@Table(name="Z_CHILD")
public class Child {

@Id
@TableGenerator(name="childIDGen", table="Z_JPA_ID_GEN",
pkColumnName="ID_STRING", valueColumnName="ID_GEN",
pkColumnValue="Z_CHILD")
@GeneratedValue(strategy=GenerationType.TABLE,generator="childIDGen")
private int id;

@Column(name="NAME")
private String name;

@Column(name="PARENT_ID")
private int parentID;

@ManyToOne
@JoinColumn(name="PARENT_ID",referencedColumnName="ID")
private Parent parent;

public int getId() {
    return id;
}

public void setId(int id) {
    this.id = id;
}

public String getName() {
    return name;
}

public void setName(String name) {
    this.name = name;
}

public int getParentID() {
    return parentID;
}

public void setParentID(int parentID) {
    this.parentID = parentID;
}

@Override
public int hashCode() {
.....
}

@Override
public boolean equals(Object obj) {
.....
}

public Parent getParent() {
    return parent;
}

public void setParent(Parent parent) {
    this.parent = parent;
}

}

persisting code 坚持代码

 em.getTransaction().begin();
 Parent parent= new Parent();
 parent.setName("Parent1");
 Set<Child> children=new HashSet<Child>();
 Child child1=new Child();
 child1.setName("Child1");
 children.add(child1);
 parent.setChildren(children);
 em.persist(parent);
 em.getTransaction().commit();

Error generated by openJPA, I am using DB2 openJPA生成的错误,我正在使用DB2

Caused by: org.apache.openjpa.lib.jdbc.ReportingSQLException: DB2 SQL Error: SQLCODE=-530, SQLSTATE=23503, SQLERRMC=ADMINISTRATOR.Z_CHILD.CC1369070983676, DRIVER=3.58.81 {prepstmnt 864433030 INSERT INTO Z_CHILD (id, NAME, PARENT_ID) VALUES (?, ?, ?) [params=(int) 450, (String) Child1, (int) 0]} [code=-530, state=23503] at org.apache.openjpa.lib.jdbc.LoggingConnectionDecorator.wrap(LoggingConnectionDecorator.java:281) at org.apache.openjpa.lib.jdbc.LoggingConnectionDecorator.wrap(LoggingConnectionDecorator.java:257) at org.apache.openjpa.lib.jdbc.LoggingConnectionDecorator.access$1000(LoggingConnectionDecorator.java:72) at org.apache.openjpa.lib.jdbc.LoggingConnectionDecorator$LoggingConnection$LoggingPreparedStatement.executeUpdate(LoggingConnectionDecorator.java:1199) org.apache.openjpa.lib.jdbc.ReportingSQLException:DB2 SQL错误:通过引起SQLCODE = -530,SQLSTATE = 23503,则sqlerrmc = ADMINISTRATOR.Z_CHILD.CC1369070983676,DRIVER = 3.58.81 {prepstmnt 864433030 INSERT INTO Z_CHILD(ID, NAME,PARENT_ID)VALUES(?,?,?)[params =(int)450,(String)Child1,(int)0]} [code = -530,state = 23503] at org.apache.openjpa.lib。 jdbc.LoggingConnectionDecorator.wrap(LoggingConnectionDecorator.java:281)在org.apache.openjpa.lib.jdbc.LoggingConnectionDecorator.wrap(LoggingConnectionDecorator.java:257)在org.apache.openjpa.lib.jdbc.LoggingConnectionDecorator.access $ 1000(LoggingConnectionDecorator .java:72)at org.apache.openjpa.lib.jdbc.LoggingConnectionDecorator $ LoggingConnection $ LoggingPreparedStatement.executeUpdate(LoggingConnectionDecorator.java:1199)

SQLCODE : -530 THE INSERT OR UPDATE VALUE OF FOREIGN KEY constraint-name IS INVALID SQLCODE:-530 FOREIGN KEY约束名称的INSERT或UPDATE值为IS INVALID

You must understand that the generated ID is set in the entity instance in the post persist phase. 您必须了解生成的ID是在post persist阶段的实体实例中设置的。 JPA is not yet able to propagate the generated parent ID to the children. JPA尚无法将生成的父ID传播给子项。 So you must set it yourself 所以你必须自己设置它

 em.getTransaction().begin();
 Parent parent= new Parent();
 parent.setName("Parent1");
 em.persist(parent); // here parent id is valuated
 Set<Child> children=new HashSet<Child>();
 Child child1=new Child();
 child1.setName("Child1");
 child1.setParentID(parent.getId());
 children.add(child1);
 parent.setChildren(children);
 em.merge(parent);
 em.getTransaction().commit();

暂无
暂无

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

相关问题 Spring JPA如何在子项在多对多关系中持久化时持久化父级 - Spring JPA how to persist parent when the child is persisted in ManyToMany relationship JPA OneToOne 关系在保存父实体时不会保留子实体 - JPA OneToOne relation doesnt persist child entity when saving parent SpringBoot JPA 使用空列持久化父级的子级 - SpringBoot JPA persist child of parent with nulls columns JPA EntityManager persist方法不将实体保存到db,但实体在持久化后具有id - JPA EntityManager persist method doesn't save entity to db, but entity has id after persist 在View上调用clearAnimation()时,动画转换不会在View上持续存在 - Animation Transformation doesn't persist on View when clearAnimation() called on View JPA / Hibernate级联持续不起作用 - JPA/Hibernate cascading persist doesn't work 使用方法时Arraylist不会保留数据 - Arraylist doesn't persist data when using a method 当子实体与父实体之间具有一对一的JPA关系时,可以持久保留子实体和父实体吗? - Is it ok to persist the Child entity along with the Parent entity when having a One-To-One JPA relationship in between them? 当使用 JPA 保持父级时如何防止保存子级 object?TransientPropertyValueException 报告 - How to prevent saving child object when persist parent with JPA?TransientPropertyValueException reported Hibernate JPA持久保存具有空父ID的子实体 - Hibernate JPA persist saved child entity with null parent id
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM