-->
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.  [ 9 posts ] 
Author Message
 Post subject: Generic Dao Problem getHibernateTemplate() is returning null
PostPosted: Thu Mar 05, 2009 3:24 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() {
   

    public static SessionFactory getSessionFactory() {
        return sessionFactory;
    }

 
}


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 Mar 05, 2009 3:34 am 
Senior
Senior

Joined: Wed Sep 19, 2007 9:31 pm
Posts: 191
Location: Khuntien (Indonesia)
have you injected the sessionfactory into your HibernateDaoSupport ?


Top
 Profile  
 
 Post subject: no
PostPosted: Thu Mar 05, 2009 3:39 am 
Beginner
Beginner

Joined: Fri Feb 20, 2009 2:19 am
Posts: 33
no i havent...
how to inject it?
i mean is thr configuration in xml or any thing else?


Top
 Profile  
 
 Post subject: no
PostPosted: Thu Mar 05, 2009 3:41 am 
Beginner
Beginner

Joined: Fri Feb 20, 2009 2:19 am
Posts: 33
no i havent...
how to inject it?
i mean is thr configuration in xml or any thing else?


Top
 Profile  
 
 Post subject: it is totally jar project
PostPosted: Thu Mar 05, 2009 4:39 am 
Beginner
Beginner

Joined: Fri Feb 20, 2009 2:19 am
Posts: 33
Note: it is totally jar project


Top
 Profile  
 
 Post subject: new error
PostPosted: Thu Mar 05, 2009 5:05 am 
Beginner
Beginner

Joined: Fri Feb 20, 2009 2:19 am
Posts: 33
error of getting null is solved by following code:

Code:
 
       HibernateTemplate ob= new HibernateTemplate(HibernateUtil.getSessionFactory(),true);
         ob.saveOrUpdate(entity);
         System.out.println("in saveOrUpdate");
 


but it is not getting reflected in DB.
i am not getting any execption now.


Top
 Profile  
 
 Post subject:
PostPosted: Thu Mar 05, 2009 8:34 pm 
Newbie

Joined: Thu Mar 05, 2009 7:11 pm
Posts: 1
Location: SJSU
Hi

Even I having the sample problem. Can u say a bit specific like where to place the sample code either in DAOImpl class or other location.

It would be helpful if you keep a word on this.

Thanks
Raghu

_________________
Thanks
Raghu


Top
 Profile  
 
 Post subject:
PostPosted: Thu Mar 12, 2009 9:15 am 
Beginner
Beginner

Joined: Fri Feb 20, 2009 2:19 am
Posts: 33
in the impl class


Top
 Profile  
 
 Post subject: Re: no
PostPosted: Thu Mar 12, 2009 9:51 am 
Senior
Senior

Joined: Wed Sep 19, 2007 9:31 pm
Posts: 191
Location: Khuntien (Indonesia)
void_void wrote:
no i havent...
how to inject it?
i mean is thr configuration in xml or any thing else?


HibernateDaoSupport is the class from spring. Could you post your spring xml configuration (applicationContext.xml or something else)?

this is the sample code
Code:
<bean id="authorDao" class="org.sample.dao.impl.AuthorDaoImpl">
      <property name="sessionFactory" ref="sessionFactory" />
</bean>
...

<bean id="sessionFactory"
      class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
      <property name="dataSource" ref="dataSource" />
      <property name="configurationClass">
         <value>org.hibernate.cfg.AnnotationConfiguration</value>
      </property>
      <property name="configLocations">
         <list>
            <value>classpath:hibernate.cfg.xml</value>
         </list>
      </property>
      <property name="hibernateProperties">
         <props>
            <prop key="hibernate.connection.driver_class">${hibernate.connection.driver_class}</prop>
            <prop key="hibernate.connection.url">${hibernate.connection.url}</prop>
            <prop key="hibernate.dialect">${hibernate.dialect}</prop>
            <prop key="hibernate.transaction.factory_class">${hibernate.transaction.factory_class}</prop>
            <prop key="hibernate.current_session_context_class">${hibernate.current_session_context_class}</prop>
            <prop key="hibernate.jdbc.batch_size">${hibernate.jdbc.batch_size}</prop>
            <prop key="hibernate.transaction.auto_close_session">${hibernate.transaction.auto_close_session}</prop>
            <prop key="hibernate.show_sql">${hibernate.show_sql}</prop>
            <prop key="hibernate.format_sql">${hibernate.format_sql}</prop>
         </props>
      </property>
   </bean>


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