2016-03-21 127 views
5

我整合春天與休眠,我使用dropwizard配置文件爲:持久性文件有配置文件在資源文件夾加載xml文件沒有問題。發現表有問題。 persistence.xml文件爲:春季+休眠集成表「table_name」缺失

<persistence-unit name="bfm" transaction-type="RESOURCE_LOCAL" > 
<provider>org.hibernate.ejb.HibernatePersistence</provider> 
<class>com.bcits.parkingmanagement.model.User</class> 
    <exclude-unlisted-classes>true</exclude-unlisted-classes> 
<properties> 
     <!-- Configuring JDBC properties --> 
     <property name="javax.persistence.jdbc.url" value="jdbc:mysql://localhost:3306/anuj" /> 
     <property name="javax.persistence.jdbc.user" value="root" /> 
     <property name="javax.persistence.jdbc.password" value="root" /> 
     <property name="javax.persistence.jdbc.driver" value="com.mysql.jdbc.Driver" /> 
</persistence-unit> 
</persistence> 

在Spring應用程序配置XML爲:在那裏我只顯示Hibernate配置

<context:annotation-config /> 
    <context:component-scan base-package="com.bcits.parkingmanagement.model" /> 

    <jdbc:embedded-database id="dataSource" type="HSQL" /> 


    <bean id="entityManagerFactory" class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean"> 
     <property name="persistenceXmlLocation" value="classpath:spring/persistence.xml" /> 
     <property name="dataSource" ref="dataSource"/> 
    </bean> 

    <bean id="transactionManager" class="org.springframework.orm.jpa.JpaTransactionManager"> 
     <property name="entityManagerFactory" ref="entityManagerFactory" /> 
    </bean> 

    <bean id="entityManager" 
     class="org.springframework.orm.jpa.support.SharedEntityManagerBean"> 
     <property name="entityManagerFactory" ref="entityManagerFactory" /> 
    </bean> 

表用戶丟失

javax.persistence.PersistenceException: [PersistenceUnit: bfm] Unable to build EntityManagerFactory 
org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.initializeBean(AbstractAutowireCapableBeanFactory.java:1562) 
    ... 18 more 
Caused by: org.hibernate.HibernateException: Missing table: user 

請任何人幫助我如何解決這個錯誤。我想用hibernate配置Spring。它是連接數據庫但不能找到表的東西。表名和列名完全相同。數據庫名稱也正確,數據庫的用戶名和密碼沒有問題。

+0

顯示您的用戶模型類。其次。你有一個名爲Anuj的數據庫和表名爲user的用戶嗎? –

+0

是 「@」 實體 「@」 表(名稱= 「用戶」) 公共類用戶{ \t \t 「@」 標識 \t 「@」 欄目(NAME = 「USER_ID」) \t私人int id; \t「@」列(名稱=「user_name」) \t私人字符串名稱; // getter and setter} –

+0

編輯您的主帖以粘貼新內容。 –

回答

4
User model is: Here User is model name and user is table which have two column one is user_id and user_name. Issue is that i had used user instead of User in query. Because User is model name and user is table name .Correct model is given below 

@NamedQueries({ 
    @NamedQuery(name="getUserName" , query="select name from User") 
    }) 

@Entity 
@Table(name ="user") 
public class User { 
    @Id 
    @Column(name="user_id") 
    private int id; 
    @Column(name="user_name") 
    private String name; 

    //getter setter 
}