facebook

Unable to test if my hibernate is working or not??

  1. MyEclipse Archived
  2.  > 
  3. Database Tools (DB Explorer, Hibernate, etc.)
Viewing 3 posts - 1 through 3 (of 3 total)
  • Author
    Posts
  • #284370 Reply

    darkapple
    Member

    As i’m beginner to hibernate, i followed a tutorial and went through the hibernate myeclipse tutorial.

    I’ve created a web project and for hibernate configuration testing i’ve created a java class but i’m getting this error contineously. Here is error message and log infos copied from console.

    0    [main] INFO  org.hibernate.cfg.Environment  - Hibernate 3.1.3
    0    [main] INFO  org.hibernate.cfg.Environment  - hibernate.properties not found
    0    [main] INFO  org.hibernate.cfg.Environment  - using CGLIB reflection optimizer
    0    [main] INFO  org.hibernate.cfg.Environment  - using JDK 1.4 java.sql.Timestamp handling
    93   [main] INFO  org.hibernate.cfg.Configuration  - configuring from resource: /hibernate.cfg.xml
    93   [main] INFO  org.hibernate.cfg.Configuration  - Configuration resource: /hibernate.cfg.xml
    390  [main] INFO  org.hibernate.cfg.Configuration  - Reading mappings from resource: com/library/Book.hbm.xml
    515  [main] INFO  org.hibernate.cfg.HbmBinder  - Mapping class: com.library.Book -> book
    531  [main] INFO  org.hibernate.cfg.Configuration  - Reading mappings from resource: com/library/Customer.hbm.xml
    578  [main] INFO  org.hibernate.cfg.HbmBinder  - Mapping class: com.library.Customer -> customer
    578  [main] INFO  org.hibernate.cfg.Configuration  - Configured SessionFactory: null
    %%%% Error Creating SessionFactory %%%%
    org.hibernate.MappingException: Association references unmapped class: Book
        at org.hibernate.cfg.HbmBinder.bindCollectionSecondPass(HbmBinder.java:2344)
        at org.hibernate.cfg.HbmBinder$CollectionSecondPass.secondPass(HbmBinder.java:2618)
        at org.hibernate.cfg.CollectionSecondPass.doSecondPass(CollectionSecondPass.java:35)
        at org.hibernate.cfg.Configuration.secondPassCompile(Configuration.java:1012)
        at org.hibernate.cfg.Configuration.buildSessionFactory(Configuration.java:1168)
        at com.library.HibernateSessionFactory.<clinit>(HibernateSessionFactory.java:34)
        at com.library.test.LibraryTest.setUp(LibraryTest.java:70)
        at com.library.test.LibraryTest.main(LibraryTest.java:27)
    593  [main] INFO  org.hibernate.cfg.Configuration  - configuring from resource: /hibernate.cfg.xml
    593  [main] INFO  org.hibernate.cfg.Configuration  - Configuration resource: /hibernate.cfg.xml
    593  [main] INFO  org.hibernate.cfg.Configuration  - Reading mappings from resource: com/library/Book.hbm.xml
    640  [main] INFO  org.hibernate.cfg.Mappings  - duplicate import: com.library.Book->com.library.Book
    640  [main] INFO  org.hibernate.cfg.Mappings  - duplicate import: com.library.Book->Book
    640  [main] INFO  org.hibernate.cfg.HbmBinder  - Mapping class: com.library.Book -> book
    %%%% Error Creating SessionFactory %%%%
    org.hibernate.MappingException: Could not read mappings from resource: com/library/Book.hbm.xml
        at org.hibernate.cfg.Configuration.addResource(Configuration.java:485)
        at org.hibernate.cfg.Configuration.parseMappingElement(Configuration.java:1465)
        at org.hibernate.cfg.Configuration.parseSessionFactory(Configuration.java:1433)
        at org.hibernate.cfg.Configuration.doConfigure(Configuration.java:1414)
        at org.hibernate.cfg.Configuration.doConfigure(Configuration.java:1390)
        at org.hibernate.cfg.Configuration.configure(Configuration.java:1310)
        at com.library.HibernateSessionFactory.rebuildSessionFactory(HibernateSessionFactory.java:86)
        at com.library.HibernateSessionFactory.getSession(HibernateSessionFactory.java:56)
        at com.library.test.LibraryTest.setUp(LibraryTest.java:70)
        at com.library.test.LibraryTest.main(LibraryTest.java:27)
    Caused by: org.hibernate.DuplicateMappingException: Duplicate class/entity mapping com.library.Book
        at org.hibernate.cfg.Mappings.addClass(Mappings.java:118)
        at org.hibernate.cfg.HbmBinder.bindRoot(HbmBinder.java:154)
        at org.hibernate.cfg.Configuration.add(Configuration.java:386)
        at org.hibernate.cfg.Configuration.addInputStream(Configuration.java:427)
        at org.hibernate.cfg.Configuration.addResource(Configuration.java:482)
        ... 9 more
    Exception in thread "main" java.lang.NullPointerException
        at com.library.test.LibraryTest.testAndCreateDomains(LibraryTest.java:40)
        at com.library.test.LibraryTest.main(LibraryTest.java:35)

    Here is my HibernateSessionFactory class:

    package com.library;
    
    
    import java.io.File;
    
    import org.hibernate.HibernateException;
    import org.hibernate.Session;
    import org.hibernate.cfg.Configuration;
    
    /**
     * Configures and provides access to Hibernate sessions, tied to the
     * current thread of execution.  Follows the Thread Local Session
     * pattern, see {@link http://hibernate.org/42.html }.
     */
    public class HibernateSessionFactory {
    
        /**
         * Location of hibernate.cfg.xml file.
         * Location should be on the classpath as Hibernate uses 
         * #resourceAsStream style lookup for its configuration file.
         * The default classpath location of the hibernate config file is
         * in the default package. Use #setConfigFile() to update
         * the location of the configuration file for the current session.   
         */
        private static String CONFIG_FILE_LOCATION = "/hibernate.cfg.xml";
        private static final ThreadLocal threadLocal = new ThreadLocal();
        private static Configuration configuration = new Configuration();
        private static org.hibernate.SessionFactory sessionFactory;
        private static String configFile = CONFIG_FILE_LOCATION;
    
        static {
           try {
             configuration.configure(configFile);
             sessionFactory = configuration.buildSessionFactory();
          } catch (Exception e) {
             System.err
                   .println("%%%% Error Creating SessionFactory %%%%");
             e.printStackTrace();
          }
        }
        private HibernateSessionFactory() {
        }
       
       /**
         * Returns the ThreadLocal Session instance.  Lazy initialize
         * the <code>SessionFactory</code> if needed.
         *
         *  @return Session
         *  @throws HibernateException
         */
        public static Session getSession() throws HibernateException {
            Session session = (Session) threadLocal.get();
    
          if (session == null || !session.isOpen()) {
             if (sessionFactory == null) {
                rebuildSessionFactory();
             }
             session = (sessionFactory != null) ? sessionFactory.openSession()
                   : null;
             threadLocal.set(session);
          }
    
            return session;
        }
    
       /**
         *  Rebuild hibernate session factory
         *
         */
       /*public synchronized static void rebuildSessionFactory() {
          try {
             configuration.configure(configFile);
             sessionFactory = configuration.buildSessionFactory();
          } catch (Exception e) {
             System.err
                   .println("%%%% Error Creating SessionFactory %%%%");
             e.printStackTrace();
          }
       }*/
        
        public synchronized static void rebuildSessionFactory() {
            if (sessionFactory != null)
                return;
            
            try {
                configuration.configure(configFile);
                sessionFactory = configuration.buildSessionFactory();
                } 
            catch (Exception e) {
                System.err
            .println("%%%% Error Creating SessionFactory %%%%");
                e.printStackTrace();
            }
        }
    
       /**
         *  Close the single hibernate session instance.
         *
         *  @throws HibernateException
         */
        public static void closeSession() throws HibernateException {
            Session session = (Session) threadLocal.get();
            threadLocal.set(null);
    
            if (session != null) {
                session.close();
            }
        }
    
       /**
         *  return session factory
         *
         */
       public static org.hibernate.SessionFactory getSessionFactory() {
          return sessionFactory;
       }
    
       /**
         *  return session factory
         *
         *   session factory will be rebuilded in the next call
         */
       public static void setConfigFile(String configFile) {
          HibernateSessionFactory.configFile = configFile;
          sessionFactory = null;
       }
    
       /**
         *  return hibernate configuration
         *
         */
       public static Configuration getConfiguration() {
          return configuration;
       }
       
    }

    Also ive gone through the similar problem on http://www.myeclipseide.com/PNphpBB2-printview-t-15149-start-0.html

    any help will be highly appreciated

    MyEclipse v 6.0
    Hibernate v 3.1.3

    #284372 Reply

    darkapple
    Member

    after few hours of search i came to the mistake i’ve made. It’s not the HibernateSessionFactory causing the error, its the hbm.cfg configuration file for a table causing trouble.

    I forgot to give full class name with package in one-to-many tag of set Here is the corrected one.

    <?xml version="1.0" encoding="utf-8"?>
    <!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
    "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
    <!-- 
        Mapping file autogenerated by MyEclipse Persistence Tools
    -->
    <hibernate-mapping>
        <class name="com.library.Customer" table="customer" catalog="library">
            <id name="id" type="java.lang.Integer">
                <column name="id" />
                <generator class="increment" />
            </id>
             <set name="books" inverse="false">
                <key><column name="customer_fk"></column></key>
                <one-to-many class="com.library.Book"/>
                           ^----------------------------^-
            </set>
            
            <property name="firstname" type="java.lang.String">
                <column name="firstname" />
            </property>
            <property name="lastname" type="java.lang.String">
                <column name="lastname" />
            </property>
            <property name="age" type="java.lang.Integer">
                <column name="age" />
            </property>
        </class>
    </hibernate-mapping>
    #284381 Reply

    Loyal Water
    Member

    Glad you got this sorted out.

Viewing 3 posts - 1 through 3 (of 3 total)
Reply To: Unable to test if my hibernate is working or not??

You must be logged in to post in the forum log in