My Quotes


When U were born , you cried and the world rejoiced
Live U'r life in such a way that when you go
THE WORLD SHOULD CRY






Thursday, April 18, 2013

Get DB Date and Time Hibernate / JPA



1.If you fire the query " SELECT sysdate param FROM dual;" (or) " SELECT systimestamp param FROM dual;"Hibernate will complain " org.hibernate.MappingException: No Dialect mapping for JDBC type: -101". Not sure of the JPA error
2.To address this, you need to ensure that Hibernate is educated to identify the sysdate (or) systimestamp as a registered field
3.The only way to do this is to addScalar to the SQLQuery
4.But here again if you say addScalar("param",Hibernate.TIMESTAMP), Type fields in org.hibernate.Hibernate are deprecated (and actually removed) as result of HHH-5196
5.So the other way is to say as follows
6.sqlQuery.addScalar("param", org.hibernate.type.TimestampType.INSTANCE);
7.Now that this is done for Hibernate, I was wondering the alternative for JPA.
8.So the thIrd hack was to hack the JPA Entity Manager to use Hibernate scalar as follows
9.query = entityMgrObj.createNativeQuery(queryString);
10.query.unwrap(SQLQuery.class).addScalar("param", org.hibernate.type.TimestampType.INSTANCE);


So here we go





For hibernate

  /**


  * public method to get the database Date and Time


  */


  public Timestamp getCurrentDBTime() throws ORMException { 


   SQLQuery sqlQuery = null;


   String queryString=null;


   Timestamp dbTimeStamp = null;


   try {


    queryString = "SELECT systimestamp param FROM DUAL";


    sessionObj = getHibSession();   


    sqlQuery = sessionObj.createSQLQuery(queryString);


    sqlQuery.addScalar("param", org.hibernate.type.TimestampType.INSTANCE);


    dbTimeStamp = (Timestamp)sqlQuery.uniqueResult(); 


    return dbTimeStamp;


   } catch (HibernateException ex) {


    throw new ORMException(ex, IORMErrorCode.LQM_ORM_E_GETCURRENT_DATE_TIME);


   } catch (RuntimeException ex) {


    throw new ORMException(ex, IORMErrorCode.LQM_ORM_E_GEN_EXCEPTION);


   } finally {


    if (sessionObj != null) {


     sessionObj.close();


    }


   }


   


  }


  /**


  * public method to get the database Date


  */




  public Date getCurrentDBDate() throws ORMException {


   SQLQuery sqlQuery = null;


   String queryString=null;


   Date dbDate = null;


   try {


    queryString = "SELECT sysdate param FROM DUAL";


    sessionObj = getHibSession();   


    sqlQuery = sessionObj.createSQLQuery(queryString);


    sqlQuery.addScalar("param", org.hibernate.type.DateType.INSTANCE);


    dbDate = (Date)sqlQuery.uniqueResult(); 


    return dbDate;


   } catch (HibernateException ex) {


    throw new ORMException(ex, IORMErrorCode.LQM_ORM_E_GETCURRENT_DATE_TIME);


   } catch (RuntimeException ex) {


    throw new ORMException(ex, IORMErrorCode.LQM_ORM_E_GEN_EXCEPTION);


   } finally {


    if (sessionObj != null) {


     sessionObj.close();


    }


   }


  }






For JPA





  /**


  * public method to get the database Date and Time


  */


  public Timestamp getCurrentDBTime() throws ORMException {


   Timestamp dbTimeStamp = null;


   String queryString=null;


   Query query = null;


   try {


    queryString = "SELECT systimestamp param FROM DUAL";


    entityMgrObj = getHibJpaEntityManager();


    query = entityMgrObj.createNativeQuery(queryString);


    query.unwrap(SQLQuery.class).addScalar("param", org.hibernate.type.TimestampType.INSTANCE);


    dbTimeStamp = (Timestamp)query.getSingleResult();


    return dbTimeStamp;


   } catch (HibernateException ex) {


    throw new ORMException(ex, IORMErrorCode.LQM_ORM_E_GETCURRENT_DATE_TIME);


   } catch (RuntimeException ex) {


    throw new ORMException(ex, IORMErrorCode.LQM_ORM_E_GEN_EXCEPTION);


   } 


  


  }


  /**


  * public method to get the database Date


  */


  public Date getCurrentDBDate() throws ORMException {


   Date dbDate = null;


   String queryString=null;


   Query query = null;


   try {


    queryString = "SELECT sysdate param FROM DUAL";


    entityMgrObj = getHibJpaEntityManager();


    query = entityMgrObj.createNativeQuery(queryString);


    query.unwrap(SQLQuery.class).addScalar("param", org.hibernate.type.DateType.INSTANCE);


    dbDate = (Date)query.getSingleResult();


    return dbDate;


   } catch (HibernateException ex) {


    throw new ORMException(ex, IORMErrorCode.LQM_ORM_E_GETCURRENT_DATE_TIME);


   } catch (RuntimeException ex) {


    throw new ORMException(ex, IORMErrorCode.LQM_ORM_E_GEN_EXCEPTION);


   } 


  


  }


setting schema information


schema names in hibernate can be painful but not with the latest releases

Refer to the following URL and you will get useful information of setting the schema names for the application at different levels


  1. Global Level
  2. Entity Level
  3. Property Level and
  4. Association Level

For JPA rather than doing it in every ENTITY class
just create the orm.xml with the following contents

<?xml version="1.0" encoding="UTF-8" ?>
<entity-mappings xmlns="http://java.sun.com/xml/ns/persistence/orm"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://java.sun.com/xml/ns/persistence/orm    
    http://java.sun.com/xml/ns/persistence/orm_1_0.xsd"
    version="1.0">
    <persistence-unit-metadata>
        <persistence-unit-defaults>
            <schema>mySchema</schema>
        </persistence-unit-defaults>
    </persistence-unit-metadata>
</entity-mappings>

Make sure that you refer this from the persistence.xml as follows

<persistence-unit>
<mapping-file>META-INF/orm.xml</mapping-file>
</persistence-unit>


Speed up Spring loading

add the following to the web.xml

 metadata-complete="true"

<web-app version="3.0" xmlns="http://java.sun.com/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee 
http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" metadata-complete="true">

and you will see the difference in Spring performance

Thursday, April 4, 2013

Useful Debugging parameters for Hibernate via Log4j

### Hibernate logging configuration ### 

### Log everything (a lot of information, but very useful for troubleshooting) ### 
#log4j.logger.org.hibernate=info 

### Log HQL and SQL ASTs during query parsing ### 
log4j.logger.org.hibernate.hql.ast.AST=DEBUG, SQL_APPENDER 
log4j.additivity.org.hibernate.hql.ast.AST=false 

### log just the SQL 
log4j.logger.org.hibernate.SQL=DEBUG, SQL_APPENDER 
log4j.additivity.org.hibernate.SQL=false 

### log JDBC bind parameters. Very userfull, when debug parameterized queries ### 
log4j.logger.org.hibernate.type=TRACE, SQL_APPENDER 
log4j.additivity.org.hibernate.type=false 

### log schema export/update ### 
#log4j.logger.org.hibernate.tool.hbm2ddl=info 

### log HQL parse trees 
#log4j.logger.org.hibernate.hql=debug 

### log cache activity ### 
#log4j.logger.org.hibernate.cache=info 

### log transaction activity 
#log4j.logger.org.hibernate.transaction=debug 

### Log all JDBC resource acquisition 
#log4j.logger.org.hibernate.jdbc=debug 

### enable the following line if you want to track down connection ### 
### leakages when using DriverManagerConnectionProvider ### 
#log4j.logger.org.hibernate.connection.DriverManagerConnectionProvider=trace 

log4j.appender.SQL_APPENDER=org.apache.log4j.RollingFileAppender 
log4j.appender.SQL_APPENDER.File=c\:/EC_sql.log
log4j.appender.SQL_APPENDER.MaxFileSize=1000KB 
log4j.appender.SQL_APPENDER.MaxBackupIndex=62 
log4j.appender.SQL_APPENDER.layout=org.apache.log4j.PatternLayout 
log4j.appender.SQL_APPENDER.layout.ConversionPattern=[%d] %5p [%t] (%F:%L) - %m%n

Validate XML String against an XSD


JAXBContext jc = JAXBContext.newInstance(<<You class>>);
JAXBSource source = new JAXBSource(jc, <<your class object>>);
 
SchemaFactory sf = SchemaFactory.newInstance(XMLConstants.W3C_XML_SCHEMA_NS_URI); 
Schema schema = sf.newSchema(new File("<<your XSD>>")); 
 
Validator validator = schema.newValidator();
validator.setErrorHandler(new MyErrorHandler());
validator.validate(source);
 
 
import org.xml.sax.ErrorHandler;
import org.xml.sax.SAXException;
import org.xml.sax.SAXParseException;
 
public class MyErrorHandler implements ErrorHandler {
 
    public void warning(SAXParseException exception) throws SAXException {
        System.out.println("\nWARNING");
        exception.printStackTrace();
    }
 
    public void error(SAXParseException exception) throws SAXException {
        System.out.println("\nERROR");
        exception.printStackTrace();
    }
 
    public void fatalError(SAXParseException exception) throws SAXException {
        System.out.println("\nFATAL ERROR");
        exception.printStackTrace();
    }
 
}

Sunday, February 17, 2013

Connection Pool setting for JPA

Make sure that the following is set in the persistence.xml file

<!-- Important -->
<property name="hibernate.connection.provider_class"
  value="org.hibernate.connection.C3P0ConnectionProvider" />

<property name="hibernate.c3p0.max_size" value="100" />
<property name="hibernate.c3p0.min_size" value="0" />
<property name="hibernate.c3p0.acquire_increment" value="1" />
<property name="hibernate.c3p0.idle_test_period" value="300" />
<property name="hibernate.c3p0.max_statements" value="0" />
<property name="hibernate.c3p0.timeout" value="100" />


Ensure that the connection provider is set and all properties of connection pool are prefixed with "hibernate.

The easy way to verify is that you should see from the log files the following statements

INFO: Initializing c3p0-0.9.1 [built 16-January-2007 14:46:42; debug? true; trace: 10]
com.mchange.v2.c3p0.impl.AbstractPoolBackedDataSource getPoolManager
INFO: Initializing c3p0 pool... com.mchange.v2.c3p0.PoolBackedDataSource@f3a94e12 [     connectionPoolDataSource ->

Spring AOP cannot set value NULL

After hours of debugging I figured that indeed the issue was related to the various jar file configurations which was causing the trouble.

I have listed the ideal permutation which might help some one to fix this issue soon.

Ensure that the following are the appropriate versions atleast for SPRING related stuff.

  1. aopalliance-1.0.jar
  2. asm-3.1.jar
  3. asm-attrs-1.5.3.jar
  4. aspectjrt-1.6.5.jar
  5. aspectjtools-1.6.9.jar
  6. aspectjweaver-1.6.5.jar
  7. cglib-nodep-2.1_3.jar
  8. commons-logging-1.1.1.jar
  9. log4j-1.2.16.jar
  10. spring-aop-3.1.2.RELEASE.jar
  11. spring-asm-3.1.2.RELEASE.jar
  12. spring-aspects-3.1.2.RELEASE.jar
  13. spring-beans-3.1.2.RELEASE.jar
  14. spring-context-3.1.2.RELEASE.jar
  15. spring-core-3.1.2.RELEASE.jar
  16. spring-expression-3.1.2.RELEASE.jar
  17. spring-instrument-3.1.2.RELEASE.jar
  18. spring-tx-3.1.2.RELEASE.jar