java Spring Boot - Hibernate - 表不存在
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/46625996/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Spring Boot - Hibernate - Table does not exists
提问by mmaceachran
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)
我有一个包含所有实体和映射的 3rd 方 jar。我目前在经典的 Spring-MVC 应用程序中成功使用了这个 jar,但现在我正在尝试在 Spring-Boot 应用程序中使用它。(1.5.7.发布)
I have this for my Applicaion.java:
我的 Applicion.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:
因为它是第三方,所以我也必须进行会话扫描,所以我的 @Configuration 类中有这个:
@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:
这在我的 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.
现在表名是“用户”,这样就有意义了。但是,我将此 jar 用于另一个应用程序,因此有关此主题的所有其他 100 个答案均不适用。
It MUST be a configuration issue? Right?
一定是配置问题?对?
UPDATEI 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:
更新我尝试了下面的@sam 答案但无济于事,但我能够查看这个 3rd 方 jar 文件的源代码,它看起来像这样:
@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?
所以注解中的表名是正确的。我如何让 Spring 使用它?我正在寻找一个默认的命名策略和东西......有什么建议吗?
采纳答案by mmaceachran
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:
感谢@rsinha 的回答:
回答by mmaceachran
Spring Boot - Hibernate - Table does not exists
Spring Boot - Hibernate - 表不存在
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 packagenot acceptable as a package name in java.
我的事情与你的程序@EntityScan问题(basePackages =“com.third.party.entity。包”)不正确,其中包不接受的,在Java中的包名。
Else
别的
Make sure your pojo entity userclass is properly defined
确保您的 pojo 实体用户类已正确定义
@Entity
@Table(name = "User")
public class User implements Serializable {
private static final long serialVersionUID = 1L;
...
}
Try to add below line code in application.propertiesand run
尝试在application.properties 中添加以下行代码并运行
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
并通过添加下面的注释排除 Hibernate AutoConfiguration 类,以防它在那里
@SpringBootApplication(exclude={HibernateJpaAutoConfiguration.class})
This question is similar to Hibernate says that table doesn't exist but it does
这个问题类似于 Hibernate 说 table 不存在但它确实存在
回答by SnuKies
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
我遇到了同样的问题,但有不同的解决方案:显然,Hibernate/JPA 将表名中的所有字母小写。所以即使我的桌子User
和我有
@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
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)
我的解决方案是将数据库中的表名从User
更改为user
(大写 -> 小写)
NOTE: I was using different Entity name for my table (That's why I have OfficeUser
as class' name and different table name)
注意:我为我的表使用了不同的实体名称(这就是为什么我有OfficeUser
类名和不同的表名)
回答by user9869932
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.
隐藏在异常堆栈跟踪中,我发现模型中的一个属性被命名为 SQL 保留字。这会阻止创建表。
Renaming the attribute solved the issue.
重命名属性解决了这个问题。
回答by Vaibs
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.
我遇到了类似的问题,原因是我的实体对象有一个名为“group”的类变量,它是一个 mysql 关键字。由于我没有用 @Column 注释它以给出一些不同的名称,它被迫使用“组”作为列名。解决方案是直接用@Column 注释类变量以赋予它不同的名称。
Before
前
private String group;
After
后
@Column(name="groupName")
private String group;