Domanda

I am new to JPA, I was trying to setup a project in JPA using Hibernate implementation. I studied that we can either write code using annotations or using *.hbm.xml. I was trying to implement it with annotations but while I try to insert values to my db it throws an error as following.

resource: centre/Centre.hbm.xml not found

my Centre.java class is as following

  /**
     * 
     */
    package centre;

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

    /**
     * @author anoop
     *
     */

    @Entity
    @Table(name="centre")
    public class Centre {

        private int id;
        private String name;
        private String address;
        private String comments;


        /**
         * @return the id
         */
        @Id
        public int getId() {
            return id;
        }
        /**
         * @param id the id to set
         */
        public void setId(int id) {
            this.id = id;
        }
        /**
         * @return the name
         */
        public String getName() {
            return name;
        }
        /**
         * @param name the name to set
         */
        public void setName(String name) {
            this.name = name;
        }
        /**
         * @return the address
         */
        public String getAddress() {
            return address;
        }
        /**
         * @param address the address to set
         */
        public void setAddress(String address) {
            this.address = address;
        }
        /**
         * @return the comments
         */
        public String getComments() {
            return comments;
        }
        /**
         * @param comments the comments to set
         */
        public void setComments(String comments) {
            this.comments = comments;
        }

    }

persistence.xml

<?xml version="1.0" encoding="UTF-8"?>
<persistence version="2.1" xmlns="http://xmlns.jcp.org/xml/ns/persistence" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/persistence http://xmlns.jcp.org/xml/ns/persistence/persistence_2_1.xsd">
    <persistence-unit name="gurukul">
        <class>centre.Centre</class>
        <exclude-unlisted-classes>true</exclude-unlisted-classes>
    </persistence-unit>
</persistence>

hibernate-cfg.xml

<?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.connection.driver_class">org.postgresql.Driver</property>
        <property name="hibernate.connection.password">password</property>
        <property name="hibernate.connection.url">jdbc:postgresql://127.0.0.1/DBNAME</property>
            <property name="hibernate.connection.username">username</property>
<property name="hibernate.dialect">org.hibernate.dialect.PostgreSQLDialect</property>
<property name="connection.pool_size">1</property>
<property name="show_sql">true</property>
<property name="hbm2ddl.auto">create</property>
    </session-factory>
</hibernate-configuration>

HibernateUtil.java

    private static final SessionFactory sessionFactory;

    static {
        try {
            sessionFactory = new AnnotationConfiguration().configure().addClass(Centre.class).buildSessionFactory();

            } catch (Throwable ex) {
            System.err.println("Initial SessionFactory creation failed." + ex);
            throw new ExceptionInInitializerError(ex);
        }
    }

    public static SessionFactory getSessionFactory() {
        return sessionFactory;
    }

/**
 * @return the em
 */
public static EntityManager getEm() {
    return em;
}

GurukulDAO.java

  /**
     * 
     */
    package dao;

    import javax.persistence.EntityManager;
    import javax.persistence.EntityTransaction;

    import org.hibernate.Session;
    import org.hibernate.Transaction;

    import util.HibernateUtil;
    import centre.Centre;

    /**
     * @author anoop
     *
     */
    public class GurukulDAO {

        public void saveCentre(String name, String address, String comments){
            Session session = HibernateUtil.getSessionFactory().openSession();
            Transaction txn = session.beginTransaction();

            Centre centre = new Centre();
            centre.setName(name);
            centre.setAddress(address);
            centre.setComments(comments);

            session.save(centre);
            txn.commit();

            txn.begin();
        }


    }

Nainclass.java

 package main;

    import dao.GurukulDAO;

    public class MainClass {

        public static void main(String s[]){
            GurukulDAO gurukulDAO = new GurukulDAO();
            gurukulDAO.saveCentre("BajajNagar", "Flat no. 5", "Head Office");
        }

    }

while I execute my MainClass I get the error

 1 [main] INFO org.hibernate.cfg.annotations.Version - Hibernate Annotations 3.4.0.GA
    13 May, 2014 2:21:21 PM org.hibernate.cfg.Environment <clinit>
    INFO: Hibernate 3.2 cr4
    13 May, 2014 2:21:21 PM org.hibernate.cfg.Environment <clinit>
    INFO: hibernate.properties not found
    13 May, 2014 2:21:21 PM org.hibernate.cfg.Environment buildBytecodeProvider
    INFO: Bytecode provider name : cglib
    13 May, 2014 2:21:21 PM org.hibernate.cfg.Environment <clinit>
    INFO: using JDK 1.4 java.sql.Timestamp handling
    13 May, 2014 2:21:21 PM org.hibernate.cfg.Configuration configure
    INFO: configuring from resource: /hibernate.cfg.xml
    13 May, 2014 2:21:21 PM org.hibernate.cfg.Configuration getConfigurationInputStream
    INFO: Configuration resource: /hibernate.cfg.xml
    13 May, 2014 2:21:21 PM org.hibernate.cfg.Configuration doConfigure
    INFO: Configured SessionFactory: null
    13 May, 2014 2:21:21 PM org.hibernate.cfg.Configuration addClass
    INFO: Reading mappings from resource: centre/Centre.hbm.xml
    Initial SessionFactory creation failed.org.hibernate.MappingNotFoundException: resource: centre/Centre.hbm.xml not found
    Exception in thread "main" java.lang.ExceptionInInitializerError
        at util.HibernateUtil.<clinit>(HibernateUtil.java:32)
        at dao.GurukulDAO.saveCentre(GurukulDAO.java:22)
        at main.MainClass.main(MainClass.java:9)
    Caused by: org.hibernate.MappingNotFoundException: resource: centre/Centre.hbm.xml not found
        at org.hibernate.cfg.Configuration.addClass(Configuration.java:538)
        at org.hibernate.cfg.AnnotationConfiguration.addClass(AnnotationConfiguration.java:963)
        at util.HibernateUtil.<clinit>(HibernateUtil.java:25)
        ... 2 more

please guide me what wrong I am doing in my code.

È stato utile?

Soluzione 2

Removed addClass(Centre.class) from my HibernateUtil.java and it worked for me.

Altri suggerimenti

Remove the <class> entry from persistence.xml file and as reported by the log you need to specify a Mapping file descriptor for Hibernate and this can be done by adding the <mapping> to hibernate-cfg.xml so your file will look like:

<?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.connection.driver_class">org.postgresql.Driver</property>
    <property name="hibernate.connection.password">password</property>
    <property name="hibernate.connection.url">jdbc:postgresql://127.0.0.1/DBNAME</property>
    <property name="hibernate.connection.username">username</property>
    <property name="hibernate.dialect">org.hibernate.dialect.PostgreSQLDialect</property>
    <property name="connection.pool_size">1</property>
    <property name="show_sql">true</property>
    <property name="hbm2ddl.auto">create</property>
    <mapping class="centre.Centre" /> <!-- You can also specify a whole package with <mapping package="centre" /> -->
  </session-factory>
</hibernate-configuration>
Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top