0 Replies Latest reply on Feb 10, 2013 2:52 PM by fabiovalse

    How to get the id of entity with auto_increment value?


      Hi there,


      I've an entity Order with @GeneratedValue(strategy = GenerationType.IDENTITY) annotation (I've a MySql database).




      @Table(name = "orders")

      public class Order implements Serializable {

      private static final long serialVersionUID = 3L;


          public Order() {





          @GeneratedValue(strategy = GenerationType.IDENTITY)

          @Column(name = "id")

          private int id;






      The persist of the order works well. The problem is that after the persist is done, if I try to execute the getId() method of the Order entity the value returned is always zero.

      The id field of the table orders is an auto_increment value and looking in the database after the persist its value is correctly inserted.


      How can I fix the problem? Looking in the internet there are a lot of suggestions saying to use GenerationType.TABLE or GenerationType.SEQUENCE that are not compatible with MySql.


      Thank you in advance