JPA entity has no primary key?

前端 未结 5 778
灰色年华
灰色年华 2020-12-16 13:36

I have an Entity class:

@Entity
@Table(name=\"CMC_MAP_SERVER_INFO\")
@NamedQuery(name=\"CmcMapServerInfo.getMapServer\", query=\"SELECT c FROM CmcMapServerIn         


        
5条回答
  •  死守一世寂寞
    2020-12-16 14:24

    Every entity object in the database is uniquely identified. An alternate way to represent a table without a primary key is to use a composite primary key using all its columns, or some of them representing a candidate key:

    @Entity
    public class TableWithoutId {
        @EmbeddedId EmbeddableTableWithoutId id;
    
        /* Getters an Setters... */
    
        //Here you can implement @Transient delegates to the Getters an Setters of "id".
    }
    
    @Embeddable
    Class EmbeddableTableWithoutId {
        int columnA;
        long columnB;
        /* Getters an Setters... */
    }
    

    Maybe you will have problems with [Select By Id]:

    entityManager.find(TableWithoutId.class, id);//it can throws NonUniqueResultException or anything like that...
    

    Take care and be happy!!!

提交回复
热议问题