英文:
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<ProductPrice> 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="name")
private String name;
}
public class Product{
@Id
private int id;
@Column(name="sku")
private String name;
@Column(name="descripcion")
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.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论