![](/img/trans.png)
[英]Spring Data Jpa OneToMany save child and parent entities at the same time?
[英]How to save data-from-parent-to-child-tables-based-on-entities-in-spring-jpa
我有一个表主表用户、主题表和评论表,其中对于单个主题可以有多个评论
用户表将已经被填充。 我将收到一个帖子请求,以使用如下结构保存主题
{
"topicId":"T001",
"title":"stackoverflow",
"commentBeans":[
{
"comment":"developer platform"
},
{
"comment":"developer communtiy"
}
]
}
使用的框架:spring boot JPA DB:postgressql
我能够以传统方式保存数据(即首先获取请求并保存主题 bean。从保存的实体中获取主键并循环评论 bean 列表,其中用户 num 将由另一个获取服务动态设置并保存它们)
我想知道是否可以使用单个保存查询来保存数据。
@Entity
@Table(name ="user")
public class User implements Serializable {
@Id
@Column(name = "user_num")
@GeneratedValue(strategy = GenerationType.AUTO)
private Long userNum;
@Column(name = "user_id")
private String userId;
@Column(name = "username")
private String userName;
}
@Entity
@Table(name = "topics")
public class TopicBean implements Serializable {
@Id
@Column(name = "topic_num")
@GeneratedValue(strategy = GenerationType.AUTO)
private Long topicNum;
@Column(name = "topicId")
private String topicId;
@Column(name = "title")
private String title;
@OneToMany(mappedBy="topicBean")
private List<CommentBean> commentBeans;
}
@Entity
@Table(name = "comments")
public class CommentBean implements Serializable {
@EmbeddedId
private CommentBeanKey key;
@Column(name = "comment")
private string comment;
@ManyToOne
@JoinColumn(name="topic_num")
private TopicBean topicBean;
@ManyToOne
@JoinColumn(name="user_num")
private TopicBean topicBean;
}
@Embeddable
public class CommentBeanKey implements Serializable{
private static final long serialVersionUID = 5889249943605061539L;
@Column(name ="topic_num")
private Long topicNum;
@Column(name ="user_num")
private Long userNum;
}
我看到了下面的链接,如果我做错了,我一点也不担心。 任何帮助表示赞赏。
https://thoughts-on-java.org/hibernate-tips-how-to-map-an-entity-to-multiple-tables/
父程序
@Entity
@Table(name = "parent")
@Getter
@Setter
@AllArgsConstructor
@NoArgsConstructor
@ToString
public class Parent {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private int parentId;
private String name;
@OneToMany(mappedBy="parent",fetch=FetchType.LAZY,cascade = CascadeType.PERSIST)
private List<Child> child = new ArrayList<Child>();
}
子程序
@Entity
@Table(name = "child")
@Getter
@Setter
@AllArgsConstructor
@NoArgsConstructor
@ToString
public class Child {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private int childId;
private String account;
@ManyToOne(fetch = FetchType.LAZY, targetEntity = Parent.class)
@JoinColumn(name="parentId", referencedColumnName = "parentId", nullable = false)
private Parent parent;
}
控制器.java
//save Child with Parent at same
@PostMapping(value = "/onetomany")
public String OneToMany(@RequestBody Parent parent)
{
System.out.println("Parent: "+parent.toString());
for (Child child : parent.getChild()) {
child.setParent(parent);
}
parent.setChild(parent.getChild());
parentRepository.save(parent);
return "saved";
/*{
"name":"Romil",
"child":[
{"account":"1"},
{"account":"2"}
]
}*/
}
//save Child with Parent's ID
@PostMapping(value = "/onetomanyPID")
public String OneToMany(@RequestBody Child child)
{
child.setParent(child.getParent());
childRepository.save(child);
return "saved";
/*{
"account":"3",
"parent":{
"parentId":"1",
"name":"Romil"
}
}*/
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.