spring + hibernate integration表缺少表“table_name”

时间:2016-03-21 10:47:50

标签: mysql spring hibernate jpa

我将spring与hibernate集成,我正在使用dropwizard configaration文件:持久性文件配置文件在资源文件夹中没有加载xml文件的问题。找到表有问题。         persistence.xml文件为:

<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 application config 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.I它连接到数据库但是找不到表。表名和colume名完全相同。数据库名称也正确,数据库的用户名和密码没有问题。

1 个答案:

答案 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
}