Spring Boot - 休眠 - 表不存在

新手上路,请多包涵

我有一个包含所有实体和映射的第 3 方 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 使用它?我正在寻找默认的命名策略和东西……有什么建议吗?

原文由 mmaceachran 发布,翻译遵循 CC BY-SA 4.0 许可协议

阅读 590
2 个回答

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

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

感谢@rsinha 的回答:

Hibernate 命名策略更改表名

原文由 mmaceachran 发布,翻译遵循 CC BY-SA 3.0 许可协议

Spring Boot - 休眠 - 表不存在

Caused by: com.mysql.jdbc.exceptions.jdbc4.MySQLSyntaxErrorException: Table 'mydb.user' **doesn't exist**

我认为你的程序 @EntityScan(basePackages = “ com.third.party.entity.package “) 的问题是不正确的,其中 不能作为 java 中的包名称接受。

别的

确保您的 pojo 实体 用户 类已正确定义

@Entity
@Table(name = "User")
public class User implements Serializable {
    private static final long serialVersionUID = 1L;
    ...
    }

尝试在 application.properties 中添加以下行代码并运行

应用程序.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 says that table doesn’t exist but it does

原文由 user4856296 发布,翻译遵循 CC BY-SA 4.0 许可协议

撰写回答
你尚未登录,登录后可以
  • 和开发者交流问题的细节
  • 关注并接收问题和回答的更新提醒
  • 参与内容的编辑和改进,让解决方法与时俱进
推荐问题