Hey,
I'm trying to setup a simple Hibernate project with Hibernate, I'm used on working with NHibernate. However now I run into strange things..
I've made a query
Code:
@SuppressWarnings("unchecked")
public void openHibernate(){
Session session = SessionFactoryUtil.getSessionFactory().openSession();
try {
Transaction tx = session.beginTransaction();
List countries = session.createQuery("from countries").list();
for (Iterator iter = countries.iterator(); iter.hasNext();) {
Countries element = (Countries) iter.next();
System.out.println(element.getCountryName());
}
tx.commit();
} catch (RuntimeException e) {
}
}
Whenever I run this, the result is that while debugging the command parser skips from the iterator to the end of the catch block.. result => an empty table (with cascades an empty database).. I tried my sample on both Oracle 10.2 PE and XE, both give the same result
Runner.java
Code:
package HibTest;
import java.util.Iterator;
import java.util.List;
import model.Countries;
import model.Employees;
import model.Jobs;
import model.Locations;
import org.hibernate.Session;
import org.hibernate.Transaction;
public class Runner {
/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
Runner run = new Runner();
run.openHibernate();
SessionFactoryUtil.getSessionFactory().close();
}
@SuppressWarnings("unchecked")
public void openHibernate(){
Session session = SessionFactoryUtil.getSessionFactory().openSession();
try {
Transaction tx = session.beginTransaction();
List countries = session.createQuery("from countries").list();
for (Iterator iter = countries.iterator(); iter.hasNext();) {
Countries element = (Countries) iter.next();
System.out.println(element.getCountryName());
}
tx.commit();
} catch (RuntimeException e) {
}
}
}
SessionFactoryUtil
Code:
package HibTest;
import org.hibernate.SessionFactory; // import org.hibernate.cfg.AnnotationConfiguration;
import org.hibernate.cfg.Configuration;
/**
* This class garanties that only one single SessionFactory
* is instanciated and that the configuration is done thread safe as
* singleton. Actually it only wraps the Hibernate SessionFactory.
* You are free to use any kind of JTA or Thread transactionFactories.
*/
public class SessionFactoryUtil {
private static final SessionFactory sessionFactory;
static {
try {
// Create the SessionFactory from hibernate.cfg.xml
sessionFactory = new Configuration().configure().buildSessionFactory();
} catch (Throwable ex) {
// Make sure you log the exception, as it might be swallowed
System.err.println("Initial SessionFactory creation failed." + ex);
throw new ExceptionInInitializerError(ex);
}
}
public static SessionFactory getSessionFactory() {
return sessionFactory;
}
}
Hibernate.cfg.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">oracle.jdbc.driver.OracleDriver</property>
<property name="hibernate.connection.password">hr</property>
<property name="hibernate.connection.url">jdbc:oracle:thin:@127.0.0.1:1521:orcl</property>
<property name="hibernate.connection.username">hr</property>
<property name="hibernate.dialect">org.hibernate.dialect.Oracle10gDialect</property>
<!-- Enable Hibernate's automatic session context management -->
<property name="current_session_context_class">thread</property>
<!-- Disable the second-level cache -->
<property name="cache.provider_class">org.hibernate.cache.NoCacheProvider</property>
<property name="transaction.factory_class">org.hibernate.transaction.JDBCTransactionFactory</property>
<!-- this will show us all sql statements -->
<property name="hibernate.show_sql">true</property>
<!-- this will create the database tables for us -->
<property name="hibernate.hbm2ddl.auto">create</property>
<mapping class="model.Countries" resource="model/Countries.hbm.xml"/>
<mapping class="model.Jobs" resource="model/Jobs.hbm.xml"/>
<mapping class="model.Locations" resource="model/Locations.hbm.xml"/>
<mapping class="model.Regions" resource="model/Regions.hbm.xml"/>
<mapping class="model.Employees" resource="model/Employees.hbm.xml"/>
</session-factory>
</hibernate-configuration>
Countries.hbm.xml
Code:
<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
<!-- Generated 22-mrt-2009 11:38:42 by Hibernate Tools 3.2.4.GA -->
<hibernate-mapping>
<class name="model.Countries" table="COUNTRIES" schema="HR">
<id name="countryId" type="string">
<column name="COUNTRY_ID" length="2" />
<generator class="assigned" />
</id>
<many-to-one name="regions" class="model.Regions" fetch="select">
<column name="REGION_ID" precision="22" scale="0" />
</many-to-one>
<property name="countryName" type="string">
<column name="COUNTRY_NAME" length="40" />
</property>
<set name="locationses" inverse="true" lazy="true" table="LOCATIONS" fetch="select">
<key>
<column name="COUNTRY_ID" length="2" />
</key>
<one-to-many class="model.Locations" />
</set>
</class>
</hibernate-mapping>
and the console output
Code:
19:55:58,425 INFO Environment:543 - Hibernate 3.3.1.GA
19:55:58,441 INFO Environment:576 - hibernate.properties not found
19:55:58,456 INFO Environment:709 - Bytecode provider name : javassist
19:55:58,456 INFO Environment:627 - using JDK 1.4 java.sql.Timestamp handling
19:55:58,628 INFO Configuration:1460 - configuring from resource: /hibernate.cfg.xml
19:55:58,628 INFO Configuration:1437 - Configuration resource: /hibernate.cfg.xml
19:55:58,894 INFO Configuration:586 - Reading mappings from resource : model/Countries.hbm.xml
19:55:59,019 INFO HbmBinder:322 - Mapping class: model.Countries -> COUNTRIES
19:55:59,222 INFO Configuration:586 - Reading mappings from resource : model/Jobs.hbm.xml
19:55:59,253 INFO HbmBinder:322 - Mapping class: model.Jobs -> JOBS
19:55:59,253 INFO Configuration:586 - Reading mappings from resource : model/Locations.hbm.xml
19:55:59,284 INFO HbmBinder:322 - Mapping class: model.Locations -> LOCATIONS
19:55:59,284 INFO Configuration:586 - Reading mappings from resource : model/Regions.hbm.xml
19:55:59,300 INFO HbmBinder:322 - Mapping class: model.Regions -> REGIONS
19:55:59,300 INFO Configuration:586 - Reading mappings from resource : model/Employees.hbm.xml
19:55:59,316 INFO HbmBinder:322 - Mapping class: model.Employees -> EMPLOYEES
19:55:59,316 INFO Configuration:1575 - Configured SessionFactory: null
19:55:59,316 INFO HbmBinder:2404 - Mapping collection: model.Countries.locationses -> LOCATIONS
19:55:59,331 INFO HbmBinder:2404 - Mapping collection: model.Regions.countrieses -> COUNTRIES
19:55:59,331 INFO HbmBinder:2404 - Mapping collection: model.Employees.employeeses -> EMPLOYEES
19:55:59,331 INFO DriverManagerConnectionProvider:64 - Using Hibernate built-in connection pool (not for production use!)
19:55:59,331 INFO DriverManagerConnectionProvider:65 - Hibernate connection pool size: 20
19:55:59,331 INFO DriverManagerConnectionProvider:68 - autocommit mode: false
19:55:59,691 INFO DriverManagerConnectionProvider:103 - using driver: oracle.jdbc.driver.OracleDriver at URL: jdbc:oracle:thin:@127.0.0.1:1521:orcl
19:55:59,691 INFO DriverManagerConnectionProvider:109 - connection properties: {user=hr, password=****}
19:56:03,034 INFO SettingsFactory:116 - RDBMS: Oracle, version: Oracle Database 10g Release 10.2.0.1.0 - Production
19:56:03,034 INFO SettingsFactory:117 - JDBC driver: Oracle JDBC driver, version: 11.1.0.7.0-Production
19:56:03,191 INFO Dialect:175 - Using dialect: org.hibernate.dialect.Oracle10gDialect
19:56:03,237 INFO TransactionFactoryFactory:62 - Transaction strategy: org.hibernate.transaction.JDBCTransactionFactory
19:56:03,237 INFO TransactionManagerLookupFactory:80 - No TransactionManagerLookup configured (in JTA environment, use of read-write or transactional second-level cache is not recommended)
19:56:03,237 INFO SettingsFactory:170 - Automatic flush during beforeCompletion(): disabled
19:56:03,237 INFO SettingsFactory:174 - Automatic session close at end of transaction: disabled
19:56:03,237 INFO SettingsFactory:181 - JDBC batch size: 15
19:56:03,237 INFO SettingsFactory:184 - JDBC batch updates for versioned data: disabled
19:56:03,253 INFO SettingsFactory:189 - Scrollable result sets: enabled
19:56:03,253 INFO SettingsFactory:197 - JDBC3 getGeneratedKeys(): disabled
19:56:03,253 INFO SettingsFactory:205 - Connection release mode: auto
19:56:03,253 INFO SettingsFactory:232 - Default batch fetch size: 1
19:56:03,253 INFO SettingsFactory:236 - Generate SQL with comments: disabled
19:56:03,253 INFO SettingsFactory:240 - Order SQL updates by primary key: disabled
19:56:03,253 INFO SettingsFactory:244 - Order SQL inserts for batching: disabled
19:56:03,253 INFO SettingsFactory:420 - Query translator: org.hibernate.hql.ast.ASTQueryTranslatorFactory
19:56:03,284 INFO ASTQueryTranslatorFactory:47 - Using ASTQueryTranslatorFactory
19:56:03,284 INFO SettingsFactory:252 - Query language substitutions: {}
19:56:03,284 INFO SettingsFactory:257 - JPA-QL strict compliance: disabled
19:56:03,284 INFO SettingsFactory:262 - Second-level cache: enabled
19:56:03,284 INFO SettingsFactory:266 - Query cache: disabled
19:56:03,284 INFO SettingsFactory:405 - Cache region factory : org.hibernate.cache.impl.bridge.RegionFactoryCacheProviderBridge
19:56:03,284 INFO RegionFactoryCacheProviderBridge:61 - Cache provider: org.hibernate.cache.NoCacheProvider
19:56:03,284 INFO SettingsFactory:276 - Optimize cache for minimal puts: disabled
19:56:03,300 INFO SettingsFactory:285 - Structured second-level cache entries: disabled
19:56:03,300 INFO SettingsFactory:305 - Echoing all SQL to stdout
19:56:03,300 INFO SettingsFactory:314 - Statistics: disabled
19:56:03,300 INFO SettingsFactory:318 - Deleted entity synthetic identifier rollback: disabled
19:56:03,300 INFO SettingsFactory:333 - Default entity-mode: pojo
19:56:03,300 INFO SettingsFactory:337 - Named query checking : enabled
19:56:03,566 INFO SessionFactoryImpl:187 - building session factory
19:56:04,300 INFO SessionFactoryObjectFactory:105 - Not binding factory to JNDI, no JNDI name configured
19:56:04,362 INFO SchemaExport:226 - Running hbm2ddl schema export
19:56:04,362 INFO SchemaExport:251 - exporting generated schema to database
19:56:05,706 INFO SchemaExport:268 - schema export complete
19:57:16,909 INFO SessionFactoryImpl:805 - closing
19:57:16,909 INFO DriverManagerConnectionProvider:170 - cleaning up connection pool: jdbc:oracle:thin:@127.0.0.1:1521:orcl
Any help would be much appreciated, thanks!