Friday, February 28, 2014

MySql JPA toplink Auto increment id generation.

To configure JPA entity to make use of auto increment feature of MySql for primary key generation you can make use of IDENTITY strategy ref below code. 


@Entity 
public class Customer { 

        @Id 
        @GeneratedValue(strategy=GenerationType.IDENTITY) 
        private long id;
        .
        .
        .
        public long getId() {
  return id;
 }
}

But problem with this way of generating ids with MySql and JPA combination is when you persist new entity and try to retrieve id assigned to newly persisted entity using getId() method. You get 0 value, because newly generated id for the record is not set with managed entity for that you need to flush after persisting the entity. 

If you don't specify strategy with GeneratedValue annotation, default value will be GenerationType.AUTO. AUTO strategy means your are asking JPA to select appropriate strategy. In most of the cases it is TABLE strategy. 


@Entity 
public class Customer { 

        @Id 
        @GeneratedValue
        private long id;
        .
        .
        .
        public long getId() {
  return id;
 }
}

One can make use of TABLE strategy directly, this way you have more control over configurations.


@Entity 
public class Customer { 

 @Id
 @GeneratedValue(generator = "idgen")
 @TableGenerator(name = "idgen", table = "ID_GEN", pkColumnName = "ID_NAME", valueColumnName = "ID_VAL", pkColumnValue = "CUST_SEQ", allocationSize = 1, initialValue = 0)
        private long id;
        .
        .
        .
        public long getId() {
  return id;
 }
}


35 comments:

  1. Replies
    1. IEEE Final Year Project centers make amazing deep learning final year projects ideas for final year students Final Year Projects for CSE to training and develop their deep learning experience and talents.

      IEEE Final Year projects Project Centers in India are consistently sought after. Final Year Students Projects take a shot at them to improve their aptitudes, while specialists like the enjoyment in interfering with innovation.

      corporate training in chennai corporate training in chennai

      corporate training companies in india corporate training companies in india

      corporate training companies in chennai corporate training companies in chennai

      I have read your blog its very attractive and impressive. I like it your blog. Digital Marketing Company in Chennai Project Centers in Chennai

      Delete
  2. Great step by step solution, thanks for the help!
    apple-ios training in chennai

    ReplyDelete
  3. Great step by step solution, thanks for the help!

    msbi training in chennai

    ReplyDelete
  4. Thanks for your marvelous posting! It is very useful and good. Come on. I want to introduce an get app installs, I try it and I feel it is so good to rank app to top in app store search results, have you ever heard it?

    ReplyDelete
  5. Very good informative article. Thanks for sharing such nice article, keep on up dating such good articles.

    Digital Transformation Services | Austere Technologies

    ReplyDelete
  6. Great article, really very helpful content you made. Thank you, keep sharing.

    cloud Services | Austere Technologies

    ReplyDelete
  7. Really great blog, it's very helpful and has great knowledgeable information. Thanks for sharing, keep updating such a good informative blog.

    Best Software Company | Austere Technologies

    ReplyDelete
  8. wow...nice blog, very helpful information. Thanks for sharing.

    Software Security Services | Austere Technologies

    ReplyDelete
  9. VERY INFORMATIVE BLOG. KEEP SHARING SUCH A GOOD ARTICLES.

    Mobility Services | Austere Technologies

    ReplyDelete
  10. Looking really so informative, thanks for sharing the valuable information here just keep sharing on latest updates...
    Best Online Software Training Institute | SQL Server Training

    ReplyDelete
  11. Wow...What an excellent informative blog, really helpful. Thank you.

    Best Commerce College| Avinash college of commerce

    ReplyDelete
  12. Very good informative article. Thanks for sharing such nice article, keep on up dating such good articles.

    Best Commerce College in Hyderabad | Avinash College of Commerce

    ReplyDelete
  13. Thank you for sharing this valuable information. But get out this busy life and find some peace with a beautiful trip. book Andaman holiday packages

    ReplyDelete
  14. Hi Thanks for the nice information its very useful to read your blog. We provide best Finance Training in Hyderabad

    ReplyDelete
  15. Hi Thanks for the nice information its very useful to read your blog. We provide best company secretary course

    ReplyDelete
  16. Hi Thanks for the nice information its very useful to read your blog. We provide best Chartered Accountancy

    ReplyDelete
  17. Hi Thanks for the nice information its very useful to read your blog. We provide best Cost And Management Accountancy (CMA)

    ReplyDelete
  18. Hi Thanks for the nice information its very useful to read your blog. We provide best Certified Public Accountant

    ReplyDelete
  19. Hi Thanks for the nice information its very useful to read your blog. We provide best Certified Financial Analyst

    ReplyDelete
  20. Thank you for sharing this valuable information. But get out this busy life and find some peace with a beautiful trip. book ANDAMAN TOUR PACKAGE @24599

    ReplyDelete
  21. Thank you for sharing this valuable information. But get out this busy life and find some peace with a beautiful trip. book BEST ANDAMAN HONEYMOON PACKAGE @5999

    ReplyDelete
  22. Hi Thanks for the nice information its very useful to read your blog. We provide best Association Of Chartered Certified Accountants

    ReplyDelete
  23. Hi Thanks for the nice information its very useful to read your blog. We provide Software Development Services

    ReplyDelete
  24. Hi Thanks for the nice information its very useful to read your blog. We provide best Chartered Institute Of Management Accountants

    ReplyDelete
  25. Hi Thanks for the nice information its very useful to read your blog. We provide best Find All Isfs Courses

    ReplyDelete
  26. Thanks for sharing a valuable information to us. Thanks for your blog.
    CIMA training institute in Hyderabad | ISFS

    ReplyDelete