-->
These old forums are deprecated now and set to read-only. We are waiting for you on our new forums!
More modern, Discourse-based and with GitHub/Google/Twitter authentication built-in.

All times are UTC - 5 hours [ DST ]



Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 2 posts ] 
Author Message
 Post subject: How can we configure hibernate with eclipse 3.2 IDE
PostPosted: Wed Aug 01, 2007 12:51 am 
Newbie

Joined: Tue Jul 31, 2007 9:11 am
Posts: 2
Location: Delhi,India
How can we configure hibernate with eclipse 3.2 IDE


Top
 Profile  
 
 Post subject:
PostPosted: Wed Aug 01, 2007 6:16 am 
Expert
Expert

Joined: Fri Jul 13, 2007 8:18 am
Posts: 370
Location: london
Not entirely sure what you mean. I use a HibernateUtil class in my eclipse project. You can either get the util class to read a hibernate config file or configure it programatically:

Reading a config file thats located in the x.x.x.storage.hibernate package:
HibernateUtil.java
Code:
package com.company.project.storage.hibernate;

public class HibernateUtil {

   public static final Configuration cfg = new Configuration();
    private static final SessionFactory sessionFactory;

    static {
        try {
            // Create the SessionFactory from hibernate.cfg.xml
           cfg.configure("/com/company/project/storage/hibernate/hibernate.cfg.xml");
            sessionFactory = cfg.buildSessionFactory();
        } catch (Throwable ex) {
            // Make sure you log the exception, as it might be swallowed
            System.out.println("Initial SessionFactory creation failed." + ex);
            throw new ExceptionInInitializerError(ex);
        }
    }

    public static SessionFactory getSessionFactory() {
        return sessionFactory;
    }
}


Configuring the session factory programatically - no config file necessary. This is a very simplistic implementation that I use for testing. I have to manually manage my session open/close rather than having a currentSession bound to the thread. Its useful for testing because you can explicitly get a brand new session and so ensure level1 caches are empty.

HibernateUtil.java
Code:
package test.groupbydate;

import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.cfg.Configuration;


public class HibernateUtil {

    private static SessionFactory factory;

    static {
       Configuration config = new Configuration()
          .addClass(Item.class)
// HSQLDB Config
//          .setProperty("hibernate.dialect", "org.hibernate.dialect.HSQLDialect")
//         .setProperty("hibernate.connection.driver_class", "org.hsqldb.jdbcDriver")
//         .setProperty("hibernate.connection.url", "jdbc:hsqldb:mem:test")
//          .setProperty("hibernate.connection.username", "sa")
//          .setProperty("hibernate.connection.password", "")
//          .setProperty("hibernate.hbm2ddl.auto", "create-drop")
          
// MYSQL Config
          .setProperty("hibernate.dialect", "org.hibernate.dialect.MySQLDialect")
         .setProperty("hibernate.connection.driver_class", "com.mysql.jdbc.Driver")
         .setProperty("hibernate.connection.url", "jdbc:mysql://localhost/test")
         .setProperty("hibernate.connection.username", "root")
         .setProperty("hibernate.connection.password", "password")
         .setProperty("hibernate.hbm2ddl.auto", "create-drop")
                  
         .setProperty("hibernate.cache.provider_class", "org.hibernate.cache.NoCacheProvider")
         .setProperty("hibernate.show_sql", "true");
        HibernateUtil.setSessionFactory(config.buildSessionFactory());
    }

    public static synchronized Session getSession() {
        if (factory == null) {
            factory = new Configuration().configure().buildSessionFactory();
        }
        return factory.openSession();
    }

    public static void setSessionFactory(SessionFactory factory) {
        HibernateUtil.factory = factory;
    }
}


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 2 posts ] 

All times are UTC - 5 hours [ DST ]


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum

Search for:
© Copyright 2014, Red Hat Inc. All rights reserved. JBoss and Hibernate are registered trademarks and servicemarks of Red Hat, Inc.