2017-10-07 161 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