views:

67

answers:

5

I have database with next 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 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 got exception:

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

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

A: 

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
A: 

As far as I can see, entity_id is a foreign key and should be identical to id in the entity table. So you can use the field entity_id as the primary key in your class like so:

@Id
@Column(name="entity_id")
private int id;
LeChe
A: 

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

Qwerky
+2  A: 

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:

@Embedded
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; 

    ...
}
axtavt
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.
mikhail
@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
+2  A: 

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