Welcome to WuJiGu Developer Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
365 views
in Technique[技术] by (71.8m points)

java - Best practise for the Many to Many relation using middle table in JPA

I have three tables in my database; PRODUCT, WAREHOUSE, and PRODUCT_WAREHOUSE. PRODUCT_WAREHOUSE is the middle table that keeps many to many relations of product and the warehouse. In JPA I used one entity for each of the tables and I used ManyToOne annotation for relations in product_warehouse. I also created DTOs for each entity.

My problem is in my ProductWarehouseDTO I keep ProductDTO and WarehouseDTO to have the same structure in my entity classes. However, in the service layer with using this structure whenever I need to insert a new record for PRODUCT_WAREHOUSE, I need full information of PRODUCT and WAREHOUSE whilst I need only ids of them. As a workaround, I thought about database queries for each product and warehouse to get the information for the records. This will create too much burden on the service.

I want to know what is the best practice for Hibernate for keeping these middle tables? Thanks for your responses.

I am putting the entities and DTOs below.

ENTITIES:

@MappedSuperclass
public class BaseEntity {
    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    private Long id;

    @Column(name = "CODE" ,nullable = false,unique = true,length = 50)
    private String code;
}
@Entity
@Table(name = "PRODUCT")
public class ProductEntity extends BaseEntity{

    @Enumerated(EnumType.STRING)
    @Column(name = "STATUS",nullable = false,length = 7)
    private ProductStatusEnum status;
}

@Entity
@Table(name = "WAREHOUSE")
public class WarehouseEntity extends BaseEntity{


    @Enumerated(value = EnumType.STRING)
    @Column(name = "STATUS",length = 7, nullable = false)
    private WarehouseStatusEnum status;
}

@Entity
@Table(name = "PRODUCT_WAREHOUSE")
public class ProductWarehouseEntity {

    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    private Long id;

    @ManyToOne
    @MapsId("id")
    @JoinColumn(name = "PRODUCT_ID", nullable = false)
    private ProductEntity product;

    @MapsId("id")
    @ManyToOne
    @JoinColumn(name = "WAREHOUSE_ID", nullable = false)
    private WarehouseEntity warehouse;

    @Column(name = "STOCK_AMOUNT", nullable = false)
    private int stockAmount = 0;

}

DTOS:

@Getter
@Setter
public class BaseDTO implements Serializable {
    private Long id;
    private String code;
}
@Getter
@Setter
public class ProductDTO extends BaseIDDTO {
    private String code;
    private ProductStatus status;
}

@Getter
@Setter
public class WarehouseDTO extends BaseDTO{
    private WarehouseStatusEnum status;
}

@Getter
@Setter
public class ProductWarehouseDTO {
    private ProductDTO product;
    private WarehouseDTO warehouse;
    private Long stockAmount;
    private Long id;

}

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)
等待大神答复

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to WuJiGu Developer Q&A Community for programmer and developer-Open, Learning and Share
...