报错
could not execute statement; nested exception is org.hibernate.exception.GenericJDBCException: could not execute statement
原因:
实体类中设置类 主键自增,但是在数据库中没有
@Entity // 表示这是一个数据对象类
@Table(name = "person")
public class Person_jpa {
@Id // 该字段对应数据库中的列为主键
@GeneratedValue(strategy = GenerationType.IDENTITY) // 主键自增长
@Column(name = "id")
private int id;
private String name;
private int age;
private String user_name;
private String password;
//get、set略
}
DROP TABLE IF EXISTS `person`;
CREATE TABLE `person` (
`id` int(100) NOT NULL,
`name` varchar(50) DEFAULT NULL,
`age` int(11) DEFAULT NULL,
`user_name` varchar(20) DEFAULT NULL,
`password` varchar(1000) DEFAULT NULL,
PRIMARY KEY (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=6 DEFAULT CHARSET=utf8;
解决:
方案一:重建表 数据库中的person表设置主键 自增
方案二:alter table person modify id integer auto_increment
DROP TABLE IF EXISTS `person`;
CREATE TABLE `person` (
`id` int(100) NOT NULL AUTO_INCREMENT,
`name` varchar(50) DEFAULT NULL,
`age` int(11) DEFAULT NULL,
`user_name` varchar(20) DEFAULT NULL,
`password` varchar(1000) DEFAULT NULL,
PRIMARY KEY (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=6 DEFAULT CHARSET=utf8;