RRoman
RRoman

Reputation: 751

How to map these entities in 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

Upvotes: 0

Views: 28

Answers (1)

Rahul Talreja
Rahul Talreja

Reputation: 199

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 is the name of your entity class and Integer is the type of id. Similarly you can implement classes for other entities as well.

Upvotes: 1

Related Questions