0

I am sorry if this is a duplicate of any existing question. Following is my employee bean.

public class Employee {
       private int id;
       private String firstName; 
       private String lastName;   
       private int salary;  

       public Employee() {}
       public Employee(String fname, String lname, int salary) {
          this.firstName = fname;
          this.lastName = lname;
          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;
       }
    }

I want to map it to my postgres table employee

CREATE TABLE employee
(
  id integer NOT NULL DEFAULT nextval('emp_id_seq'::regclass),
  first_name character varying(20),
  last_name character varying(20),
  salary integer,
  CONSTRAINT employee_pkey PRIMARY KEY (id)
)

Here is my main Class code snippet

  try{
     factory = new Configuration().configure().buildSessionFactory();
  }catch (Throwable ex) { 
     System.err.println("Failed to create sessionFactory object." + ex);
     throw new ExceptionInInitializerError(ex); 
  }

while running it i get the following error

Failed to create sessionFactory object.org.hibernate.InvalidMappingException: Could not parse mapping document from resource Employee.hbm.xml
Exception in thread "main" java.lang.ExceptionInInitializerError
    at ManageEmployee.main(ManageEmployee.java:26)
Caused by: org.hibernate.InvalidMappingException: Could not parse mapping document from resource Employee.hbm.xml
    at org.hibernate.cfg.Configuration.addResource(Configuration.java:575)
    at org.hibernate.cfg.Configuration.parseMappingElement(Configuration.java:1593)
    at org.hibernate.cfg.Configuration.parseSessionFactory(Configuration.java:1561)
    at org.hibernate.cfg.Configuration.doConfigure(Configuration.java:1540)
    at org.hibernate.cfg.Configuration.doConfigure(Configuration.java:1514)
    at org.hibernate.cfg.Configuration.configure(Configuration.java:1434)
    at org.hibernate.cfg.Configuration.configure(Configuration.java:1420)
    at ManageEmployee.main(ManageEmployee.java:23)

Here is my mapping file

<?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 package = "com.demo.hibernate.beans">
   <class name="Employee" table="employee">
      <meta attribute="class-description">
         This class contains the employee detail. 
      </meta>
      <id name="id" type="int" column="id">
         <generator class="native"/>
      </id>
      <property name="firstName" column="first_name" type="string"/>
      <property name="lastName" column="last_name" type="string"/>
      <property name="salary" column="salary" type="int"/>
   </class>
</hibernate-mapping>

What could be the possible reason i know this has something to do with Employee.hbm.xml. Please help

Thanks in advance !!!

Here is CFG File if it may help

<?xml version="1.0" encoding="utf-8"?>
<!DOCTYPE hibernate-configuration PUBLIC "-//Hibernate/Hibernate Configuration DTD 3.0//EN" "http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd"> 
<hibernate-configuration>
   <session-factory>
   <property name="hibernate.dialect">
      org.hibernate.dialect.PostgreSQLDialect
   </property>
   <property name="hibernate.connection.driver_class">
      org.postgresql.Driver
   </property>

   <!-- Assume test is the database name -->
   <property name="hibernate.connection.url">
      jdbc:postgresql://localhost:5432/postgres
   </property>
   <property name="hibernate.connection.username">
      postgres
   </property>
   <property name="hibernate.connection.password">
      postgres
   </property>

   <!-- List of XML mapping files -->
   <mapping resource="Employee.hbm.xml"/>

</session-factory>
</hibernate-configuration>
8
  • @Do you have a hibernate.cfg.xml? Commented Aug 9, 2013 at 10:00
  • Is your Employee class under any package? Commented Aug 9, 2013 at 10:03
  • HBM CFG and classes are all under src folder Commented Aug 9, 2013 at 10:09
  • Why not retry putting it in a package? Commented Aug 9, 2013 at 10:13
  • As JtheRocker said try putting it in a package, and specify your package-name like this: <hibernate-mapping package="your.package.name"> As it is under default package now, you might know classes in a default package are not accessible form outside. So try putting it that way. Commented Aug 9, 2013 at 10:29

1 Answer 1

1

Error means Hibernate was unable to load mapping file (check code, is a wrap for a FileNotFoundException) so your error is in hibernate.cfg.xml file,not in mapping!
Check if <mapping file="path/to/Employee.hbm.xml" /> is correct

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

2 Comments

HBM CFG and classes are all under src folder. So i guess it will not be needed
try /Employee.hbm.xml

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.