1

I write a java maven project for restful webservice using jersey + hibernate and having below error:

javax.servlet.ServletException: org.hibernate.MappingException: Unknown entity: 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 File:

 <?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>

The table name is "logindetail".

Database Class:

 @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;

    }}

Main Class:

 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;

        }}

now this error is coming java.lang.NullPointerException anyone can help me to eliminate this error i will b thankful :)

10
  • Please don't use lowercase names for class names. But that's not the problem. I do not see a package directive above your class code. But you have used a package 'org.asad.dto.' in the hibernate configuration. That might be the problem. If it is then, put the class in the resp. package. Commented Jul 4, 2015 at 10:36
  • Check the package of logindetail is the same like provided in <mapping class="..." />. Commented Jul 4, 2015 at 10:36
  • @PeterPaulKiefer ok i will take care about class names in future . The package is put in the class but still the error. Commented Jul 4, 2015 at 10:42
  • @barthel yes the package is same as the provided in the mapping class. Commented Jul 4, 2015 at 10:43
  • Tell us about the action that throws this exception and provide the full stacktrace and Hibernate version. Also switch loglevel of hibernate to debug/trace will more explain the error. Commented Jul 4, 2015 at 10:57

1 Answer 1

1

Can you check if you imported correct @Entity annotation.

As @Entity comes under two packages one is org.hibernate.annotations.Entity and other one with javax.persistence.Entity.

Use javax.persistence.Entity to annotate your entity beans. Don't import org.hibernate.annotations.Entity.

Sign up to request clarification or add additional context in comments.

5 Comments

it works but data is not comming from database showinng 204 no content
Post your main class content, then only one can suggest something.
@Asad I uv this answer because it might help other developers which have a similiar problem. And it shows you and me ;-) how important it is to provide a complete class file - with package and imports. Lucky you, that Arpit has so much experience to gues the answer correctly.
@Arpit i add the class see and suggest me
@Asad the logindetail entity class instead. And please check the format and layout before save the question.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.