Spring Data JPA在空数据库中为包含外键的复合主键实体创建错误的字段。

huangapple go评论82阅读模式
英文:

Spring data jpa creates wrong fields in empty database for entity with composite primary key that contains foreign key

问题

以下是翻译好的部分:

我有以下数据库结构以进行简化:

表 Product:

id(主键)

表 Place:

id(主键)

表 WarehouseProduct:

place(主键,外键)
product(主键,外键)
count

我使用以下代码来描述代码中的数据库:

Place:

@Entity 
public class Place {
    @Id
    String id;
}

Product:

@Entity 
public class Product {
    @Id
    String id;
}

WarehouseProduct 的主键:

@Embeddable
public class WarehouseProductPK implements Serializable {
    String place;
    String product;
}

WarehouseProduct:

@Entity
public class WarehouseProduct {
    @EmbeddedId;
    WarehouseProductPK pk;

    @OneToOne(targetEntity = Product.class)
    Product product;

    @OneToOne(targetEntity = Place.class)
    Place place;

    @Column(nullable = false)
    int count;
}

我在空的 h2 数据库上进行了测试。结果是为 WarehouseProduct 创建了以下表格:

表 WarehouseProduct:

place(主键)
product(主键)
count
placeId(外键)
productId(外键)

如何使用 Spring Data JPA 在空数据库中创建所需的数据库结构?

英文:

I have following database structure to simplify:

Table Product:
-----
id(pk)

Table Place:
-----
id(pk)

Table WarehouseProduct:
-----
place(pk, fk)
product(pk, fk)
count

And i use this code to describe database in code:

Place:

@Entity 
public class Place {
    @Id
    String id;
}

Product:

@Entity 
public class Product {
    @Id
    String id;
}

primary key for WarehouseProduct:

@Embeddable
public class WarehouseProductPK implements Serializable {
    String place;
    String product;
}

WarehouseProduct:

@Entity
public class WarehouseProduct {
    @EmbeddedId;
    WarehouseProductPK pk;

    @OneToOne(targetEntity = Product.class)
    Product product;

    @OneToOne(targetEntity = Place.class)
    Place place;

    @Column(nullable = false)
    int count;
}

I test this on empty h2 database. In result is created following table for WarehouseProduct:

Table WarehouseProduct:
-----
place(pk)
product(pk)
count
placeId(fk)
productId(fk)

How to create required db structure with data jpa in empty database?

答案1

得分: 0

使用以下代码为 WarehouseProduct 实体数据,JPA 将创建所需的数据库结构:

WarehouseProduct 的主键:

@Embeddable
public class WarehouseProductPK implements Serializable {
    @OneToOne(targetEntity = Place.class)
    Place place;

    @OneToOne(targetEntity = Product.class)
    Product product;
}

WarehouseProduct:

@Entity
public class WarehouseProduct {
    @EmbeddedId;
    WarehouseProductPK pk;

    @Column(nullable = false)
    int count;
}

结果是在空的 H2 数据库中,我得到了如下的 WarehouseProduct 表格:

Table WarehouseProduct:
-----
placeId(pk, fk)
productId(pk, fk)
count
英文:

With following code for WarehouseProduct entity data jpa creates required database structure:

Primary key for WarehouseProduct:

@Embeddable
public class WarehouseProductPK implements Serializable {
    @OneToOne(targetEntity = Place.class)
    Place place;

    @OneToOne(targetEntity = Product.class)
    Product product;
}

WarehouseProduct:

@Entity
public class WarehouseProduct {
    @EmbeddedId;
    WarehouseProductPK pk;

    @Column(nullable = false)
    int count;
}

In result i got such table for WarehouseProduct in empty h2 db:

Table WarehouseProduct:
-----
placeId(pk, fk)
productId(pk, fk)
count

答案2

得分: 0

@Embeddable
public class WarehouseProductPK implements Serializable {

    @Column(name="place")
    String placeId;

    @Column(name="product")
    String productId;
}

@Entity
public class WarehouseProduct {
    @EmbeddedId
    WarehouseProductPK pk;

    @OneToOne
    @MapsId("placeId")
    @JoinColumn(name="place")
    Place place;

    @OneToOne
    @MapsId("productId")
    @JoinColumn(name="product")
    Product product;

    @Column(nullable = false)
    int count;
}
英文:
@Embeddable
public class WarehouseProductPK implements Serializable {

    @Column(name="place")
    String placeId;

    @Column(name="product")
    String productId;
}

@Entity
public class WarehouseProduct {
    @EmbeddedId;
    WarehouseProductPK pk;

    @OneToOne
    @MapsId("placeId")
    @JoinColumn(name="place")
    Place place;

    @OneToOne
    @MapsId("productId")
    @JoinColumn(name="product")
    Product product;

    @Column(nullable = false)
    int count;
}

huangapple
  • 本文由 发表于 2020年9月11日 17:28:24
  • 转载请务必保留本文链接:https://go.coder-hub.com/63844322.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定