您好我正在尝试在Mysql中插入一条记录,并在Spring启动JPA中获取插入用户的ID。我得到了以下错误。我已经看过这样的几个问题,但是他们为这些输出提供了USE JDBC模板的答案。是不是可以这样做?或者这与其他一些问题有关。
控制器
@RequestMapping(value = "/signup", method = RequestMethod.POST)
public String createsignup(@RequestParam String name,@RequestParam String email,@RequestParam String password, ModelMap model) {
int userid = 0;
User user = new User(name,email);
userrepository.save(user);
userid = user.getId();
Authentication auth = new Authentication(email,password,userid);
authrepository.save(auth);
model.put("remember_token", auth.getRemember_token());
return "redirect:/profile";
}
模型
@Entity
公共类用户{
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private int id;
private String name;
private String email;
private String location;
public User() {
}
}
错误
018-01-31 23:45:32.890 INFO 2512 --- [nio-8080-exec-1] o.s.web.servlet.DispatcherServlet : FrameworkServlet 'dispatcherServlet': initialization completed in 7 ms
Hibernate: insert into user (email, location, name) values (?, ?, ?)
Hibernate: select next_val as id_val from hibernate_sequence for update
2018-01-31 23:45:41.783 ERROR 2512 --- [nio-8080-exec-2] o.hibernate.id.enhanced.TableStructure : could not read a hi value
com.mysql.jdbc.exceptions.jdbc4.MySQLSyntaxErrorException: Table 'blog.hibernate_sequence' doesn't exist
at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method) ~[na:1.8.0_45]
at sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:62) ~[na:1.8.0_45]
更新
应用程序属性
spring.mvc.view.prefix=/WEB-INF/jsp/
spring.mvc.view.suffix=.jsp
logging.level.org.springframework.web=INFO
spring.jpa.properties.hibernate.dialect = org.hibernate.dialect.MySQL5Dialect
spring.jpa.show-sql = true
spring.jpa.hibernate.ddl-auto=none
spring.datasource.url=jdbc:mysql://localhost:3306/blog
spring.datasource.username=pranava
spring.datasource.password=**********
来自服务器日志的消息
2018-02-01 22:21:29.691 INFO 5648 --- [ restartedMain] org.hibernate.Version : HHH000412: Hibernate Core {5.2.10.Final}
2018-02-01 22:21:29.693 INFO 5648 --- [ restartedMain] org.hibernate.cfg.Environment : HHH000206: hibernate.properties not found
o.hibernate.annotations.common.Version : HCANN000001: Hibernate Commons Annotations {5.0.1.Final}
2018-02-01 22:21:29.928 INFO 5648 --- [ restartedMain] org.hibernate.dialect.Dialect : HHH000400: Using dialect: org.hibernate.dialect.MySQL5Dialect
2018-02
答案 0 :(得分:2)
经过大量搜索并尝试我发现User Table是Postgresql中的默认表。这就是为什么不允许的原因。我已经更改了表名并且它有效。有没有其他方法可以重复使用它?。
答案 1 :(得分:0)
确保为hibernate和MySQL指定了正确的方言。似乎无法正确设置标识列的值。
例如,如果您使用Spring Boot,请检查您的应用程序属性,例如:
spring.jpa.properties.hibernate.dialect = org.hibernate.dialect.MySQL5Dialect
或者,如果您使用没有Spring Boot的Spring MVC,那么您有:
hibernateJpa.setDatabasePlatform("org.hibernate.dialect.MySQL5Dialect");
在LocalContainerEntityManagerFactoryBean
配置中。