Spring Data JPA通过从父级获取id与父级实体一起插入子级

Man*_*hak 6 java hibernate parent-child jpa-2.0 spring-data-jpa

我想通过调用父对象保存将父实体和子实体一起保存到MySQL数据库中.父实体和子实体之间存在一对一的映射.父ID是自动生成的,我们需要在子项中使用它作为子项的pk.

我使用的是Spring Data JPA 2.0(JPA提供程序是Hibernate)和Spring MVC框架.当试图插入实体时,我收到以下错误.

根本原因

org.springframework.dao.DataIntegrityViolationException: not-null property references a null or transient value: com.serro.cbmapi.model.Child.parent; nested exception is org.hibernate.PropertyValueException: not-null property references a null or transient value: com.serro.cbmapi.model.Child.parent org.springframework.orm.jpa.vendor.HibernateJpaDialect.convertHibernateAccessException(HibernateJpaDialect.java:207)
Run Code Online (Sandbox Code Playgroud)

这是我的数据库架构:

Parent Table:
   CREATE TABLE `parent` (
   `pid` int(11) NOT NULL AUTO_INCREMENT,
   `parent_name` varchar(256) CHARACTER SET utf8 COLLATE utf8_bin DEFAULT NULL,
    PRIMARY KEY (`pid`)    ) ENGINE=InnoDB AUTO_INCREMENT=13 DEFAULT CHARSET=latin1;

    ) ENGINE=InnoDB DEFAULT CHARSET=latin1;

Child Table:
    CREATE TABLE `child` (
    `cid` int(11) NOT NULL,
    `child_name` varchar(256) CHARACTER SET utf8 COLLATE utf8_bin DEFAULT NULL,
    PRIMARY KEY (`cid`),
    CONSTRAINT `child_f1` FOREIGN KEY (`cid`) REFERENCES `parent` (`pid`)

    ) ENGINE=InnoDB DEFAULT CHARSET=latin1;
Run Code Online (Sandbox Code Playgroud)

这是我的Java实体父实体:

@Entity(name="parent")
@NamedQuery(name="Parent.findAll", query="SELECT p FROM parent p")
public class Parent implements Serializable {
    private static final long serialVersionUID = 1L;

    @Id
    @GeneratedValue
    private int pid;

    @Column(name="parent_name")
    private String parentName;

    //bi-directional one-to-one association to Child
    @OneToOne(mappedBy="parent",cascade=CascadeType.ALL)
    private Child child;

    //getter, setters
}
Run Code Online (Sandbox Code Playgroud)

儿童恩惠:

@Entity(name="child")
@NamedQuery(name="Child.findAll", query="SELECT c FROM child c")
public class Child implements Serializable {
    private static final long serialVersionUID = 1L;

    @Id
    private int cid;

    @Column(name="child_name")
    private String childName;

    //bi-directional one-to-one association to Parent
    @OneToOne(fetch = FetchType.LAZY)
    @JoinColumn(name="cid")
    @MapsId("cid")
    private Parent parent;

    //getter, setters
}
Run Code Online (Sandbox Code Playgroud)

这是我的主要方法

AbstractApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml");  
ParentRepository parentResp = context.getBean(ParentRepository.class);  
Parent parent = new Parent();
parent.setParentName("Parent1");
Child child = new Child();
child.setChildName("Child1");
parent.setChild(child);
parentResp.save(parent);
Run Code Online (Sandbox Code Playgroud)

Cha*_*nya 2

例外情况

not-null property references a null or transient value: com.serro.cbmapi.model.Child.parent; nested exception is org.hibernate.PropertyValueException: not-null property references a null or transient value: com.serro.cbmapi.model.Child.parent
Run Code Online (Sandbox Code Playgroud)

parent你的Child对象中的属性是null.


因此,要解决此问题,您需要添加以下代码行:

child.setParent(parent);
Run Code Online (Sandbox Code Playgroud)

另根据JPA 文档

指定为 EmbeddedId 主键、EmbeddedId 主键内的属性或父实体的简单主键提供映射的多对一或一对一关系属性。value 元素指定关系属性对应的复合键内的属性。 如果实体的主键与关系引用的实体的主键具有相同的 Java 类型,则不指定 value 属性。

parent在类中的字段Child应该在没有value属性的情况下声明@MapsId

@OneToOne(fetch = FetchType.LAZY)
@JoinColumn(name="cid")
@MapsId
private Parent parent;
Run Code Online (Sandbox Code Playgroud)