1

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

}
BalusC
  • 1,082,665
  • 372
  • 3,610
  • 3,555
DarkSide
  • 25
  • 1
  • 9
  • This might work for you: http://stackoverflow.com/a/16349845/1004631 – robot_alien Apr 24 '17 at 14:16
  • thank you for the quick answer! but now I'm getting id=600, 601...etc – DarkSide Apr 24 '17 at 14:24
  • 1
    It did work ! Thank you very much!! Can you please copy you comment as an answer so I can mark the question as resolved! thanks again – DarkSide Apr 24 '17 at 14:28
  • Great! How about the sequencing now? – robot_alien Apr 24 '17 at 14:29
  • Working just fine! – DarkSide Apr 24 '17 at 14:35
  • The answer is in the link you provided : Setting hibernate.id.new_generator_mappings to false in persistence.xml and adding the allocationSize to 1 in the @SequenceGenerator – DarkSide Apr 24 '17 at 14:37
  • For next questions, please narrow down the tagging. Java-se is way to broad (and only meant for problems that can be reproduced in a clean jdk with just a main class and no external imports). JSF and certainly PrimeFaces are only related if you have a problem in the ui layer. – Kukeltje Apr 24 '17 at 14:40
  • 2
    Possible duplicate of [Hibernate generates negative id values when using a sequence](http://stackoverflow.com/questions/9861416/hibernate-generates-negative-id-values-when-using-a-sequence) – Kukeltje Apr 24 '17 at 14:41

1 Answers1

0

after adding this to @SequenceGenerator then working fine!!!

allocationSize = 1
K Kumar
  • 131
  • 5
  • 14