JPA entity without id

前端 未结 5 975
死守一世寂寞
死守一世寂寞 2020-11-30 22:38

I have a database with the following structure:

CREATE TABLE entity (
    id SERIAL,
    name VARCHAR(255),
    PRIMARY KEY (id)
);

CREATE TABLE entity_prop         


        
5条回答
  •  感情败类
    2020-11-30 22:46

    I know that JPA entities must have primary key but I can't change database structure due to reasons beyond my control.

    More precisely, a JPA entity must have some Id defined. But a JPA Id does not necessarily have to be mapped on the table primary key (and JPA can somehow deal with a table without a primary key or unique constraint).

    Is it possible to create JPA (Hibernate) entities that will be work with database structure like this?

    If you have a column or a set of columns in the table that makes a unique value, you can use this unique set of columns as your Id in JPA.

    If your table has no unique columns at all, you can use all of the columns as the Id.

    And if your table has some id but your entity doesn't, make it an Embeddable.

提交回复
热议问题