JPA entity without id

前端 未结 5 973
死守一世寂寞
死守一世寂寞 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:56

    I guess your entity_property has a composite key (entity_id, name) where entity_id is a foreign key to entity. If so, you can map it as follows:

    @Embeddable
    public class EntityPropertyPK {
        @Column(name = "name")
        private String name;
    
        @ManyToOne
        @JoinColumn(name = "entity_id")
        private Entity entity;
    
        ...
    }
    
    @Entity 
    @Table(name="entity_property") 
    public class EntityProperty { 
        @EmbeddedId
        private EntityPropertyPK id;
    
        @Column(name = "value")
        private String value; 
    
        ...
    }
    

提交回复
热议问题