【问题标题】:JPA child doesn't persist when parent persist method is called调用父持久方法时,JPA子不持久
【发布时间】:2013-05-20 18:27:41
【问题描述】:

在调用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;
}

}

子表

@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;
}

}

持久化代码

 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();

openJPA 产生的错误,我使用的是 DB2

原因:org.apache.openjpa.lib.jdbc.ReportingSQLException:DB2 SQL 错误:SQLCODE=-530,SQLSTATE=23503, SQLERRMC=ADMINISTRATOR.Z_CHILD.CC1369070983676,驱动程序=3.58.81 {prepstmnt 864433030 插入 Z_CHILD(id、NAME、PARENT_ID) 值 (?, ?, ?) [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) 在 org.apache.openjpa.lib.jdbc.LoggingConnectionDecorator$LoggingConnection$LoggingPreparedStatement.executeUpdate(LoggingConnectionDecorator.java:1199)

【问题讨论】:

  • 您需要用英语多解释一下您要做什么。代码格式很好,但是这个问题看不顺眼,因为没有解释。
  • 调用 em.persist(parent) 时,该子实体不会与父实体一起持久化

标签: java jpa db2 openjpa entities


【解决方案1】:

SQL代码:-530 FOREIGN KEY 约束名称的插入或更新值无效

您必须了解,生成的 ID 是在 post persist 阶段的实体实例中设置的。 JPA 还不能将生成的父 ID 传播给子 ID。所以必须自己设置

 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();

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2010-12-25
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-03-24
    • 1970-01-01
    • 1970-01-01
    • 2021-08-01
    相关资源
    最近更新 更多