Thursday, May 22, 2014

Create own ID column auto managed by JPA

By default, JPA uses Id column, but I want to create my own id column named userid for my user table. I am always confused with Id column in multiple tables so I append entity name before id in order to know which one I am referring. For an example, for user table I use userid, for customer, I use customerid and so on.

For that, I created a new id column for JPA entity and annotated it with @Id.

 @Id
 @GeneratedValue(strategy = GenerationType.AUTO)
 @Column(name = "user_id")
 private Integer userid;


But it didn't work for me. After searching the web for some time, I found out that I should use full package name for ID. in the place of @Id, I had to use @javax.persistence.Id The resulting code was this

 @javax.persistence.Id
 @GeneratedValue(strategy = GenerationType.AUTO)
 @Column(name = "user_id")
 private Integer userid;

Now the code works fine.

No comments:

Post a Comment