That is not working. I have reopened the bug report, because as soon as
I provide an instance of the primary key to the variable marked as
@EmbeddedId, TopLink includes the PK column in the SQL INSERT statement,
providing a default value of zero, leading to duplicate PK values. Here
is the source snippet I have used (and yes, as soon as I abstain from
using @EmbeddedId and just use a simple @Id, it works pretty well -- so
it is not a DBMS problem):
@Embeddable
public final class MyPK implements Serializable {
@GeneratedValue(strategy = IDENTITY)
public int id;
...
}
@Entity
public class MyEntity {
@EmbeddedId
private MyPK pk;
protected MyEntity() {
}
public MyEntity(final String description) {
this.description = description;
this.pk = new MyPK();
}
private String description;
@PostPersist
protected void afterInsert() {
System.out.println(this.pk.id);
}
}
public final class Client {
public static final void main(final String[] args) {
final EntityManagerFactory emf =
Persistence.createEntityManagerFactory("caq");
final EntityManager em = emf.createEntityManager();
em.getTransaction().begin();
em.persist(new MyEntity("Test"));
em.getTransaction().commit();
}
}
So I am assuming that it is a bug in TopLink.
Thanks
Markus
Gordon Yorke schrieb:
> Create the Embedded class instance within your entity when creating
> your entity. @GenerateValue provides a pk *value* for a field it does
> not create an embedded class instance.
> --Gordon
>
> Markus KARG wrote:
>> I am using @EmbeddedId to reference my @Embeddable PK class.
>> The PK class is using @GeneratedValue(strategy = IDENTITY).
>> The table's PK column is AUTOINCREMENT'ing.
>>
>> When running this code, I get a new row in the table, including a
>> unique ID:
>>
>> em.getTransaction().begin();
>> CostDescription c = new CostDescription();
>> c.setDescription("Test3");
>> em.persist(c);
>> em.getTransaction().commit();
>>
>> But the field marked as @EmbeddedId always stays null.
>>
>> I tried using em.refresh(c), but TopLink says "Entity no longer
>> exists in the database" (what is not true: the row actually is to be
>> found in the db).
>>
>> Any ideas how to get it running?
>>
>> Thanks
>> Markus
>>
--
http://www.xing.com/go/invita/58469