Spring Boot - Hibernate - 表不存在

mma*_*ran 12 java spring hibernate spring-data-jpa spring-boot

我有一个第三方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);
    }
}
Run Code Online (Sandbox Code Playgroud)

由于它是第三方,我必须进行会话扫描,所以我在@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;
}
Run Code Online (Sandbox Code Playgroud)

这在我的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
Run Code Online (Sandbox Code Playgroud)

我收到此错误:

Caused by: com.mysql.jdbc.exceptions.jdbc4.MySQLSyntaxErrorException: Table 'mydb.user' doesn't exist
Run Code Online (Sandbox Code Playgroud)

现在表名是"用户",所以这样才有意义.但是,我正在将此jar用于其他应用程序,因此关于此主题的所有其他100个答案都不适用.

它必须是一个配置问题?对?

更新 我尝试下面的@sam答案无济于事,但我能够看到这个第三方jar文件的源代码,它看起来像这样:

@Entity
@Table(name = "User")
public class User {
     etc...
}
Run Code Online (Sandbox Code Playgroud)

因此注释中的表名是正确的.我怎样让Spring使用它?我正在寻找默认的命名策略和东西...任何建议?

mma*_*ran 10

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

spring.jpa.hibernate.naming.physical-strategy=org.hibernate.boot.model.naming.PhysicalNamingStrategyStandardImpl
Run Code Online (Sandbox Code Playgroud)

感谢@rsinha在这里回答:

Hibernate命名策略更改表名


小智 5

Spring Boot-休眠-表不存在

Caused by: com.mysql.jdbc.exceptions.jdbc4.MySQLSyntaxErrorException: Table 'mydb.user' **doesn't exist**
Run Code Online (Sandbox Code Playgroud)

我的事情与你的程序@EntityScan问题(basePackages =“com.third.party.entity。包 ”)不正确,其中包不接受的,在Java中的包名。

其他

确保正确定义了pojo实体用户类

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

尝试在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
Run Code Online (Sandbox Code Playgroud)

并在下面添加“注释”,以排除Hibernate AutoConfiguration类(如果有的话)

@SpringBootApplication(exclude={HibernateJpaAutoConfiguration.class})
Run Code Online (Sandbox Code Playgroud)

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


Snu*_*ies 5

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

@Entity
@Table(name = "User")
public class OfficeUser {
...
}
Run Code Online (Sandbox Code Playgroud)

我仍然会收到错误消息:

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

我的解决方案是将数据库中的表名从User更改为user(大写 - > 小写)

注意:我为我的表使用了不同的实体名称(这就是为什么我有OfficeUser类名和不同的表名)