I use Scaffolding in MyEclipse to generate Web Application project, using spring MVC and MySQL database. Please give me some clues to solve problem bellow.
I get problem with id primary key. It doesn't auto increment event if i use:
@GeneratedValue(strategy=GenerationType.IDENTITY)
@GeneratedValue(strategy=GenerationType.AUTO)
@GeneratedValue(strategy=GenerationType.SEQUENCE)
@GeneratedValue(strategy=GenerationType.TABLE)
I have several tables with the primary key set to AUTO_INCREMENT. Like this example:
CREATE TABLE IF NOT EXISTS `ehealthdb`.`timestamp` (
`id` BIGINT(20) NOT NULL AUTO_INCREMENT COMMENT '',
`login_date` DATETIME NULL COMMENT '',
`logout_date` DATETIME NULL COMMENT '',
`create_date` DATETIME NOT NULL COMMENT '',
`update_date` DATETIME NULL COMMENT '',
`isActive` TINYINT(1) NULL COMMENT '',
PRIMARY KEY (`id`) COMMENT '')
ENGINE = InnoDB;
When I try to use the save using the generated post URL and the following json payload:
{
"loginDate":"1374839856000",
"logoutDate":"1374839856000",
"createDate":"1374839856000",
"updateDate":"1374839856000",
"isActive":true
}
Request processing failed; nested exception is javax.persistence.PersistenceException:
org.hibernate.id.IdentifierGenerationException: ids for this class
must be manually assigned before calling save():
ehealth.domain.Timestamp
I think this is caused by a missing @GeneratedValue, which scaffolding has not created on my primary keys. I had to add it in manually like below:
But It's still not work. It's still get error.
@Id
@GeneratedValue(strategy=GenerationType.IDENTITY) //add this
@Column(name = "id", unique = true, nullable = false)
@Basic(fetch = FetchType.EAGER)
@XmlElement
Integer id;
Best Answer
Don't try to set the ID field at all. It's handled automatically.
Use @GeneratedValue(strategy=GenerationType.IDENTITY). Make sure your Persistence.xml has
Should probably be using Hibnerate version 4 or 5.
Also, I've never seen
FetchType
on an ID column. Remove this.I do this:
Also, probably should understand the full setup. What version of Spring?