java - Spring Boot - hibernate - 表不存在

标签 java spring hibernate spring-boot spring-data-jpa

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

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

由于它是第三方,我还必须进行 session 扫描,所以我在 @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

现在表名是“用户”,所以这很有意义。但是,我将此 jar 用于另一个应用程序,因此关于此主题的所有其他 100 个答案都不适用。

一定是配置问题?正确的?

更新 我在下面尝试@sam 回答无济于事,但我能够查看这个 3rd 方 jar 文件的源代码,它看起来像这样:

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

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

最佳答案

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

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

感谢@rsinha 在这里的回答:

Hibernate naming strategy changing table names

关于java - Spring Boot - hibernate - 表不存在,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46625996/

相关文章:

java - 在 String 中添加数字而忽略其字母

java - Kotlin 比 java 更难逆向工程吗

java - 无论如何@Inject/@Autowire 一个内部类到一个外部类?

eclipse - org.springframework.beans.factory.BeanCreationException : Error creating bean with name 'transactionManager

java - 在 Java 中使用 ASM 监控对象创建

java - Libgdx - 使用TexturePacker时出现错误

spring - Spring Webflux中有没有静态的方法来获取当前的ServerHttpRequest?

java - 使用 Maven 和 Eclipse 制作我的第一个 Web 项目时遇到问题

java - 在哪里可以找到 Hibernate 的 DTD?

java - 在 HibernateDaoSupport 中使用 in 子句