Hibernate在通过Maven Tomcat插件运行时不解析映射文件

时间:2015-09-02 03:30:03

标签: java hibernate maven tomcat intellij-idea

这在使用IntelliJ RESTFul Web服务框架支持时起作用。但是,我现在正尝试使用Tomcat插件通过Maven运行整个测试服务。

我有这个Hibernate配置文件:

             org.hibernate.dialect.PostgreSQL82Dialect         org.postgresql.Driver         JDBC:在PostgreSQL://本地主机:5432 / microservicedb         Postgres的         输入mypassword

    <!--<property name="connection_pool_size">1</property>
    <property name="hbm2ddl.auto">create</property>
    <property name="cache.provider_class">org.hibernate.cache.HashtableCacheProvider</property>
    <property name="transaction.factory_class">org.hibernate.transaction.JDBCTransactionFactory</property>
    <property name="current_session_context_class">thread</property>-->

    <property name="hibernate.show_sql">false</property>

    <mapping resource="user.hbm.xml"></mapping>
</session-factory>

用户文件定义如下:

<?xml version="1.0" encoding="utf-8"?>
<!DOCTYPE hibernate-mapping PUBLIC
        "-//Hibernate/Hibernate Mapping DTD//EN"
        "http://www.hibernate.org/dtd/hibernate-mapping-3.0.dtd">

<hibernate-mapping>
    <class name="com.pidac.myapp.testrestapi.User" table="Users">
        <meta attribute="class-description">
            This class contains the employee detail.
        </meta>
        <id name="id" type="int" column="id">
            <generator class="increment"/>
        </id>
        <property name="firstName" column="firstname" type="string"/>
        <property name="lastName" column="lastname" type="string"/>           
    </class>
</hibernate-mapping>

两个文件都保存在src / main / resources下。

我的HibernateUtils非常标准:

public class HibernateUtils {
    private static SessionFactory sessionFactory;

    public static SessionFactory getSessionFactory() {

        if (sessionFactory == null) {
            // loads configuration and mappings
            Configuration configuration = new Configuration().configure();
            ServiceRegistry serviceRegistry
                    = new StandardServiceRegistryBuilder()
                    .applySettings(configuration.getProperties()).build();

            // builds a session factory from the service registry
            sessionFactory = configuration.buildSessionFactory(serviceRegistry);
        }

        return sessionFactory;
    }

    public static Session getSession() throws HibernateException {
        return getSessionFactory().openSession();
    }
}

我有一个样本泽西资源如下:

@Path("/user")
public class UserResource {
    @GET
    @Produces(MediaType.APPLICATION_JSON)
    public User[] getAll(){

        Session session = HibernateUtils.getSession();
        session.beginTransaction();

        Query query=  session.createQuery("from User where  id > :id");

        query.setParameter("id", 0);

        Object[] users = query.list().toArray();

        User[] toReturn = new User[users.length];
        for (int i = 0; i < users.length; i++){
            toReturn[i] = (User)users[i];
        }

        session.close();
        return toReturn;
    }

    @POST
    @Produces(MediaType.APPLICATION_JSON)
    public User[] createUsers(User[] users){

        Session session = HibernateUtils.getSession();
        session.beginTransaction();

        for (User user : users){
            System.out.println("Adding user to session " + user.toString());
            session.save(user);
        }

        session.getTransaction().commit();
        session.close();

      return users;
    }

用户类定义如下:

package com.pidac.myapp.testrestapi;

/**  * Created by Klaus on 31/08/2015.  */ public class User {
    private String _firstName;
    private String _lastName; 
    private int _id;

    public String getFirstName(){
        return _firstName;
    }
    public void setFirstName(String name){
        _firstName = name;
    }    

    public int getId() {
        return _id;
    }

    public void setId(int _id) {
        this._id = _id;
    }

    public String getLastName() {
        return _lastName;
    }

    public void setLastName(String _lastName) {
        this._lastName = _lastName;
    }

    @Override public String toString(){
        return String.format("%s Ss", _firstName, _age);
    } }

当我通过Maven在Tomcat中运行时,我收到错误

 User is not mapped [from User where id > :id]

我可能会错过什么?

1 个答案:

答案 0 :(得分:0)

这是一个非常奇怪的问题。我甚至删除了所有的hibernate映射文件,并使用IntelliJ Persistence Tools窗口创建文件,但遇到了同样的问题。

我最后只是在User类上使用hibernate注释,如下所示:

import org.hibernate.annotations.GenericGenerator;
import javax.persistence.Column;

@javax.persistence.Entity
@javax.persistence.Table( name = "users" )
public class User {

    @Column (name = "firstname")
    private String firstName;

    @Column (name = "lastname")
    private String lastName;

    public String getFirstName(){
        return firstName;
    }
    public void setFirstName(String firstName){ this.firstName = firstName;
    }

    @javax.persistence.Id
    @javax.persistence.GeneratedValue
    //@GenericGenerator(name="increment", strategy = "increment")
    private int id;

    public int getId() {
        return id;
    }

    public void setId(int id) {
        this.id = id;
    }

    public String getLastName() {
        return lastName;
    }

    public void setLastName(String lastName) {
        this.lastName = lastName;
    }

    @Override public String toString(){
        return String.format("%s %s", firstName, lastName);
    }
}

并稍微修改了我的HibernateUtils以识别带注释的类,如下所示:

public class HibernateUtils {
    private static SessionFactory sessionFactory;

    public static SessionFactory getSessionFactory() {

        if (sessionFactory == null) {

            // loads configuration and mappings
            Configuration configuration = new Configuration().configure().addAnnotatedClass(User.class);
            ServiceRegistry serviceRegistry
                    = new StandardServiceRegistryBuilder()
                    .applySettings(configuration.getProperties()).build();

            // builds a session factory from the service registry
            sessionFactory = configuration.buildSessionFactory(serviceRegistry);
        }

        return sessionFactory;
    }

    public static Session getSession() throws HibernateException {
        return getSessionFactory().openSession();
    }
}

通过Tomcat Maven插件或IntelliJ的应用程序服务器工具窗口部署时,服务运行正常。