gpt4 book ai didi

java - Spring Data JPA 通过从父实体获取 id 来插入子实体和父实体

转载 作者:塔克拉玛干 更新时间:2023-11-03 05:17:26 24 4
gpt4 key购买 nike

我想通过对父实体调用 save 将父实体和子实体一起保存到 MySQL 数据库中。 Parent 和 Child 实体之间存在一对一的映射。父 ID 是自动生成的,我们也需要在 child 中使用与 child 的 pk 相同的 ID。

我正在使用 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)

这是我的数据库架构:

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;

这是我的 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
}

子实体:

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

这是我的主要方法

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

最佳答案

您应该在两个对象中都使用级联。试试这个:

@Entity(name="parent")
public class Parent implements Serializable {
//...
@OneToOne(mappedBy="parent",cascade=CascadeType.ALL)
private Child child;
//...
}

@Entity(name="child")
public class Child implements Serializable {
//...
@OneToOne(optional = false, fetch = FetchType.LAZY, cascade = CascadeType.REMOVE)
@JoinColumn(name="cid", referencedColumnName = "id")
private Parent parent;
//...
}

关于java - Spring Data JPA 通过从父实体获取 id 来插入子实体和父实体,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25490774/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com