-->
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.  [ 4 posts ] 
Author Message
 Post subject: Can't build a SessionFactory
PostPosted: Fri Apr 07, 2006 5:49 am 
Newbie

Joined: Fri Mar 24, 2006 5:20 am
Posts: 13
Location: Zaragoza
Hi everybody. I can't build a SessionFactory and i don't know why. I'm using MySQL and an archive hibernae.config.xml:

Code:
<?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">com.mysql.jdbc.Driver</property>
        <property name="hibernate.connection.password">fryGUY538</property>
        <property name="hibernate.connection.url">jdbc:mysql://localhost/authors</property>
        <property name="hibernate.connection.username">root</property>
        <property name="hibernate.dialect">org.hibernate.dialect.MySQLDialect</property>
   
       <mapping resource="Person.hbm.xml" />
      <mapping resource="Work.hbm.xml" />
      <mapping resource="Author.hbm.xml" />

   </session-factory>
</hibernate-configuration>


and my class is :

Code:
public class Principal {
   Configuration configuration=null;
   SessionFactory sessionFactory=null;
   Session session=null;
   
   public static void main(String args[]){
      Principal p = new Principal();
      p.getSession();
      p.listar();
   }
   
   public Principal (){};
   
   public SessionFactory getSessionFactory() {
      
      
      if (sessionFactory==null){
         try {
         sessionFactory = new Configuration().configure().buildSessionFactory();
//         sessionFactory=getConfiguration().buildSessionFactory();
         }
         catch (HibernateException e){
            
         }
      }

      return sessionFactory;
      
   }

   public Session getSession() {
      if (session==null || !session.isOpen()){
         try {
            session = getSessionFactory().openSession();
         }
         catch (HibernateException e){
            
         }
      }
   return session;
   }
   

   
   public void listar (){
      
      Do something
}


Th thing is ehn I run this I obtain a null pointer exception because the method getSessionFactory returns NULL (I've seen this in Eclipse debug), and when I try to build a Session I get the null pointer exception I've said above. It gives me the following:

Code:
10:57:03,664  INFO Environment:479 - Hibernate 3.1.2
10:57:03,664  INFO Environment:509 - hibernate.properties not found
10:57:03,680  INFO Environment:525 - using CGLIB reflection optimizer
10:57:03,680  INFO Environment:555 - using JDK 1.4 java.sql.Timestamp handling
10:57:03,789  INFO Configuration:1308 - configuring from resource: /hibernate.cfg.xml
10:57:03,789  INFO Configuration:1285 - Configuration resource: /hibernate.cfg.xml
10:57:04,289  INFO Configuration:469 - Reading mappings from resource: Person.hbm.xml
10:57:04,492  INFO HbmBinder:309 - Mapping class: Person -> persons
10:57:04,523  INFO Configuration:469 - Reading mappings from resource: Work.hbm.xml
10:57:04,601  INFO HbmBinder:309 - Mapping class: Work -> works
10:57:04,601  INFO HbmBinder:1397 - Mapping collection: Work.authors -> author_work
10:57:04,664  INFO HbmBinder:824 - Mapping subclass: Book -> works
10:57:04,664  INFO HbmBinder:824 - Mapping subclass: Song -> works
10:57:04,680  INFO Configuration:469 - Reading mappings from resource: Author.hbm.xml
10:57:04,742  INFO HbmBinder:309 - Mapping class: Author -> authors
10:57:04,851  INFO HbmBinder:1397 - Mapping collection: Author.works -> author_work
10:57:04,851  INFO Configuration:1419 - Configured SessionFactory: null
10:57:04,867  INFO DriverManagerConnectionProvider:41 - Using Hibernate built-in connection pool (not for production use!)
10:57:04,867  INFO DriverManagerConnectionProvider:42 - Hibernate connection pool size: 20
10:57:04,867  INFO DriverManagerConnectionProvider:45 - autocommit mode: false
10:57:04,898  INFO DriverManagerConnectionProvider:80 - using driver: com.mysql.jdbc.Driver at URL: jdbc:mysql://localhost/authors
10:57:04,898  INFO DriverManagerConnectionProvider:86 - connection properties: {user=root, password=****}
10:57:05,273  INFO SettingsFactory:77 - RDBMS: MySQL, version: 5.0.19-nt
10:57:05,289  INFO SettingsFactory:78 - JDBC driver: MySQL-AB JDBC Driver, version: mysql-connector-java-3.1.12 ( $Date: 2005-11-17 15:53:48 +0100 (Thu, 17 Nov 2005) $, $Revision$ )
10:57:05,320  INFO Dialect:103 - Using dialect: org.hibernate.dialect.MySQLDialect
10:57:05,320  INFO TransactionFactoryFactory:31 - Using default transaction strategy (direct JDBC transactions)
10:57:05,336  INFO TransactionManagerLookupFactory:33 - No TransactionManagerLookup configured (in JTA environment, use of read-write or transactional second-level cache is not recommended)
10:57:05,430  INFO SettingsFactory:125 - Automatic flush during beforeCompletion(): disabled
10:57:05,508  INFO SettingsFactory:129 - Automatic session close at end of transaction: disabled
10:57:05,508  INFO SettingsFactory:136 - JDBC batch size: 15
10:57:05,508  INFO SettingsFactory:139 - JDBC batch updates for versioned data: disabled
10:57:05,508  INFO SettingsFactory:144 - Scrollable result sets: enabled
10:57:05,508  INFO SettingsFactory:152 - JDBC3 getGeneratedKeys(): enabled
10:57:05,508  INFO SettingsFactory:160 - Connection release mode: auto
10:57:05,508  INFO SettingsFactory:184 - Maximum outer join fetch depth: 2
10:57:05,508  INFO SettingsFactory:187 - Default batch fetch size: 1
10:57:05,508  INFO SettingsFactory:191 - Generate SQL with comments: disabled
10:57:05,508  INFO SettingsFactory:195 - Order SQL updates by primary key: disabled
10:57:05,508  INFO SettingsFactory:338 - Query translator: org.hibernate.hql.ast.ASTQueryTranslatorFactory
10:57:05,508  INFO ASTQueryTranslatorFactory:24 - Using ASTQueryTranslatorFactory
10:57:05,508  INFO SettingsFactory:203 - Query language substitutions: {}
10:57:05,508  INFO SettingsFactory:209 - Second-level cache: enabled
10:57:05,508  INFO SettingsFactory:213 - Query cache: disabled
10:57:05,523  INFO SettingsFactory:325 - Cache provider: org.hibernate.cache.EhCacheProvider
10:57:05,523  INFO SettingsFactory:228 - Optimize cache for minimal puts: disabled
10:57:05,523  INFO SettingsFactory:237 - Structured second-level cache entries: disabled
10:57:05,539  INFO SettingsFactory:264 - Statistics: disabled
10:57:05,539  INFO SettingsFactory:268 - Deleted entity synthetic identifier rollback: disabled
10:57:05,539  INFO SettingsFactory:283 - Default entity-mode: pojo
10:57:05,586  INFO SessionFactoryImpl:153 - building session factory
10:57:05,601  WARN Configurator:126 - No configuration found. Configuring ehcache from ehcache-failsafe.xml found in the classpath: jar:file:/C:/Documents%20and%20Settings/spalacin/Escritorio/hibernate-3.1.2/hibernate-3.1/lib/ehcache-1.1.jar!/ehcache-failsafe.xml
pasa por el buildSessionFactory
Exception in thread "main" java.lang.NullPointerException
   at Principal.getSession(Principal.java:58)
   at Principal.main(Principal.java:13)


Any idea? I think mapping files are okey because I dont't get any mapping exception.

Thanks in advance for your time.


Top
 Profile  
 
 Post subject:
PostPosted: Fri Apr 07, 2006 8:06 am 
Beginner
Beginner

Joined: Wed Apr 05, 2006 3:19 pm
Posts: 43
You should use this class (I see you are using hibernate 3.1?)

Code:
import net.sf.hibernate.*;
import net.sf.hibernate.cfg.*;

public class HibernateUtil {

    private static Log log = LogFactory.getLog(HibernateUtil.class);

    private static final SessionFactory sessionFactory;

    static {
        try {
            // Create the SessionFactory
            sessionFactory = new Configuration().configure().buildSessionFactory();
        } catch (Throwable ex) {
            log.error("Initial SessionFactory creation failed.", ex);
            throw new ExceptionInInitializerError(ex);
        }
    }

    public static final ThreadLocal session = new ThreadLocal();

    public static Session currentSession() throws HibernateException {
        Session s = (Session) session.get();
        // Open a new Session, if this Thread has none yet
        if (s == null) {
            s = sessionFactory.openSession();
            session.set(s);
        }
        return s;
    }

    public static void closeSession() throws HibernateException {
        Session s = (Session) session.get();
        session.set(null);
        if (s != null)
            s.close();
    }
}


More info in the documentation: http://www.hibernate.org/hib_docs/refer ... start.html


Top
 Profile  
 
 Post subject:
PostPosted: Fri Apr 07, 2006 8:42 am 
Newbie

Joined: Fri Mar 24, 2006 5:20 am
Posts: 13
Location: Zaragoza
Thank you for anwering me, Laurens. I'm very new at hibernate and quite new at Java, so however your class wouks very well (THANK YOU VERY MUCH), I don't understand some things.

First of all, I've never heard about a LogFactory (moreover, when I try to use it Eclipse underlines it, the imports doesn't work for it .Perhaps I'm using a unupdated version, yes, I'm working with Híbernate 3.1), but the class still works without it.

I've also never worked with a thread, but I know what they are. Must I use it? Another way for working?

Why do you declares everything static? Can't you create a class, make and object and use its methods? Which adventages will I obtain if I declare static everything?

Thanks again.


Top
 Profile  
 
 Post subject:
PostPosted: Sat Apr 08, 2006 11:44 am 
Beginner
Beginner

Joined: Wed Apr 05, 2006 3:19 pm
Posts: 43
The logfactory is for logging purposes (the [INFO] and [DEBUG] messages you see).

To answer your second question, a static field is a class field, which means that its data is stored in one location in memory, no matter how many objects are created from its class. So imagine you had an integer "count", like this:

static int count = 0;
public MyClass(){
count++;
}

Imagine you make two objects of MyClass, this would be the result:

MyClass one = new MyClass();
MyClass two = new MyClass();
system.out.println("Count is: " + two.count); // or one.count or MyClass.count

> Count is: 2

If you wouldn't have made it static, it would say:

system.out.println("Count is: " + one.getCount()); // or two.getCount()

> Count is: 1


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 4 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.