Discussions

I have a requirement where in I need to generate Unique Id for each row as and when new record is created. that Id is not set to AUTO INCREMENT in DB.so i tried to use @TableGenerator annotation but it is failing saying
java.sql.SQLException: Incorrect integer value: 'id' for column 'GEN_KEY' at row 1
I am using mysql database and the schema name is HSS. I configured like this in my corresponding entity bean.
@TableGenerator(
name="keyGen",
table="SEQUENCE_TABLE",
pkColumnName="SEQUENCE_NAME",
valueColumnName="SEQUENCE_COUNT",
pkColumnValue="id")
@Id
@GeneratedValue(strategy = GenerationType.TABLE,generator="keyGen")
public Integer getId() {
return this.id;
}
id is the primary key of the table.
I have also created SEQUENCE_TABLE in my HSS schema with both the fields SEQUENCE_NAME and SEQUENCE_COUNT being made as Integer data type.
can some body help as what is wrong here.

Harish, there seems to be an issue in your DB Table Configuration. As per your @TableGenerator definition, SQL should refer to a table "HSS.SEQUENCE_TABLE" with Primary Column as 'SEQUENCE_NAME' having a row with value 'id' and fetch an Integer value from the SEQUENCE_COUNT.
SEQUENCE_NAME must be of type String ie VARCHAR which can store the name of the Sequence, "id" in your case, I guess your table have Integer instead.
To be more Generic let the Persistence API create the Table for you.
Also try mentioning the schema with @TableGenerator as
@TableGenerator(
name="keyGen",
schema="HSS"
table="SEQUENCE_TABLE", ...
You can also try another strategy like
@GeneratedValue(strategy = GenerationType.AUTO) or GenerationType.SEQUENCE
Hope it will help
Sarabjit S. Rupaal

TechTarget provides technology professionals with the information they need to perform their jobs - from developing strategy, to making cost-effective purchase decisions and managing their organizations technology projects - with its network of technology-specific websites, events and online magazines.