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

发布于 2022-09-06 22:31:18 字数 4246 浏览 7 评论 0

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) 试试
    

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文