@OneToOne映射为@OneToMany

时间:2019-06-10 04:03:01

标签: mysql hibernate spring-data-jpa

// @@ OneToOne关系已由Spring数据JPA ///在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
}

//产品与库存之间的关系为@OneToOne,但//在Mysql中映射为// @@ ToToMany,以下是反向的mysql数据库和//关系     [1]:https://docs.datastax.com/en/archived/cql/3.3/cql/cql_reference/ref-lexical-valid-chars.html     [2]:https://imgur.com/a/OqC0Ddt

1 个答案:

答案 0 :(得分:0)

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

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