Reputation: 548
I have a table master table user ,topics table and comments table where in for a single topic there can be multiple comments
user table will be already populated. I will get a post request to save the topic with structure like below
{
"topicId":"T001",
"title":"stackoverflow",
"commentBeans":[
{
"comment":"developer platform"
},
{
"comment":"developer communtiy"
}
]
}
Frameworks used: spring boot JPA DB : postgressql
I am able to save the data the traditional way (i.e get the request and save topic bean first. get the primarykey from saved entity and loop the list of commentbean where user num will be set dynamically by another get service and save them)
I wanted to know if there is anyway to save the data with single save query.
@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;
}
I saw the below link and am little worried if am doing the wrong way. any help is appreciated.
https://thoughts-on-java.org/hibernate-tips-how-to-map-an-entity-to-multiple-tables/
Upvotes: 1
Views: 4978
Reputation: 13727
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"
}
}*/
}
Upvotes: 2