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.