Pranavadurai
Pranavadurai

Reputation: 1030

insert a data and get back ID in spring boot JPA

Hi i am trying to insert a record in Mysql and get the ID of the inserted user in Spring boot JPA. i am getting following error. i have seen couple of questions like this but there they provided answer as USE JDBC Template for these kind of output. is it not possible to do it??. or this is related to some other issue.

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";
 }

Model

  @Entity

public class User {

@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private int id;
private String name;
private String email;
private String location;

public User() {

}
}

Error

      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]

UPDATE

application properties

 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=**********

This the message from server log

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

Upvotes: 2

Views: 6735

Answers (2)

Pranavadurai
Pranavadurai

Reputation: 1030

After lot of search and try i found that User Table is default table in Postgresql. that's why it did not allowed. i have changed the table name and it worked. is there any other way is there to reuse it ?.

Upvotes: 2

lzagkaretos
lzagkaretos

Reputation: 2910

Make sure you have specified the correct dialect for hibernate and MySQL. It seems that value for identity column can't be set correctly.

For instance, if you use Spring Boot, check your application properties for something like:

spring.jpa.properties.hibernate.dialect = org.hibernate.dialect.MySQL5Dialect

Or if you use Spring MVC without Spring Boot, that you have:

hibernateJpa.setDatabasePlatform("org.hibernate.dialect.MySQL5Dialect");

in LocalContainerEntityManagerFactoryBean configuration.

Upvotes: 0

Related Questions