one class
@Entity
public class Shuo{
@Id
@GeneratedValue
private int id;
private String title;
private Date date;
String content;
@ManyToOne(cascade = CascadeType.REFRESH)
@JoinColumn(name = "user_id")//
private User user;
// : /
@OneToMany(mappedBy = "shuo",cascade = CascadeType.PERSIST)
List<ShuoImg> imgs;
public List<ShuoImg> getImgs() {
return imgs;
}
public void setImgs(List<ShuoImg> imgs) {
for (ShuoImg img : imgs) {
img.setShuo(this);
}
this.imgs = imgs;
}
//set get
many class
@Entity
public class ShuoImg {
@Id
@GeneratedValue
int id;
@ManyToOne
@JoinColumn(name = "shuo_id")
@JsonIgnore
Shuo shuo;
String imgSrc;
service class
@Resource
ShuoRepository rep;
@PostMapping("add")
public String add(@RequestBody ShuoAddVO vo) {
System.out.println(vo);
User user = new User();
user.setId(1);
//
Shuo shuo = new Shuo();
shuo.setContent(vo.getContent());
shuo.setUser(user);
shuo.setDate(new Date());
shuo.setImgs(vo.getImgs());
rep.save(shuo);
//
return "";
}
this is the printed sql
Hibernate: select next_val as id_val from hibernate_sequence for update
Hibernate: update hibernate_sequence set next_val= ? where next_val=?
Hibernate: select next_val as id_val from hibernate_sequence for update
Hibernate: update hibernate_sequence set next_val= ? where next_val=?
Hibernate: select next_val as id_val from hibernate_sequence for update
Hibernate: update hibernate_sequence set next_val= ? where next_val=?
Hibernate: insert into shuo (content, date, title, user_id, id) values (?, ?, ?, ?, ?)
Hibernate: insert into shuo_img (img_src, shuo_id, id) values (?, ?, ?)
Hibernate: insert into shuo_img (img_src, shuo_id, id) values (?, ?, ?)
after execution, the id of the two classes will be shared and grow together, such as
it"s strange that I don"t have such a problem with another set of code
@Entity
public class Game {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private int id;
private String name;
@OneToMany(cascade = CascadeType.PERSIST, mappedBy = "game")
private List<Content> contents;
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public List<Content> getContents() {
return contents;
}
public void setContents(List<Content> contents) {
for (Content content : contents) {
content.setGame(this);
}
this.contents = contents;
}
}
@Entity
@JsonIgnoreProperties(ignoreUnknown = true)
public class Content {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
Integer id;
String text;
@ManyToOne
@JoinColumn(name = "game_id")
@JsonIgnore
Game game;
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public String getText() {
return text;
}
public void setText(String text) {
this.text = text;
}
public Game getGame() {
return game;
}
public void setGame(Game game) {
this.game = game;
}
@Override
public String toString() {
return "Content [id=" + id + ", text=" + text + "]";
}
}
@Controller
public class Controllerq {
@Autowired
GameRepository rep;
@RequestMapping("add")
@ResponseBody
public String register() {
Game game = new Game();
game.setName("hahagame");
Content content = new Content();
content.setText("hahah1");
Content content2 = new Content();
content2.setText("hahah2");
game.setContents(Arrays.asList(new Content[] { content, content2 }));
return rep.save(game).toString();
}
(strategy = GenerationType.IDENTITY)