-->
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: Generic Dao Problem getHibernateTemplate() is returning null
PostPosted: Thu Mar 05, 2009 3:21 am 
Beginner
Beginner

Joined: Fri Feb 20, 2009 2:19 am
Posts: 33
here is my 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.dialect">org.hibernate.dialect.OracleDialect</property>
    <property name="hibernate.connection.driver_class">oracle.jdbc.OracleDriver</property>
    <property name="hibernate.connection.url">jdbc:oracle:thin:@192.1.200.32:1521:orcl</property>
    <property name="hibernate.connection.username">****</property>
    <property name="hibernate.connection.password">****</property>
      <property name="current_session_context_class">thread</property>
      <mapping class="javaapplication42.Employee"/>
  </session-factory>
</hibernate-configuration>


Employee.java
Code:
package javaapplication42;

import java.io.Serializable;

import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.Id;
import javax.persistence.Table;

@Entity
@Table(name = "EMPLOYEE")
public class Employee implements Serializable {
   public Employee() {

   }
   @Id
   @Column(name = "id")
   Integer id;

    public Employee(Integer id, String name) {
        this.id = id;
        this.name = name;
    }

   @Column(name = "name")
   String name;

   public Integer getId() {
      return id;
   }

   public void setId(Integer id) {
      this.id = id;
   }

   public String getName() {
      return name;
   }

   public void setName(String name) {
      this.name = name;
   }

}


EmployeeImpl.java
Code:
package javaapplication42;

public class EmployeeImpl extends BaseAbstractDao<Employee> implements EmployeeDao{

    /**
     * @param args the command line arguments
     */
    @Override
    public void saveEmployee(Employee employee) {
        System.out.println("in saveEmployee");
       super.saveOrUpdateEntity(employee);
    }
   public EmployeeImpl(){
       System.out.println("in EmployeeImpl");
        saveEmployee(new Employee(new Integer(1),"abc"));
    }
}

GenericDao.java
Code:
package javaapplication42;

public interface GenericDao  <T> {
public void saveOrUpdateEntity(T entity);

}

EmployeeDao.java
Code:
package javaapplication42;

/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/



/**
*
* @author jjoshi
*/
public interface EmployeeDao extends GenericDao<Employee> {
     public void saveEmployee(Employee employee);

}

BaseAbstractDao.java
Code:
package javaapplication42;




import java.lang.reflect.ParameterizedType;
import org.springframework.orm.hibernate3.HibernateTemplate;
import org.springframework.orm.hibernate3.support.HibernateDaoSupport;

public abstract class BaseAbstractDao<EntityType> extends HibernateDaoSupport implements GenericDao<EntityType> {
     private Class<EntityType> persistentClass;
     @SuppressWarnings("unchecked")
    public BaseAbstractDao() {
        super();
        this.persistentClass = (Class<EntityType>) ((ParameterizedType) getClass().getGenericSuperclass()).getActualTypeArguments()[0];
         System.out.println("in BAseAbstractDao");
    }
     @Override
    public void saveOrUpdateEntity(EntityType entity) {

         System.out.println("output = "+getHibernateTemplate());
         System.out.println("in saveOrUpdate");
    }
}

HibernateUtil.java
Code:
package javaapplication42;



import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.Statement;
import org.hibernate.SessionFactory;
import org.hibernate.cfg.AnnotationConfiguration;

public class HibernateUtil {

    private Statement st;
    private static final SessionFactory sessionFactory;


    static {

        try {
            // Create the SessionFactory from hibernate.cfg.xml
            sessionFactory = new AnnotationConfiguration().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 HibernateUtil() {
        try {
            Class.forName("oracle.jdbc.driver.OracleDriver");
            Connection con = DriverManager.getConnection("jdbc:oracle:thin:@192.1.200.32:1521:orcl", "student", "student");
            st = con.createStatement();
        } catch (Exception e) {
            System.out.println(e);
        }
    }

    public static SessionFactory getSessionFactory() {
        return sessionFactory;
    }

    public ResultSet executeSQLCommand(String sql) throws Exception {

       
        return (st.executeQuery(sql));
    }
}


Main.java

Code:
package javaapplication42;


import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;
public class main {
    public static void main(String[] args) {
    SessionFactory session = HibernateUtil.getSessionFactory();
     Session sess = session.getCurrentSession();
        /** Starting the Transaction */
        Transaction tx = sess.beginTransaction();
        System.out.println("Transaction is "  +tx );
        System.out.println("Session is  "  +sess );
        EmployeeImpl ob= new EmployeeImpl();

    }

}
theException i am getting is
run:
5 Mar, 2009 12:39:30 PM org.hibernate.cfg.annotations.Version <clinit>
INFO: Hibernate Annotations 3.3.1.GA
5 Mar, 2009 12:39:30 PM org.hibernate.cfg.Environment <clinit>
INFO: Hibernate 3.2.5
5 Mar, 2009 12:39:30 PM org.hibernate.cfg.Environment <clinit>
INFO: hibernate.properties not found
5 Mar, 2009 12:39:30 PM org.hibernate.cfg.Environment buildBytecodeProvider
INFO: Bytecode provider name : cglib
5 Mar, 2009 12:39:30 PM org.hibernate.cfg.Environment <clinit>
INFO: using JDK 1.4 java.sql.Timestamp handling
5 Mar, 2009 12:39:30 PM org.hibernate.cfg.Configuration configure
INFO: configuring from resource: /hibernate.cfg.xml
5 Mar, 2009 12:39:30 PM org.hibernate.cfg.Configuration getConfigurationInputStream
INFO: Configuration resource: /hibernate.cfg.xml
5 Mar, 2009 12:39:30 PM org.hibernate.cfg.Configuration doConfigure
INFO: Configured SessionFactory: null
5 Mar, 2009 12:39:30 PM org.hibernate.cfg.AnnotationBinder bindClass
INFO: Binding entity from annotated class: javaapplication42.Employee
5 Mar, 2009 12:39:30 PM org.hibernate.cfg.annotations.EntityBinder bindTable
INFO: Bind entity javaapplication42.Employee on table EMPLOYEE
5 Mar, 2009 12:39:31 PM org.hibernate.cfg.AnnotationConfiguration secondPassCompile
INFO: Hibernate Validator not found: ignoring
5 Mar, 2009 12:39:31 PM org.hibernate.connection.DriverManagerConnectionProvider configure
INFO: Using Hibernate built-in connection pool (not for production use!)
5 Mar, 2009 12:39:31 PM org.hibernate.connection.DriverManagerConnectionProvider configure
INFO: Hibernate connection pool size: 20
5 Mar, 2009 12:39:31 PM org.hibernate.connection.DriverManagerConnectionProvider configure
INFO: autocommit mode: false
5 Mar, 2009 12:39:31 PM org.hibernate.connection.DriverManagerConnectionProvider configure
INFO: using driver: oracle.jdbc.OracleDriver at URL: jdbc:oracle:thin:@192.1.200.32:1521:orcl
5 Mar, 2009 12:39:31 PM org.hibernate.connection.DriverManagerConnectionProvider configure
INFO: connection properties: {user=student, password=****}
5 Mar, 2009 12:39:33 PM org.hibernate.cfg.SettingsFactory buildSettings
INFO: RDBMS: Oracle, version: Oracle Database 10g Enterprise Edition Release 10.1.0.2.0 - Production
With the Partitioning, OLAP and Data Mining options
5 Mar, 2009 12:39:33 PM org.hibernate.cfg.SettingsFactory buildSettings
INFO: JDBC driver: Oracle JDBC driver, version: 10.2.0.1.0XE
5 Mar, 2009 12:39:33 PM org.hibernate.dialect.Dialect <init>
INFO: Using dialect: org.hibernate.dialect.OracleDialect
5 Mar, 2009 12:39:33 PM org.hibernate.dialect.Oracle9Dialect <init>
WARNING: The Oracle9Dialect dialect has been deprecated; use either Oracle9iDialect or Oracle10gDialect instead
5 Mar, 2009 12:39:33 PM org.hibernate.dialect.OracleDialect <init>
WARNING: The OracleDialect dialect has been deprecated; use Oracle8iDialect instead
5 Mar, 2009 12:39:33 PM org.hibernate.transaction.TransactionFactoryFactory buildTransactionFactory
INFO: Using default transaction strategy (direct JDBC transactions)
5 Mar, 2009 12:39:33 PM org.hibernate.transaction.TransactionManagerLookupFactory getTransactionManagerLookup
INFO: No TransactionManagerLookup configured (in JTA environment, use of read-write or transactional second-level cache is not recommended)
5 Mar, 2009 12:39:33 PM org.hibernate.cfg.SettingsFactory buildSettings
INFO: Automatic flush during beforeCompletion(): disabled
5 Mar, 2009 12:39:33 PM org.hibernate.cfg.SettingsFactory buildSettings
INFO: Automatic session close at end of transaction: disabled
5 Mar, 2009 12:39:33 PM org.hibernate.cfg.SettingsFactory buildSettings
INFO: JDBC batch size: 15
5 Mar, 2009 12:39:33 PM org.hibernate.cfg.SettingsFactory buildSettings
INFO: JDBC batch updates for versioned data: disabled
5 Mar, 2009 12:39:33 PM org.hibernate.cfg.SettingsFactory buildSettings
INFO: Scrollable result sets: enabled
5 Mar, 2009 12:39:33 PM org.hibernate.cfg.SettingsFactory buildSettings
INFO: JDBC3 getGeneratedKeys(): disabled
5 Mar, 2009 12:39:33 PM org.hibernate.cfg.SettingsFactory buildSettings
INFO: Connection release mode: auto
5 Mar, 2009 12:39:33 PM org.hibernate.cfg.SettingsFactory buildSettings
INFO: Default batch fetch size: 1
5 Mar, 2009 12:39:33 PM org.hibernate.cfg.SettingsFactory buildSettings
INFO: Generate SQL with comments: disabled
5 Mar, 2009 12:39:33 PM org.hibernate.cfg.SettingsFactory buildSettings
INFO: Order SQL updates by primary key: disabled
5 Mar, 2009 12:39:33 PM org.hibernate.cfg.SettingsFactory buildSettings
INFO: Order SQL inserts for batching: disabled
5 Mar, 2009 12:39:33 PM org.hibernate.cfg.SettingsFactory createQueryTranslatorFactory
INFO: Query translator: org.hibernate.hql.ast.ASTQueryTranslatorFactory
5 Mar, 2009 12:39:33 PM org.hibernate.hql.ast.ASTQueryTranslatorFactory <init>
INFO: Using ASTQueryTranslatorFactory
5 Mar, 2009 12:39:33 PM org.hibernate.cfg.SettingsFactory buildSettings
INFO: Query language substitutions: {}
5 Mar, 2009 12:39:33 PM org.hibernate.cfg.SettingsFactory buildSettings
INFO: JPA-QL strict compliance: disabled
5 Mar, 2009 12:39:33 PM org.hibernate.cfg.SettingsFactory buildSettings
INFO: Second-level cache: enabled
5 Mar, 2009 12:39:33 PM org.hibernate.cfg.SettingsFactory buildSettings
INFO: Query cache: disabled
5 Mar, 2009 12:39:33 PM org.hibernate.cfg.SettingsFactory createCacheProvider
INFO: Cache provider: org.hibernate.cache.NoCacheProvider
5 Mar, 2009 12:39:33 PM org.hibernate.cfg.SettingsFactory buildSettings
INFO: Optimize cache for minimal puts: disabled
5 Mar, 2009 12:39:33 PM org.hibernate.cfg.SettingsFactory buildSettings
INFO: Structured second-level cache entries: disabled
5 Mar, 2009 12:39:33 PM org.hibernate.cfg.SettingsFactory buildSettings
INFO: Statistics: disabled
5 Mar, 2009 12:39:33 PM org.hibernate.cfg.SettingsFactory buildSettings
INFO: Deleted entity synthetic identifier rollback: disabled
5 Mar, 2009 12:39:33 PM org.hibernate.cfg.SettingsFactory buildSettings
INFO: Default entity-mode: pojo
5 Mar, 2009 12:39:33 PM org.hibernate.cfg.SettingsFactory buildSettings
INFO: Named query checking : enabled
5 Mar, 2009 12:39:36 PM org.hibernate.impl.SessionFactoryImpl <init>
INFO: building session factory
5 Mar, 2009 12:39:36 PM org.hibernate.impl.SessionFactoryObjectFactory addInstance
INFO: Not binding factory to JNDI, no JNDI name configured
Transaction is org.hibernate.transaction.JDBCTransaction@1976011
Session is SessionImpl(PersistenceContext[entityKeys=[],collectionKeys=[]];ActionQueue[insertions=[] updates=[] deletions=[] collectionCreations=[] collectionRemovals=[] collectionUpdates=[]])
in BAseAbstractDao
in EmployeeImpl
in saveEmployee
output = null
in saveOrUpdate
BUILD SUCCESSFUL (total time: 8 seconds)


getHibernateTemplate() is returning null
how to overcome it???




Top
 Profile  
 
 Post subject:
PostPosted: Thu Apr 16, 2009 3:18 pm 
Newbie

Joined: Thu Apr 16, 2009 1:36 pm
Posts: 1
Hola!

Have you configured the ApplicationContext for HibernateSessionFactory injection?
The HibernateSessionFactory can be autowired if you want, otherwise it will always be null HibernateTemplate

Try this in your BaseAbstractDao:
Code:
@Autowired
public final void setHibernateSessionFactory(@Qualifier("hibernateSessionFactory") SessionFactory sessionFactory) {
        setSessionFactory(sessionFactory);
}



Saludos

Ezequiel


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.