2017-10-07 131 views
1

我有一个第三方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); 
    } 
} 

由于它是一个第三方的,我必须有会话扫描以及所以我有这个在我的@配置类:

@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个答案都不适用。

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

UPDATE 我试图@sam回答以下无济于事,但我能看一下这个第三方jar文件的源代码,它看起来像这样:

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

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

回答

2

Spring Boot - Hibernate - Table does not exists

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

我的事情的问题与您的程序@EntityScan(basePackages = “com.third.party.entity。”)不正确,其中没有acctable在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 says that table doesn't exist but it does

+0

我无法设置DDL-AUTO更新,因为我无法更改数据库。排除HibernateJpaAutoConfiguration只是给了我一个未找到的类型为'javax.persistence.EntityManagerFactory'的bean的bean' – mmaceachran

+0

@mmaceachran你检查@EntityScan(basePackages =“com.third.party.entity.package”)是否正确! – 2017-10-08 00:41:02

+0

是的,那是正确的包装。例如,我刚刚重命名了它。我编辑了我的问题,以便显示User类。 – mmaceachran

0

真正的答案(对我来说),可能类似类型的问题帮助别人不要使用隐式命名策略。如果你只是想用什么在实体类注释,使用物理一个这样的:

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

得益于@rsinha答案在这里:

Hibernate naming strategy changing table names