Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

JPA entity without id

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?

like image 893
mikhail Avatar asked Sep 29 '10 10:09

mikhail


People also ask

Can we create JPA entity without Id?

JPA requires that every entity has an ID. So no, entity w/o an ID is not allowed. Every JPA entity must have a primary key.

Can we have an entity without @ID?

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.

Why Id is mandatory in JPA?

Id is required by JPA, but it is not required that the Id specified in your mapping match the Id in your database. For instance you can map a table with no id to a jpa entity. To do it just specify that the "Jpa Id" is the combination of all columns.


3 Answers

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;       ... } 
like image 120
axtavt Avatar answered Sep 16 '22 12:09

axtavt


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.

like image 39
Pascal Thivent Avatar answered Sep 18 '22 12:09

Pascal Thivent


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.

like image 36
Gilberto Avatar answered Sep 17 '22 12:09

Gilberto