tags:

views:

509

answers:

1

I've created the following scenario:

@javax.persistence.Entity
@Inheritance(strategy = InheritanceType.TABLE_PER_CLASS)
public class MyEntity implements Serializable{
    @Id
    @GeneratedValue
    protected Long id;
    ...
    @ElementCollection
    @CollectionTable(name="ENTITY_PARAMS")
    @MapKeyColumn (name = "ENTITY_KEY")
    @Column(name = "ENTITY_VALUE")
    protected Map<String, String> parameters;
    ...
}

As well as:

@javax.persistence.Entity
public class Sensor extends MyEntity{
    @Id
    @GeneratedValue
    protected Long id;
    ...

    // so here "protected Map<String, String> parameters;" is inherited !!!!
    ...
}

So running this example, no tables are created and i get the following message:

WARNUNG: Got SQLException executing statement "CREATE TABLE ENTITY_PARAMS (Entity_ID BIGINT NOT NULL, ENTITY_VALUE VARCHAR(255), ENTITY_KEY VARCHAR(255), Sensor_ID BIGINT NOT NULL, ENTITY_VALUE VARCHAR(255))": com.mysql.jdbc.exceptions.jdbc4.MySQLSyntaxErrorException: Duplicate column name 'ENTITY_VALUE'

I also tried overriding the attributes on the Sensor class...

@AttributeOverrides({
    @AttributeOverride(name = "ENTITY_KEY", column = @Column(name = "SENSOR_KEY")),
    @AttributeOverride(name = "ENTITY_VALUE", column = @Column(name = "SENSOR_VALUE"))
})

... but the same error.

EDIT:

Okay, I'd found out that with the Inheritance strategy "JOINED" as well as with "SINGLE_TABLE" everything works fine. Also it seems that it has nothing to do with the EclipseLink version - I tried 1.3 and 2.0.1.

END_EDIT

Can anybody help me?

A: 

Okay, I've just found out what was wrong!

In such a scenario I'd built you should couldn't use the @CollectionTable(name="ENTITY_PARAMS") annotation.

So, by just using... @ElementCollection @MapKeyColumn (name = "PARAM_KEY") @Column(name = "PARAM_VALUE") private Map parameters;

Every works fine, and the resulting tables (in MySQL) are:

CREATE TABLE Sensor_PARAMETERS (
    Sensor_ID BIGINT NOT NULL,
    PARAM_VALUE VARCHAR(255),
    PARAM_KEY VARCHAR(255)
)

and

CREATE TABLE Entity_PARAMETERS (
    Entity_ID BIGINT NOT NULL,
    PARAM_VALUE VARCHAR(255),
    PARAM_KEY VARCHAR(255)
)

So, without that attribute everything works fine.... Hope, nobody needs this post. Even if: "Congratulation, you found the answer!" ;-)

gerry