gpt4 book ai didi

java - 在 spring boot JPA 中插入数据并取回 ID

转载 作者:行者123 更新时间:2023-11-29 02:12:17 31 4
gpt4 key购买 nike

您好,我正在尝试在 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/

31 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com