After I persist an object and commit like this:
Version version = new Version();
version.setVersion(getNext(this.version));
version.setProjectID(projectID);
em.persist(version);
em.getTransaction().commit();
I want to get the ID(Primary Key) by this code line:
Object id = factory.getPersistenceUnitUtil().getIdentifier(version);
I can get the Version
and ProjectID
but the ID
is null. The ID is auto generated but still return null. I tried to implement @GeneratedValue(strategy=GenerationType.IDENTITY)
in my entity class but nothing change. Any solution for me?
EDIT WITH MORE CODE
Version Entity: this is using an EmbededId
@Entity
@NamedQuery(name="Version.findAll", query="SELECT v FROM Version v")
@Table(name="Version")
public class Version implements Serializable {
@EmbeddedId
@GeneratedValue(strategy=GenerationType.SEQUENCE)
private VersionPK id;
@Column(name="Version")
private String version;
@Column(name="ProjectID")
private int projectID;
Embeddable class:
@Embeddable
public class VersionPK implements Serializable {
//default serial version id, required for serializable classes.
private static final long serialVersionUID = 1L;
@Column(name="ID")
private int id;
public VersionPK() {
}