gpt4 book ai didi

mysql - @OneToOne 映射为 @OneToMany

转载 作者:行者123 更新时间:2023-11-29 15:55:12 24 4
gpt4 key购买 nike

//@OneToOne 关系通过 Spring data JPA///hibernate in MySQL 映射为 @OneToMany

@Entity
@Table(name = "product")
public class Product extends DataJournal {

@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Long id;
private String name;
private Double price;

private Date dom;
private Date expiryDate;

@OneToOne(mappedBy = "product", optional = false)
private Stock stock;
//getters and setters
}

@Entity
@Table(name = "stock")
public class Stock extends DataJournal {

@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Long id;
private Long quantity;
private Long reOrderLabel;

@OneToOne
@JoinColumn(name="product_id")
private Product product;
//getters and setters
}

//Product 和 stock 之间的关系是 @OneToOne 但在 Mysql 中被映射为//as @OneToMany,以下是反向的 mysql 数据库和//关系 [1]:https://imgur.com/a/OqC0Ddt [2]:https://imgur.com/a/3fFMuDD

最佳答案

如果您让 Hibernate 为您生成 DDL,请尝试使用 @JoinColumn(unique = true)在您的 Stock 类中:

@Entity
@Table(name = "stock")
public class Stock extends DataJournal {

@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Long id;
private Long quantity;
private Long reOrderLabel;

@OneToOne
@JoinColumn(name="product_id", unique = true)
private Product product;
//getters and setters
}

关于mysql - @OneToOne 映射为 @OneToMany,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56520373/

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