Spring Boot - Hibernate - 表不存在

问题描述 投票:0回答:8

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

我的 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);
    }
}

由于它是第三方,我也必须进行会话扫描,所以我在我的 @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 答案,但没有成功,但我能够查看这个第 3 方 jar 文件的源代码,它看起来像这样:

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

所以注释中的表名是正确的。我如何让 Spring 使用它?我正在寻找默认的命名策略和东西......有什么建议吗?

java spring hibernate spring-boot spring-data-jpa
8个回答
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 中添加以下行代码并运行

应用程序属性

# 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 说表不存在但它确实存在


5
投票

我有同样的问题,但有不同的解决方案:显然,Hibernate/JPA 将表名称中的所有字母小写。所以即使我的桌子是

User
并且我有

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

我仍然收到错误消息:

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

我的解决方案是将数据库中的表名称从

User
更改为
user
(大写 -> 小写)

注意:我为表使用了不同的实体名称(这就是为什么我将

OfficeUser
作为类名称和不同的表名称)


3
投票

请在您的 application.properties 中添加以下属性

spring.jpa.generate-ddl=true

2
投票

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

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

重命名属性解决了问题。


1
投票

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

之前

 private String group;

之后

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

1
投票

在我提出之前,这里的建议都不适合我

spring.jpa.generate-ddl=true

在我的 application.properties 文件中。


0
投票

您是否尝试在 docker 中构建您的应用程序? 然后在 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
© www.soinside.com 2019 - 2024. All rights reserved.