mmaceachran
mmaceachran

Reputation: 3338

Spring Boot - Hibernate - Table does not exists

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?

Upvotes: 21

Views: 45635

Answers (9)

Ashish Singh
Ashish Singh

Reputation: 1

This issue occurs when using MySQL versions above 5 (e.g., MySQL 7, 8 in my case). The problem arises if the same table exists in different databases. When using a different database and trying to create a table with the same name as one in previous database, Spring jpa will not create the table and will throw an exception.

Solution:

  1. Downgrade MySQL version to 5.5 or 5, and update the MySQL jars to the appropriate version (5.5 or 5).
  2. If you don't want to downgrade MySQL, consider changing the table name in your entity class.
  3. if you don't want above 2 steps then manually create table in MySQL and it will work 100%

Upvotes: 0

Vishal Bramhankar
Vishal Bramhankar

Reputation: 253

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

Upvotes: 0

Devashish Sharma
Devashish Sharma

Reputation: 41

Please add below property in your application.properties

spring.jpa.generate-ddl=true

Upvotes: 4

larisoft
larisoft

Reputation: 191

None of the suggestions here worked for me until I put

spring.jpa.generate-ddl=true

in my application.properties file.

Upvotes: 1

user9869932
user9869932

Reputation: 7327

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.

Upvotes: 2

user4856296
user4856296

Reputation:

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

Upvotes: 9

Vaibs
Vaibs

Reputation: 2096

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;

Upvotes: 1

SnuKies
SnuKies

Reputation: 1723

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)

Upvotes: 5

mmaceachran
mmaceachran

Reputation: 3338

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

Upvotes: 21

Related Questions