I am creating a CRUD web application using JPA.
Technically every thing is working fine (no errors at all), but when checking my database I noticed that the after adding a new entry to my table, the ID generated from a sequence is a negative value: -46, -45, -44, etc ...
Here are the relevant parts of my code :
My entity :
@Entity
@NamedQuery(name="Book.findAll", query="SELECT b FROM Book b")
@SequenceGenerator(name="ma_seq", sequenceName="book_seq")
public class Book implements Serializable {
private static final long serialVersionUID = 1L;
@GeneratedValue(strategy=GenerationType.SEQUENCE, generator="ma_seq")
@Id private long id;
private String auteur;
private String langue;
private String titre;
public Book() {
}
//...getters and setters
}
My DAO :
public class MyDAO {
//Constructeur
public MyDAO(){
}
@PersistenceContext
private EntityManager em;
@Resource
private UserTransaction userTransaction;
public EntityManager getEm() {
return em;
}
public void setEm(EntityManager em) {
this.em = em;
}
@Transactional
public void register(Book livre) throws NotSupportedException, SystemException, SecurityException, IllegalStateException, RollbackException, HeuristicMixedException, HeuristicRollbackException {
// Save employee
userTransaction.begin();
this.em.persist(livre);
userTransaction.commit();
}
//other fonctions
}