Reputation: 665
How do I prevent someone from creating an entity and setting it's id? I want to defer to the database autoincrement and make it throw a 409 error if it happens. I am trying @GeneratedValue(strategy = GenerationType.IDENTITY) and @GeneratedValue(strategy = GenerationType.AUTO) with the primary key in the database autoincrementing, but the setting of the id is never flagged as a problem and will override my autoincrement scheme in the mysql database.
Upvotes: 0
Views: 1061
Reputation: 10497
One thing you can do is : make your id property and its setter method private
, which will not let user access setId
method of your object,
But as Hibernate uses set method to initialize the entity which you're reading from your DB, it can access this method using reflection. So you don't have to worry about changing any other things.
Upvotes: 2
Reputation: 620
Hi maybe you can only have a getter on the id, but i do not know hibernate/jpa
Upvotes: 0