gpt4 book ai didi

java - 如何在spring-jpa中保存基于实体的父表到子表的数据

转载 作者:行者123 更新时间:2023-11-30 05:32:41 25 4
gpt4 key购买 nike

我有一个表主表用户,主题表和评论表其中单个主题可以有多个评论

用户表将已填充。我将收到一个帖子请求,以保存具有如下结构的主题

{  
"topicId":"T001",
"title":"stackoverflow",
"commentBeans":[
{
"comment":"developer platform"
},

{
"comment":"developer communtiy"
}
]
}

使用的框架: Spring Boot 日本PA数据库:postgressql

我能够以传统方式保存数据(即首先获取请求并保存主题 bean。从保存的实体中获取主键并循环 commentbean 列表,其中用户 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/

最佳答案

Parent.java

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

Child.java

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

}

Controller.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"
}
}*/
}

关于java - 如何在spring-jpa中保存基于实体的父表到子表的数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57232700/

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