86

I have a database with the following structure:

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

CREATE TABLE entity_property (
    entity_id SERIAL,
    name VARCHAR(255),
    value TEXT
);

When I try to create an EntityProperty class

@Entity
@Table(name="entity_property")
public class EntityProperty {

    private String name;
    private String value;

    @Column(name="name")
    public String getName() {
        return name;
    }
    public void setName(String name) {
        this.name = name;
    }

    @Column(name="value", nullable=true, length=255)
    public String getValue() {
        return value;
    }
    public void setValue(String value) {
        this.value = value;
    }
}

I get the following exception:

org.hibernate.AnnotationException: No identifier specified for entity: package.EntityProperty

I know that JPA entities must have a primary key but I can't change the database schema due to reasons that are beyond my control. Is it possible to create JPA (Hibernate) entities that will work with database schema like this?

lloiacono
  • 4,714
  • 2
  • 30
  • 46
Michael
  • 4,635
  • 7
  • 25
  • 33

6 Answers6

65

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; 

    ...
}
Italo Borssatto
  • 15,044
  • 7
  • 62
  • 88
axtavt
  • 239,438
  • 41
  • 511
  • 482
  • 2
    Thanks. But how can I directly access to entity "name" property in this case? My JP-QL query works well only if access to "name" via "entity.id.name", but I think that it's not right. – Michael Sep 30 '10 at 06:23
  • 1
    @mikhail: If you dislike approach with `@EmbeddedId`, you may try alternative approach with `@IdClass`: http://docs.jboss.org/hibernate/stable/annotations/reference/en/html_single/#d0e1112 – axtavt Sep 30 '10 at 09:10
  • 4
    Should the annotation for `EntityPropertyPK` be `@Embeddedable` instead of `@Embedded`? – binary_assemble Jun 09 '15 at 02:56
  • 1
    Yes the annotation for `EntityPropertyPK` has to be `@Embeddable` as `@Embedded` can only be applied on fields and methods. Moreover, class `EntityPropertyPK` must be declared `Serializable`. – Dominik Oct 18 '16 at 21:58
  • @mikhail : could you pls show me your mapping inside entity.java file – Saravanan Nov 01 '18 at 06:30
  • What if I don't have keys in database and changing schema is forbidden? Table with `date` and `some_int` for example? – Zon Nov 08 '18 at 12:15
  • @Michael you easily do what you require by making an extra getter and setter such that the getter extracts the value from embedded object – Abdeali Chandanwala Aug 09 '23 at 11:19
45

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.

Pascal Thivent
  • 562,542
  • 136
  • 1,062
  • 1,124
  • 5
    please , I m new . how can I use all the columns as ID in an entity with this small example : int SIN , int age , String name , String adresse . thank you – Omar B. Nov 09 '18 at 15:00
  • 2
    "you can use all of the columns as the Id", but my table is required to be able to store identical rows as separate records ;c – Klesun Jun 14 '21 at 09:32
37

See the Java Persistence book: Identity and Sequencing

The relevant part for your question is the No Primary Key section:

Sometimes your object or table has no primary key. The best solution in this case is normally to add a generated id to the object and table. If you do not have this option, sometimes there is a column or set of columns in the table that make up a unique value. You can use this unique set of columns as your id in JPA. The JPA Id does not always have to match the database table primary key constraint, nor is a primary key or a unique constraint required.

If your table truly has no unique columns, then use all of the columns as the id. Typically when this occurs the data is read-only, so even if the table allows duplicate rows with the same values, the objects will be the same anyway, so it does not matter that JPA thinks they are the same object. The issue with allowing updates and deletes is that there is no way to uniquely identify the object's row, so all of the matching rows will be updated or deleted.

If your object does not have an id, but its table does, this is fine. Make the object an Embeddable object, embeddable objects do not have ids. You will need a Entity that contains this Embeddable to persist and query it.

Ry-
  • 218,210
  • 55
  • 464
  • 476
Gilberto
  • 893
  • 1
  • 13
  • 28
4

I guess you can use @CollectionOfElements (for hibernate/jpa 1) / @ElementCollection (jpa 2) to map a collection of "entity properties" to a List in entity.

You can create the EntityProperty type and annotate it with @Embeddable

Bozho
  • 588,226
  • 146
  • 1,060
  • 1,140
  • An good example for `@ElementCollection` with `@CollectionTable` is at https://en.wikibooks.org/wiki/Java_Persistence/ElementCollection . See also https://stackoverflow.com/q/8969059/685806 – Pino Mar 11 '21 at 13:24
1

If there is a one to one mapping between entity and entity_property you can use entity_id as the identifier.

Qwerky
  • 18,217
  • 6
  • 44
  • 80
0

I do this.

Class Main

@Entity
@Table(name = "DOOR_HGF")
public class DOORSHGF implements Serializable {

@OneToMany(mappedBy = "hgfs", fetch = FetchType.LAZY)
private List<HGF> hgfs= new ArrayList<>();

private String Room;

//Rest Class

}

Child Class

@Entity
@Table(name="HGF")
Class HGF implements serializable {

@Id
@Column(name="ROWID")
private String id;

private String name;

@ManyToOne
@JoinColumn(name="ID_HGF_NK")
private DOORSHGF hgfs;

//Rest Class

}
CelzioBR
  • 126
  • 1
  • 10