Thursday, September 3, 2015

Hibernate 4 migration tips

I was migrating the spring and hibernate in my application and found couple of issues. 

The hibernate 3.2.5 was not working with the old configuration methods.


With all the dependencies I got a null pointer exception 


java.lang.NullPointerException at org.hibernate.engine.jdbc.internal.JdbcServicesImpl.configure(JdbcServicesImpl.java:207) at org.hibernate.service.internal.StandardServiceRegistryImpl.configureService(StandardServiceRegistryImpl.java:75)

The above  exception message can be resolved by adding below in Hibernate property:

<property name="hibernate.temp.use_jdbc_metadata_defaults">false</property>

For developing a standalone hibernate code to ensure the entity works with the web application we can have a utility class with main method and make the changes.

hbm.cfg.xml
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE hibernate-mapping PUBLIC
      "-//Hibernate/Hibernate Mapping DTD //EN"
          "http://www.hibernate.org/dtd/hibernate-mapping-3.0.dtd">
<hibernate-configuration>
    <session-factory>
       <property name="hibernate.connection.driver_class">oracle.jdbc.driver.OracleDriver</property>
       <property name="hibernate.connection.username">schemaname</property> 
       <property name="hibernate.connection.password">password</property>
       <property name="hibernate.connection.url">jdbc:oracle:thin:@database.com:port:databasename </property>
      <property name="hibernate.dialect">org.hibernate.dialect.Oracle10gDialect</property>
      <property name="connection.pool_size">1</property>
      <property name="show_sql">true</property>
      <property name="hibernate.temp.use_jdbc_metadata_defaults">false</property>
      <property name="hibernate.format_sql">false</property>
    </session-factory>
</hibernate-configuration>


public class HibernateUtil {
private static final SessionFactory sessionFactory;
static {
  //above config location - if present outside the class path, we use this file in the configuration object
        File f = new File("C:\\hibernate.cfg.xml");
        String xmlFile = "C:\\resources\\hibernate.query.xml";
//Add file that has the named query - use the addFile chaining method in configuration object.
//1. create a configuration object
Configuration config= new Configuration().configure(f);
config.addAnnotatedClass(Entity1.class)
.addAnnotatedClass(Entity2.class)
                .addFile(xmlFile);
//2. Create a service registry builder object and this needs to be attached to the config to get the session factory.
ServiceRegistryBuilder builder = new ServiceRegistryBuilder().applySettings(config.getProperties());

//3. buildSessionFactory expects a serviceRegistry object to create a session Factory
// the session object can be obtained from this sessionFactory - object factory.
sessionFactory = config.buildSessionFactory(builder.buildServiceRegistry());
}
public void main(String args[]){
  Session session = sessionFactory.openSession();
  Query query = session.createQuery("from entity1");
  List<Object> resobj = query.list();
  
//if we need to update the filter to the session use
 session.enableFilter("filterName");
 // Named Query
 Query query1 = session.getNamedQuery("QUERY_NAME_IN_XMLFILE");
List<Object> resobj1 = query1.list();

//if to execute the native SQL query and transform it to a entity within.
Query query2 = session.createSQLQuery("select * from ENTITY1_TABLE");
 query2.setResultTransformer(Transformers.aliasToBean(Entity1.class));
List<Entity1> resobj2 = query2.list();
}
}

//The above will execute and returns the results as expected.
Note: if there is a filter then we should use the enableFilter("name of filter")
if we have a parameterized filter we can set that using  session.enableFilter("name").setParameter("filterparam");

No comments:

Post a Comment