简体   繁体   中英

Mapping error while persisting JPA object

I'm getting stuck with this error in a week. Could anyone explain why I'm getting this error and how to solve it?

I have an Entiry class Version and I want to return the Id after persist an Version entity object. But I'm getting this error:

Exception [EclipseLink-71] (Eclipse Persistence Services - 2.5.2.v20140319-9ad6abd): org.eclipse.persistence.exceptions.DescriptorException
Exception Description: A NullPointerException was thrown while setting the value of the instance variable [id] to the value [38].
Internal Exception: java.lang.NullPointerException
Mapping: org.eclipse.persistence.mappings.DirectToFieldMapping[id-->Version.ID]
Descriptor: RelationalDescriptor(varick.rap.demo.entities.VersionPK --> [DatabaseTable(Version)])

This is my full code:

    factory = Persistence.createEntityManagerFactory(PERSISTENCE_NAME);
    entity = factory.createEntityManager();
    entity.getTransaction().begin();

    Version version = new Version();
    version.setVersion(getNext(this.version));
    version.setProjectID(projectID);
    entity.persist(version);
    entity.flush();
    System.out.println("Version Id: " + version.getId());
    entity.getTransaction().commit();

Version entity class(this class is using an @EmbeddedId):

@Entity
@Table(name="Version")
public class Version implements Serializable {
    @EmbeddedId 
    private VersionPK id;

    @Column(name="Version")
    private String version;

    @Column(name="ProjectID")
    private int projectID;

    public Version() {
    }

    public VersionPK getId() {
        return this.id;
    }
    //get and set methods here

VersionPK class:

@Embeddable
public class VersionPK implements Serializable {
    //default serial version id, required for serializable classes.
    private static final long serialVersionUID = 1L;

    @Column(name="ID")
    @GeneratedValue(strategy=GenerationType.IDENTITY)
    private int id;

    public VersionPK() {
    }
    //get and set method here

the primary key is not set. maybe you should do like this :

VersionPK pk = new VersionPK();
pk.setId("****");

Version version = new Version();
version.setId(pk);
version.setVersion(getNext(this.version));
version.setProjectID(projectID);
entity.persist(version);
entity.flush();
System.out.println("Version Id: " + version.getId());
entity.getTransaction().commit();

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM