facebook

Cannot get hibernate to work(HibernateSessionFactory problem

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

    mm2ha
    Member

    Hi, I have been following this tutorial for hibernate:
    http://www.laliluna.de/first-hibernate-example-tutorial.html , I did also the one on this website(that worked kind of ok, but not too good either). My problem is that when I try to run it with this code:

    
    public static void main(String[] args) {
            
    Line 25        Integer primaryKey = createHoney();
            System.out.println("primary key is " + primaryKey);
            updateHoney(primaryKey);
            listHoney();
        }
    

    where honey is my database with fields id, name, taste, I get this error:

    
    java.lang.NoClassDefFoundError: org/dom4j/Attribute
        at hibernate.HibernateSessionFactory.<clinit>(HibernateSessionFactory.java:30)
        at hibernate.TestClient.createHoney(TestClient.java:43)
        at hibernate.TestClient.main(TestClient.java:25)
    Exception in thread "main" 
    

    code from TestClient.createHoney:

    
    private static Integer createHoney() {
            Session session = null;
            Transaction tx = null;
            Logger log = Logger.getLogger("TestClient");
    
            log.info("creating honey");
    
            // [laliluna] our returned primary key
            Integer id = null;
    
            try {
                // [laliluna] get the session from the factory
    line 43:        session = HibernateSessionFactory.currentSession();
    
                // [laliluna] always start a transaction before doing something
                // (even reading) from the database
                tx = session.beginTransaction();
    
                // [laliluna] create a new object
                Honey honey = new Honey();
                honey.setName("Sebastian's favourite honey");
                honey.setTaste("sweet");
    
                // [laliluna] save it to the database, Hibernate returns your object
                // with the primary key field updated!
                id = (Integer) session.save(honey);
    
                // [laliluna] commit your transaction or nothing is wrote to the db
                tx.commit();
    
                // [laliluna] clean up (close the session)
                session.close();
    

    and from HibernateSessionFactory:

        private static String CONFIG_FILE_LOCATION = "/hibernate.cfg.xml";
    
        /** Holds a single instance of Session */
        private static final ThreadLocal threadLocal = new ThreadLocal();
    
        /** The single instance of hibernate configuration */
        private static Configuration cfg = new Configuration();
    
        /** The single instance of hibernate SessionFactory */
        private static net.sf.hibernate.SessionFactory sessionFactory;
    
        /**
         * Returns the ThreadLocal Session instance.  Lazy initialize
         * the <code>SessionFactory</code> if needed.
         *
         *  @return Session
         *  @throws HibernateException
         */
        public static Session currentSession() throws HibernateException {
            Session session = (Session) threadLocal.get();
    
            if (session == null || ! session.isConnected()) {
                if (sessionFactory == null) {
                    try {
                        cfg.configure(CONFIG_FILE_LOCATION);
                        sessionFactory = cfg.buildSessionFactory();
                    }
                    catch (Exception e) {
                        System.err.println("%%%% Error Creating SessionFactory %%%%");
                        e.printStackTrace();
                    }
                }
                session = sessionFactory.openSession();
                threadLocal.set(session);
            }
    
            return session;
        }
    
        /**
         *  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();
            }
        }
    
        /**
         * Default constructor.
         */
        private HibernateSessionFactory() {
        }
    
    }

    I created the hibernate thorough your tutorial(MyEclipse->add hib) and also did the hibernate mapping to the honey table. But i really dont understand what that no class def could be. Please help me, if you know what can be the problem. thanks a lot

    #233732 Reply

    Riyad Kalla
    Member

    Is your dom4j JAR file in your WEB-INF/lib directory?

    #233739 Reply

    mm2ha
    Member

    I am creating just a java application, so I did not have the web-in folder. but yes, i had the dom4j file in the /lib directory with all the other files needed for hibernate. then I have mooved all of them just to the / , but that did not work either.

    #233740 Reply

    mm2ha
    Member

    by the way, it is dom4j-1.6.jar file

    #233754 Reply

    mm2ha
    Member

    when I add another dom4j file, this time the dom4j.jar from the jboss\lib directory, I get this error:

    Exception in thread "main" java.lang.NoClassDefFoundError: org/apache/commons/logging/LogFactory
        at net.sf.hibernate.cfg.Configuration.<clinit>(Configuration.java:95)
        at hibernate.HibernateSessionFactory.<clinit>(HibernateSessionFactory.java:30)
        at hibernate.TestClient.createHoney(TestClient.java:44)
        at hibernate.TestClient.main(TestClient.java:26)

    what is wrong?

    #233761 Reply

    Riyad Kalla
    Member

    You now need to add commons-logging.jar to your project.

    #233792 Reply

    mm2ha
    Member

    thanks a lot, I have figured it out. the problem was, that I added the hibernate suppert thorough myeclipse->add hibernate . however, it added all the jars only as text files, so then I had to add all of them into the build path. After that i had to figure out and fix few other things, but now it is working! thanks for help!!

Viewing 7 posts - 1 through 7 (of 7 total)
Reply To: Cannot get hibernate to work(HibernateSessionFactory problem

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