Hibernate + PostgreSQL抛出异常:未知实体

时间:2015-07-04 10:26:40

标签: java hibernate web-services postgresql maven

我使用webservice并在jersey + hibernate下面为restful error编写了一个java maven项目:

  

javax.servlet.ServletException:org.hibernate.MappingException:   未知实体:org.asad.dto.logindetail

     

org.glassfish.jersey.servlet.WebComponent.service(WebComponent.java:419)

     

org.glassfish.jersey.servlet.ServletContainer.service(ServletContainer.java:       381)       org.glassfish.jersey.servlet.ServletContainer.service(ServletContainer.java:344)

     

org.glassfish.jersey.servlet.ServletContainer.service(ServletContainer.java:       221)

hibernate.cfg.xml文件:

 <?xml version='1.0' encoding='utf-8'?>

<!DOCTYPE hibernate-configuration PUBLIC

        "-//Hibernate/Hibernate Configuration DTD 3.0//EN"

        "http://www.hibernate.org/dtd/hibernate-configuration-3.0.dtd">


<hibernate-configuration>


    <session-factory>


        <!-- Database connection settings -->

        <property name="connection.driver_class">org.postgresql.Drive</property>

        <property 
    name="connection.url">jdbc:postgresql://localhost:5432/logindb</property>

    <property name="connection.username">postgres</property>

        <property name="connection.password">project</property>

        <!-- JDBC connection pool (use the built-in) -->

        <property name="connection.pool_size">1</property>


        <!-- SQL dialect -->

        <property 
    name="dialect">org.hibernate.dialect.PostgreSQLDialect</property>

        <!-- Disable the second-level cache  -->

    <property name="cache.provider_class">org.hibernate.cache.NoCacheProvider</property>


        <!-- Echo all executed SQL to stdout -->

        <property name="show_sql">true</property>

        <!-- Drop and re-create the database schema on startup -->

        <property name="hbm2ddl.auto">update</property>

        <propertyname="connection.driver_class">org.postgresql.Driver</property>

        <!-- Names the annotated entity class -->

        <mapping class="org.asad.dto.logindetail"/>

    </session-factory>

</hibernate-configuration>

表名是&#34; logindetail&#34;。

数据库类:

 @Entity
     @Table (name = "logidetail")  public class logindetail {

    @Id

    int userId;

    String name;

    String password;


    public void setUserId(int i){

        this.userId = i;

    }

    public int getUserId(){

        return userId;

    }

    public void setName(String name){

        this.name  = name;

    }

    public String getName(){

        return name;

    }

    public void setPassword(String pass){

        this.password = pass;

    }

    public String getPassword(){

        return password;

    }}

主要类别:

 package org.asad.login.login.loginservice; 

import java.util.ArrayList;

import javax.management.Query;

import javax.validation.Validation;

import javax.validation.Validator;

import javax.validation.ValidatorFactory;

import org.asad.dto.logindetail;

import org.hibernate.SessionFactory;

import org.hibernate.cfg.Configurationimport org.hibernate.classic.Session;

public class LoginService{

    SessionFactory sessionFactory = null;

    Session session = null;

    public String getDatabaseUser(){

        logindetail user = null;

        String name=null;try{

        sessionFactory = new Configuration().configure().buildSessionFactory();

        session = sessionFactory.openSession();

        session.beginTransaction();

        user = (logindetail)session.get(logindetail.class, 2);

        name = user.getName();

        session.getTransaction().commit();

        session.close();

    }catch(Exception e){

       e.printStackTrace();

     }

    return name;

        }}

现在出现此错误java.lang.NullPointerException 任何人都可以帮助我消除这个错误我会感激:)

1 个答案:

答案 0 :(得分:1)

您可以检查是否导入了正确的@Entity注释。

由于@Entity属于两个包,其中一个为org.hibernate.annotations.Entity,另一个为javax.persistence.Entity

使用javax.persistence.Entity注释您的实体bean。不要导入org.hibernate.annotations.Entity