• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Autoincrement int field on each EntityManager.merge() or EntityManager.persist()

 
Martin Asenov
Ranch Hand
Posts: 34
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello, everyone!

I've got a class that has a int field id, marked with annotations @Id and @GeneratedValue(strategy = GenerationType.AUTO) and another int field, called counter.

The issue here is that i need that counter to be incremented and unique after every single merge(), and not only on persist(). I need this for some business logic needs.

Can someone give me some hint on this?

Thank you in advance!

Best,
Martin
 
James Sutherland
Ranch Hand
Posts: 553
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
An @Version mapping will perform this, and also give you the benefit of locking.

Otherwise, you need to increment the field yourself, either in your app/model, or using a preUpdate JPA event.
 
Martin Asenov
Ranch Hand
Posts: 34
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thank you, James, looks like this is the perfect solution for me...

Best,
Martin
 
Don't get me started about those stupid light bulbs.
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic