Reputation: 13
Eclipselink generated a create table statement like so:
Create Table myTable (ID (255) not null, col1 (255), col2(255), col3(255) PK (ID, col1, col2)
@Embeddable
MyPK implements Serializable
{
@OneToOne
@Id
String col1;
@OneToOne
@Id
String col2;
...
}
@Entity
MyClass implements Serializable
{
@EmbeddedId
MyPK pk;
String col1;
String col2;
String col3;
...
}
How do I prevent the generation of the ID
column in the Create Table
statement? I ask because em.persist(MyClass)
throws a constraint exception on ID
being null. I expected that the @EmbeddedId
would override this and prevent that field from being created.
EDIT
The table that I am trying to generate in code looks like so:
fk - col1
fk - col2
VarChar - col3
Upvotes: 1
Views: 420
Reputation: 1996
First issue is a String attribute can not be a OneToOne mappings. Second a OneToOne mapping can not be used within an EmbeddedId. Third you do not use the @Id annotation within an EmbeddedId as an Embeddable can not have identity.
The easiest way to do this is:
@Entity
@IdClass(MyPK.class)
MyClass implements Serializable
{
@OneToOne
@Id
TargetClass rel1;
@OneToOne
@Id
SecondTargetClass rel2
@Basic
String col3;
...
}
MyPK implements Serializable
{
String rel1;
String rel2;
...
}
If you really need an Embeddable for the pk class then replace the @ID annotation with @MapsId and add the EmbeddedId annotation back in MyClass and the Embeddable annotation back to MyPK
Upvotes: 2