如何在JPA中映射这些实体

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

How to map these entities in JPA

问题

我有以下的表格:

create table product (
id int primary key,
sku text unique,
descripcion text
);

create table price_list (
id int primary key,
name text not null
)

create table price_list_item (
id_price_list int,
id_product int,
price decimal(12,2)
)

所以我想将这些表格映射到以下的类:

public class PriceList {
  private int id;
  private String name;
  private List<ProductPrice> prices;

  // ...
}

public class ProductPrice {
  private int idProduct;
  private String sku;
  private String descripcion;
  private BigDecimal price;
}

但是我似乎找不到/理解如何使用JPA来实现这个。

英文:

I have the following tables:

create table product (
id int primary key,
sku text unique,
descripcion text
);

create table price_list (
id int primary key,
name text not null
)

create table price_list_item (
id_price_list int,
id_product int,
price decimal(12,2)
)

And so i would like to map those tables to these classes:

public class PriceList {
  private int id;
  private String name;
  private List&lt;ProductPrice&gt; prices;

 .....
}

public class ProductPrice {
  private int idProduct;
  private String sku;
  private String descripcion;
  private BigDecimal price
}

But i can't seem to find / understand how to do this using JPA

答案1

得分: 1

希望这段代码对您有所帮助。

public class PriceList {
    @Id
    private int id;
    @Column(name="name")
    private String name;
}

public class Product {
    @Id
    private int id;
    @Column(name="sku")
    private String name;
    @Column(name="descripcion")
    private String description;
}

"prices" 应该由实现了 JpaRepository<PriceList, Integer> 的 PriceListRepository 返回。PriceList 是您的实体类的名称,Integer 是 id 的类型。类似地,您也可以为其他实体实现类。

英文:

Hope this code helps.

public class PriceList {
    @Id
    private int id;
    @Column(name=&quot;name&quot;)
    private String name;
}

public class Product{
    @Id
    private int id;
    @Column(name=&quot;sku&quot;)
    private String name;
    @Column(name=&quot;descripcion&quot;)
    private String description;
}

prices is supposed to be returned by PriceListRepository which can implement JpaRepository<PriceList, Integer>. PriceList is the name of your entity class and Integer is the type of id. Similarly you can implement classes for other entities as well.

huangapple
  • 本文由 发表于 2020年3月4日 10:13:39
  • 转载请务必保留本文链接:https://go.coder-hub.com/60518155.html
匿名

发表评论

匿名网友

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

确定