mercredi 17 juin 2015

Sqlite JPA Primary Key error

Hello i have a Problem with Sqlite and JPA. I have the following table in Sqlite:

Section:
secID INTEGER Primary Key
secname TEXT

If I use Primary Key in Sqlite this Value will automatically updated when i insert a value like

INSERT INTO Section(secname) VALUES("Default");

In my Java Classes i have the following Class "Section":

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

    @Id
    @Column(name="\"secID\"")
    private int secID;

    @Column(name="\"secname\"")
    private String secname;
    + Getter and Setter
}

The table had by Default two values. If i try to insert a new value in the table with jpa like:

Section sec = new Section();
sec.setSecname("Test2");
tx.begin();
    em.persist(sec);
tx.commit();

it will be inserted with secID = 0. If i then try to insert a new Value i become an Error because at this time the secID = 1 and this secID already exists in the Table because of the two Values that are already in the Table (they have secID = 1 and secID = 2).

How can i avoid this Problem with JPA? I use EclipseLink.

Aucun commentaire:

Enregistrer un commentaire