(Spring Boot (DATA JPA)) How can I ensure that each table can have multiple records with the same id without causing overwriting existing records?

I am currently creating a POST endpoint that sends a request body containing an array of strings and other variables. I am trying to save this data into two different tables, both referencing the same ID that is stored in the first table. Each table schema has an ID column. However, when I try to insert data into the second table using the ID from the first table, it seems like the second table can’t have duplicate IDs, resulting in only one record being saved.

For example, in the service layer

public ReturnDto save(InputDto inputDto) {

FirstTableEntity a = a.builder(...somecode).build()

FirstTableEntity saveda = repo1.save(a); // Spring Data JPA repository interface for performing CRUD on FirstTableEntity

List<SecondTableEntity> List = inputDto.{ArrayofString}().stream().map(
     e -> SecondTableEntity.builder()
         .id(saveda.getId()) // This is where I am referencing the first table's Id.
         ...somecode
 ).collect(Collectors.toList());

 repo2.saveAll(List); // Spring Data JPA repository interface for performing CRUD on SecondTableEntity

FirstTable DAO class
(I did not need to specify id for the builder petter due to this @GeneratedValue Annotation for first table and this is the id I am trying to referencing in second table.)

 @Id
 @GeneratedValue(strategy = GenerationType.AUTO)
 @Column(name = "SALE_ID")
 @JdbcTypeCode(SqlTypes.VARCHAR)
 private UUID id;

SecondTable DAO class

 @Id
 @Column(name = "SALE_ID")
 @JdbcTypeCode(SqlTypes.VARCHAR)
 private UUID id;

Schema for first table

create or replace FIRSTTABLE (
    SALE_ID VARCHAR(100) NOT NULL DEFAULT UUID_STRING() COMMENT 'System generated UUID',...etc
)

Schema for second table

create or replace SECONDTABLE (
    SALE_ID VARCHAR(100) NOT NULL COMMENT 'ID of sale',...etc

)

Suppose I have a array of string [a,b] in the request body, and each a,b should have a same id value that referencing first table’s id but it only result that b is the only one get saved.

When I see the log, it seems like a is storing first but then b is overwrite the a since they have a same id. However what I want is below. ID should be uuid but just better understanding I am using simple number.

ID
 3   C
 3   D
 3   E

Any advice would be appreciated. I am in work hour, so I will check for comments every 5-10 minutes. Any feedback would be greatly appreciated. If you need more info, please let me know as well!

1

What you want to use here is a foreign key and a parent-child relationship between FirstTable and SecondTable.

SecondTable should have a separate, its own id which would be unique within SecondTable.

A Primary Key (marked by @Id in java code) is a unique identifier of each entity, so it is always not null and unique within its table. That is why you got ‘a’ overwritten by ‘b’. Since both have the same id – thus represent the same entity.

A Foreign Key is a link to another entity in another table. Usually Foreign Key in the ChildTable refers to the Primary Key of the entity in the ParentTable (but may refer to any other unique field in the ParentTable). Foreign Key can be the same for several entites in the ChildTable (SecondTable in your case).

So what you need to do is to add a foreign key field to the SecondTable (both java and sql) and ‘link’ it to the primary key (@Id) of the FirstTable:

SecondTable entity class

 @Id
 @Column(name = "ID")
 @JdbcTypeCode(SqlTypes.VARCHAR)
 private UUID id;

 @ManyToOne
 @JoinColumn(name = "SALE_ID") // Foreign Key
 private FirstTableEntity firstTableEntity;

FirstTableEntity class. You can leave it unchanged, or add a backward link to child entites:

@OneToMany(mappedBy = "firstTableEntity", cascade = CascadeType.ALL)
private List<SecondTableEntity> secondTableEntities;

Schema for SecondTable (syntaxis may differ)

 create or replace SECONDTABLE (
    ID VARCHAR(100) PRIMARY KEY DEFAULT UUID_STRING() COMMENT 'System generated UUID'
    SALE_ID VARCHAR(100) FOREIGN KEY REFERENCES FirstTable(SALE_ID) COMMENT 'ID of sale',...etc )

Additional point – naming conventions. Usually, Primary Key fields are called simply “ID” and Foreign Key fields – “RefferedTableName_ID”. So I would recommend to rename FirstTable PK from “SALE_ID” to “ID” to avoid misunderstandings.

Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa Dịch vụ tổ chức sự kiện 5 sao Thông tin về chúng tôi Dịch vụ sinh nhật bé trai Dịch vụ sinh nhật bé gái Sự kiện trọn gói Các tiết mục giải trí Dịch vụ bổ trợ Tiệc cưới sang trọng Dịch vụ khai trương Tư vấn tổ chức sự kiện Hình ảnh sự kiện Cập nhật tin tức Liên hệ ngay Thuê chú hề chuyên nghiệp Tiệc tất niên cho công ty Trang trí tiệc cuối năm Tiệc tất niên độc đáo Sinh nhật bé Hải Đăng Sinh nhật đáng yêu bé Khánh Vân Sinh nhật sang trọng Bích Ngân Tiệc sinh nhật bé Thanh Trang Dịch vụ ông già Noel Xiếc thú vui nhộn Biểu diễn xiếc quay đĩa Dịch vụ tổ chức tiệc uy tín Khám phá dịch vụ của chúng tôi Tiệc sinh nhật cho bé trai Trang trí tiệc cho bé gái Gói sự kiện chuyên nghiệp Chương trình giải trí hấp dẫn Dịch vụ hỗ trợ sự kiện Trang trí tiệc cưới đẹp Khởi đầu thành công với khai trương Chuyên gia tư vấn sự kiện Xem ảnh các sự kiện đẹp Tin mới về sự kiện Kết nối với đội ngũ chuyên gia Chú hề vui nhộn cho tiệc sinh nhật Ý tưởng tiệc cuối năm Tất niên độc đáo Trang trí tiệc hiện đại Tổ chức sinh nhật cho Hải Đăng Sinh nhật độc quyền Khánh Vân Phong cách tiệc Bích Ngân Trang trí tiệc bé Thanh Trang Thuê dịch vụ ông già Noel chuyên nghiệp Xem xiếc khỉ đặc sắc Xiếc quay đĩa thú vị
Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa
Thiết kế website Thiết kế website Thiết kế website Cách kháng tài khoản quảng cáo Mua bán Fanpage Facebook Dịch vụ SEO Tổ chức sinh nhật