0

Here is the exception i am facing, it occurs when i call session.save

object.org.hibernate.MappingException: Unknown entity:
  com.java.learn.pojo.Employee

here is my java code

try
{
    Configuration configuration = new Configuration().configure();
    StandardServiceRegistryBuilder builder =
        new StandardServiceRegistryBuilder().applySettings(configuration.getProperties());
    SessionFactory factory = configuration.buildSessionFactory(builder.build());
    Session session = factory.openSession();
    Employee e = new Employee(1, "", "", 3);
    e.setFirstName("sparsh");
    session.save(e);
    System.out.println("in dao");
}
catch (Throwable ex)
{
    System.err.println("Failed to create sessionFactory object." + ex);
    factory.close();
    throw new ExceptionInInitializerError(ex);
}

and here is my hibrnate.cfg.xml

<hibernate-configuration>
    <session-factory>
        <property name="hibernate.dialect">
            org.hibernate.dialect.MySQLDialect
        </property>
        <property name="hibernate.connection.driver_class">
            com.mysql.jdbc.Driver
        </property>

        <!-- Assume students is the database name -->
        <property name="hibernate.connection.url">
            jdbc:mysql://localhost/test
        </property>
        <property name="hibernate.connection.username">
            root
        </property>
        <property name="hibernate.connection.password">
            root1
        </property>
        <property name="hbm2ddl.auto">create</property>
        <property name="show_sql">true</property>
        <mapping class="com.java.learn.pojo.Employee"/>
    </session-factory>
</hibernate-configuration>

this exception occurs because of no mapping for Pojo class, but I have added it.

Pojo class

package com.java.learn.pojo;

import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import javax.persistence.Table;

@Table
@Entity
public class Employee
{
    @Id
    @GeneratedValue
    @Column(name = "id")
    private int id;

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

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

    @Column(name = "salary")
    private int salary;

    public Employee()
    {
    }

    public Employee(int id, String firstName, String lastName, int salary)
    {
        super();
        this.id = id;
        this.firstName = firstName;
        this.lastName = lastName;
        this.salary = salary;
    }

    public int getId()
    {
        return id;
    }

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

    public String getFirstName()
    {
        return firstName;
    }

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

    public String getLastName()
    {
        return lastName;
    }

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

    public int getSalary()
    {
        return salary;
    }

    public void setSalary(int salary)
    {
        this.salary = salary;
    }
}

Please help

4
  • where have you placed your hibrnate.cfg.xml? Commented Apr 17, 2017 at 4:18
  • in web-inf/classes Commented Apr 17, 2017 at 4:19
  • I think this link will help you. Commented Apr 17, 2017 at 4:19
  • @sparsh610 you should place it in your resources folder. Or if it's any other folder, then you should give that path in configuration. Commented Apr 17, 2017 at 4:22

2 Answers 2

0

You can use new Configuration().configure() to load the Entity mappings from hbm.xml files.

But because you are using annotations for your Employee Entity class, you simply need to use AnnotationConfiguration instead of Configuration as shown below:

So, replace the line below:

Configuration configuration = new Configuration().configure();

with:

 Configuration configuration = new AnnotationConfiguration().configure();
Sign up to request clarification or add additional context in comments.

3 Comments

but annoationconfiguration get deprectaed ... also u trying to inject sessionfactory in configuration instance
Also, I suggest first try with AnnotationConfiguration and check if it works and then can drill down what could be the issue...
0

Problem:

The problem is obvious here, you have not correctly mapped your Employee entity which is so relevant from the error stack trace:

object.org.hibernate.MappingException: Unknown entity: com.java.learn.pojo.Employee

And the problem is that the mapped entity in your hibernate.cfg.xml configuration file can't be reached because you placed your file under web-inf/classes so when hibernate tries to look for your Entity it will look for it under web-inf/classes and it won't find it.

Solution:

To solve this problem you just need to place your hibernate.cfg.xml file under Java/src folder instead of web-inf/classes so the Entity can be reached.

Comments

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.