Spring Boot - Hibernate - 数据表不存在。

21

我有一个第三方jar包,其中包含所有的实体和映射关系。我目前在一个传统的Spring-MVC应用程序中成功使用这个jar包,但现在我正在尝试在一个Spring-Boot应用程序中使用它。(1.5.7.RELEASE)

这是我的Application.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);
    }
}

由于它是第三方,所以我必须进行会话扫描,因此我在我的@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;
}

这是我的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

我正在遇到这个错误:
Caused by: com.mysql.jdbc.exceptions.jdbc4.MySQLSyntaxErrorException: Table 'mydb.user' doesn't exist

现在表名为“User”,这有点说得通。但是,我正在为另一个应用程序使用此jar文件,因此所有其他100个关于此主题的答案均不适用。

这一定是配置问题了吧?对吗?

更新 我尝试了下面@sam的回答,但没有成功,但我能够查看此第三方jar文件的源代码,它看起来像这样:

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

因此,注释中的表名是正确的。我该如何让Spring使用它?我正在查找默认命名策略之类的东西... 有什么建议吗?

7个回答

21

对我而言,可能会对某些人有帮助的真正答案是根本不使用隐式命名策略。如果你只想使用实体类中注释的内容,则可以像这样使用物理命名策略:

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

感谢@rsinha在这里的回答:

Hibernate命名策略更改表名


9
Spring Boot - Hibernate - 表不存在
Caused by: com.mysql.jdbc.exceptions.jdbc4.MySQLSyntaxErrorException: Table 'mydb.user' **doesn't exist**

我认为您的程序中 @EntityScan(basePackages = "com.third.party.entity.package") 的问题在于 package 不是 Java 中可接受的包名称。 否则,请确保您的 pojo 实体 user 类已正确定义。
@Entity
@Table(name = "User")
public class User implements Serializable {
    private static final long serialVersionUID = 1L;
    ...
    }

尝试在 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

如果存在Hibernate AutoConfiguration类,请通过添加下面的注释来排除它

@SpringBootApplication(exclude={HibernateJpaAutoConfiguration.class})

这个问题类似于Hibernate报告表不存在,但实际上存在。它涉及编程和数据库相关的内容。

我无法将ddl-auto设置为update,因为我无法更改数据库。排除HibernateJpaAutoConfiguration只会给我一个未找到的bean,类型为'javax.persistence.EntityManagerFactory'。 - mmaceachran
@mmaceachran 你检查过 @EntityScan(basePackages = "com.third.party.entity.package") 是否正确了吗!! - user4856296
是的,那就是正确的包。我只是为了举例重新命名了它。而且我编辑了我的问题,展示了一下User类。 - mmaceachran

5

我遇到了同样的问题,但采用了不同的解决方案:显然,Hibernate/JPA会将表名中的所有字母都转换为小写。因此,即使我的表名是User,我也必须在代码中使用@Table(name = "user")来引用它。

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

我仍然会收到错误信息,如下所示: com.mysql.jdbc.exceptions.jdbc4.MySQLSyntaxErrorException: Table 'doctors_office.user' doesn't exist 对于我来说,解决方案是在数据库中更改表的名称,从User改为user(大写->小写)。
注意:我为我的表使用了不同的Entity名称(这就是为什么我有OfficeUser作为类名和不同的表名)。

3
请在您的application.properties中添加以下属性:
spring.jpa.generate-ddl=true

2

我尝试了上面所有的解决方案,但最后发现错误来自一个更简单的原因。

在异常堆栈中隐藏着一个信息,我发现模型中有一个属性被命名为SQL保留字,这导致表无法创建。

重命名该属性解决了问题。

“Original Answer”翻译成“最初的回答”。


1
对我来说也是一样的。我不知道为什么他们采用小写,然而 MySQL 是不区分大小写的。 - Prashant
我遇到了完全相同的问题,我的一个列名为KEY,而hibernate无法创建表格。感谢您的评论。 - Vinicius Souza

1

在我尝试了这里提供的所有建议之后,直到我使用了

spring.jpa.generate-ddl=true

在我的application.properties文件中。


这对我也起作用了,这行代码是做什么的? - Aaron

1
我遇到了类似的问题,原因是我的实体对象有一个名为“group”的类变量,它是mysql关键字。由于我没有使用@Column进行注释以给出不同的名称,所以它被强制使用“group”作为列名称。解决方案很简单,只需使用@Column注释类变量以赋予它一个不同的名称。

之前

 private String group;

之后

  @Column(name="groupName")
    private String group;

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接