java - 在 Spring Boot JPA 中插入数据并获取 ID

标签 java mysql spring hibernate spring-data-jpa

嗨,我正在尝试在 Mysql 中插入一条记录,并在 Spring boot JPA 中获取插入用户的 ID。我收到以下错误。我见过几个这样的问题,但他们为此类输出提供了使用 JDBC 模板的答案。难道做不到吗??或者这与其他一些问题有关。

Controller

@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

公共(public)类用户{

@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月

最佳答案

经过大量搜索和尝试,我发现用户表是 Postgresql 中的默认表。这就是为什么它不允许。我已经更改了表名称并且它起作用了。还有其他方法可以重用它吗?

关于java - 在 Spring Boot JPA 中插入数据并获取 ID,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48549149/

相关文章:

php - 如何处理多个表而不获取重复数据? (MySQL/PDO)

mysql - 哪个更快?组合查询还是多个查询?

java - 作业参数正在被缓存

java - 从 jsp 查询 LDAP

java - 如何从 J2ME 应用程序访问通话记录?

Java - 'continue' 特定超时时间后循环迭代

php - 这个 ezSQL 查询有什么问题?

java - 在 Jersey 客户端 API 中使用 BeanParam

java/spring 打印出 bean 属性值

spring - Graphql工具:将实体类型映射到graphql类型