0

I want to map a column from my database as primary key which is not auto-increment. Please suggest me how to create hibernate annotation for this column

Ashish Nayal
  • 11
  • 1
  • 7
  • Bydefalut primary key column is not auto-increament untill you add any annotation for making it auto-increment – MayurB Nov 27 '13 at 05:29
  • but when I try to insert in database it is executing select query with this primary key column in the where clause – Ashish Nayal Nov 27 '13 at 05:46

1 Answers1

0

Use @Id annotation which defines the mapping of that property to the primary key column. Don't use @GeneratedValue annotation.

@Id
@Column(name = "id")
Integer id;
Vipul Paralikar
  • 1,508
  • 10
  • 22
  • When doing this as you suggested as I try to insert in database it is executing select query – Ashish Nayal Nov 27 '13 at 05:43
  • when I am using @generatedvalue for primary key the insert query is executing correctly but otherwise not – Ashish Nayal Nov 27 '13 at 05:58
  • as you said your primary key column is not auto-increment, you need to set primary key during insert operation. But if you want hibernate to set primary key then use @generatedvalue which is you said have been using. – Vipul Paralikar Nov 27 '13 at 06:28
  • when I am giving primary key value from user interface,and inserting it using hibernate with annotation it is not executing insert query.Is it necessary in hibernate that we cannot assign value to primary key it is auto generated. – Ashish Nayal Nov 27 '13 at 06:32
  • refer to http://stackoverflow.com/questions/3194721/bypass-generatedvalue-in-hibernate-merge-data-not-in-db – Vipul Paralikar Nov 27 '13 at 06:43
  • i am not able to get what u are trying to explain from the page u referred – Ashish Nayal Nov 27 '13 at 06:57
  • it explains where you can define your own ID generator and if the ID is not set, you delegate the generation to the default generator i.e. hibernate will generate the ID – Vipul Paralikar Nov 27 '13 at 07:09