使用springdatajpa,hibernate将两个类级联保存,为什么这两个表会共享主键?

one类

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


@Entity
public class ShuoImg {
@Id
@GeneratedValue
int id;

@ManyToOne
@JoinColumn(name = "shuo_id")
@JsonIgnore
Shuo shuo;
String imgSrc;

service类

@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 "保存成功";
}

这是打印的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 (?, ?, ?)   
    

执行后 这两个类的 id会共用,共同增长,比如

图片描述

图片描述

很奇怪 我另一套代码又不存在这样的问题

@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) 试试
    
阅读 2.1k
评论
    0 个回答
    撰写回答

    登录后参与交流、获取后续更新提醒