'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?
Solution 1:[1]
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:
Solution 2:[2]
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
Solution 3:[3]
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)
Solution 4:[4]
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.
Solution 5:[5]
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;
Solution 6:[6]
None of the suggestions here worked for me until I put
spring.jpa.generate-ddl=true
in my application.properties file.
Solution 7:[7]
Please add below property in your application.properties
spring.jpa.generate-ddl=true
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
Solution | Source |
---|---|
Solution 1 | mmaceachran |
Solution 2 | user9869932 |
Solution 3 | SnuKies |
Solution 4 | user9869932 |
Solution 5 | Vaibs |
Solution 6 | larisoft |
Solution 7 | Suraj Rao |