Spring Boot - Hibernate - Table does not exists
Asked Answered
G

8

21

I have a 3rd party jar that holds all the entities and mapping. I am currently using this jar successfully in a classic Spring-MVC application, but now I am trying to use it in a Spring-Boot application. (1.5.7.RELEASE)

I have this for my Applicaion.java:

@SpringBootApplication
@EntityScan(basePackages = "com.third.party.entity.package")
public class Application extends SpringBootServletInitializer {

    public static void main(String[] args) {
      SpringApplication.run(Application.class, args);
    }
}

As it is a third party, I have to have the session scan as well so I have this in my @Configuration class:

@Bean
public LocalSessionFactoryBean sessionFactory(EntityManagerFactory emf) throws ClassNotFoundException {
    LocalSessionFactoryBean fact = new LocalSessionFactoryBean();
    fact.setAnnotatedPackages("com.third.party.entity.package");
    fact.setPackagesToScan("com.third.party.entity.package");
    fact.setDataSource(dataSource);
    return fact;
}

and this in my application.properties:

spring.datasource.url=jdbc:mysql://dbserver:3306/mydb?useSSL=false
spring.datasource.username=user
spring.datasource.password=password
spring.datasource.tomcat.max-wait=20000
spring.datasource.tomcat.max-active=50
spring.datasource.tomcat.max-idle=20
spring.datasource.tomcat.min-idle=15
spring.jpa.properties.hibernate.dialect =org.hibernate.dialect.MySQL5Dialect
spring.jpa.properties.hibernate.id.new_generator_mappings = false
spring.jpa.properties.hibernate.format_sql = true
spring.jpa.properties.hibernate.current_session_context_class=org.springframework.orm.hibernate5.SpringSessionContext

I am getting this error:

Caused by: com.mysql.jdbc.exceptions.jdbc4.MySQLSyntaxErrorException: Table 'mydb.user' doesn't exist

Now the table name is "User" so that sort of makes sense. However, I am using this jar for another application so all of the other 100 answers on this subject do not apply.

It MUST be a configuration issue? Right?

UPDATE I tried @sam answer below to no avail, but I was able to look at the source code of this 3rd party jar file, and it looks like this:

@Entity
@Table(name = "User")
public class User {
     etc...
}

So the table name in the annotation is correct. How do I get Spring to use that? Im looking a default naming strategies and stuff... Any Suggestions?

Grounder answered 7/10, 2017 at 23:13 Comment(0)
G
21

The real answer (for me) that may help someone is not to use an implicit naming strategy at all. If you just want to use what is annotated in the entity class, use a physical one like this:

spring.jpa.hibernate.naming.physical-strategy=org.hibernate.boot.model.naming.PhysicalNamingStrategyStandardImpl

Thanks to the @rsinha answer here:

Hibernate naming strategy changing table names

Grounder answered 11/10, 2017 at 15:35 Comment(0)
H
9

Spring Boot - Hibernate - Table does not exists

Caused by: com.mysql.jdbc.exceptions.jdbc4.MySQLSyntaxErrorException: Table 'mydb.user' **doesn't exist**

I thing the issue with your program @EntityScan(basePackages = "com.third.party.entity.package") is incorrect where package not acceptable as a package name in java.

Else

Make sure your pojo entity user class is properly defined

@Entity
@Table(name = "User")
public class User implements Serializable {
    private static final long serialVersionUID = 1L;
    ...
    }

Try to add below line code in application.properties and run

application.properties

# Hibernate ddl auto (create, create-drop, update, none): with "update" the database
# schema will be automatically updated accordingly to java entities found in the project
spring.jpa.hibernate.ddl-auto = update

And exclude the Hibernate AutoConfiguration class in case it is there, by adding the Annotation below

@SpringBootApplication(exclude={HibernateJpaAutoConfiguration.class})

This question is similar to Hibernate says that table doesn't exist but it does

Honduras answered 7/10, 2017 at 23:28 Comment(3)
I cannot set ddl-auto to update, as I cannot change the DB. Excluding HibernateJpaAutoConfiguration just gives me a bean not found bean of type 'javax.persistence.EntityManagerFactory'Grounder
@Grounder Did you check @EntityScan(basePackages = "com.third.party.entity.package") is correct!!Honduras
yes, that is the correct package. I just renamed it for example. And I edited my question so show the User class a bit.Grounder
H
5

I had the same issue, but a different solution: Apparently, Hibernate/JPA lower-cases all the letters in the table name. So even if my table was User and I had

@Entity
@Table(name = "User")
public class OfficeUser {
...
}

I'd still get the error saying :

com.mysql.jdbc.exceptions.jdbc4.MySQLSyntaxErrorException: Table 'doctors_office.user' doesn't exist

The solution for me was to change the table's name in DB from User to user (uppercase -> lowercase)

NOTE: I was using different Entity name for my table (That's why I have OfficeUser as class' name and different table name)

Heaver answered 1/4, 2018 at 7:41 Comment(0)
C
4

Please add below property in your application.properties

spring.jpa.generate-ddl=true
Camillecamilo answered 22/4, 2022 at 5:41 Comment(0)
R
2

I tried all the solutions above, but in the end the error was coming from a much simpler reason.

Hidden in the Exception Stacktrace I found that one of the attributes in the Model was named as a SQL reserved word. This was preventing the table from being created.

Renaming the attribute solved the issue.

Rydder answered 25/5, 2019 at 5:10 Comment(2)
for me also it was same. i don't know why they are taking in lowercase however mysql is not case sensitive.Lagos
I was having exactly the same issue, one of my columns was named KEY, and hibernate wasn't able to create the table.Thanks for your comment.Subfloor
S
1

I faced the similar issue and the reason was that my Entity Object was having a class variable named "group" which is a mysql keyword. As i haven't annotated it with @Column to give some different name , it was forced to use "group" as a column name. Solution was straight forward to annotate the class variable with @Column to give it a different name.

Before

 private String group;

After

  @Column(name="groupName")
    private String group;
Subglacial answered 8/2, 2019 at 10:9 Comment(0)
C
1

None of the suggestions here worked for me until I put

spring.jpa.generate-ddl=true

in my application.properties file.

Castled answered 20/4, 2022 at 18:39 Comment(1)
This also worked for me, what does this line do?Human
B
0

Are you trying to build your app in docker?? then set up this in application.properties

spring.datasource.url=jdbc:mysql://localhost:3306/customerbank?
autoReconnect=true&AllowPublicKeyRetrieval=true&useSSL=false
spring.datasource.username=vishal
spring.datasource.password=Vishal@123
spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver
spring.jpa.hibernate.ddl-auto=update
spring.jpa.properties.hibernate.dialect =org.hibernate.dialect.MySQL5Dialect
spring.jpa.show-sql=true
Breakwater answered 25/2 at 19:19 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.