My tables are the following :
CREATE TABLE table1
(
id BIGSERIAL PRIMARY KEY NOT NULL,
param1 varchar(255)
);
CREATE TABLE table2
(
id_table1 BIGINT NOT NULL,
pk2 varchar(255) NOT NULL,
param2 varchar(255) NOT NULL,
CONSTRAINT fk_table1 FOREIGN KEY (id_table1) REFERENCES merging_configuration(id)
);
ALTER TABLE merging_configuration_type
ADD PRIMARY KEY (id_table1, pk2);
I really need the (id_table1, pk2) to be the key
So In hibernate I wrote the following classes:
@Entity(name = "table1")
public class Table1 {
@Id
private Long id;
private String param1;
@OneToMany
@JoinColumn(name = "id_table1")
private List<Table2> table2list;
// constructors, getters, setters…
}
@Entity(name = "table2")
public class Table2 {
@EmbeddedId
private Table2Id id;
private String param2;
}
@Embeddable
public class Table2Id implements Serializable {
@Column(name = "id_table1")
private Long idTable1;
@Column(name = "pk2")
private String pk2;
}
So everything compile and the get functions works perfectly but I cannot POST or PUT anything with the Table1Repository.save(entity):
- case 1: the keys in
table2Listof myentityare the same as for the database: theparam1updates but notparam2 - case 2:
table2Listof myentitydoes not contains existing data in the database: I have this error in the logs:
[ERROR] o.h.e.j.s.SqlExceptionHelper - ERROR: a NULL value violates the NOT NULL constraint of the column "id_table1" in the relation "table2".
Meaning: The failed row contains (null, myPK2, myParam2)
- case 3 :
table2Listof myentitycontains non existing data in the database: the .save throw an error with the following message :Unable to find path.Table2 with id Table2Id(idTableB=1, pk2=myPK2)
Could anyone help me ? I think there is a problem with my entities but I don't see anything else that could work
Thanks a lot