mercredi 17 juin 2015

Null or Zero Primary Key in unit work clone sqlite

Hello I have a Problem with JPA and SQLite.

I have a table called Category in SQLite. This is the Structur of this table:

Category:
catID INTEGER Primary Key
catName TEXT

Now i'm trying to insert a new Category via JPA. This is my method to do this:

public void insertCategory(EntityManager em, String catName) {
    Category cat = new Category();
    cat.setCatName(catName);
    em.getTransaction().begin();
        em.persist(cat);
        em.flush();
    em.getTransaction().commit();
}

Now I have an Execption at em.flush(). The Exception says

Null or zero primary key encountered in UnitOfWork clone

I know this happens because of the Object cat. The catID is still null. The class looks like:

@Entity
@Table(name="Category")
public class Category implements Serializable {
    private static final long serialVersionUID = 1L;

    @Column(name="catName")
    private String catName;

    @Id
    @Column(name="catID")
    private Integer catID;
    + Getter and Setter
}

SQLite increments the Primary Key automatically when not given in the query. How can i inform my Program about the Primary Key to run without getting this Exception??

Aucun commentaire:

Enregistrer un commentaire