弹簧靴 - 休眠 - 表不存在

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

我有这个用于我的应用程序.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;
}

和这个在我的应用程序.属性:

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使用它?我正在寻找一个默认的命名策略和东西...有什么建议吗?


答案 1

可能帮助某人的真正答案(对我来说)是根本不使用隐式命名策略。如果只想使用实体类中批注的内容,请使用如下所示的物理类:

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

感谢这里@rsinha答案:

休眠命名策略更改表名


答案 2

弹簧靴 - 休眠 - 表不存在

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 中添加以下行代码并运行

应用程序.属性

# 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

并排除休眠自动配置类,以防它在那里,通过添加下面的注释

@SpringBootApplication(exclude={HibernateJpaAutoConfiguration.class})

这个问题类似于Hibernate说该表不存在,但它确实存在


推荐