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

Manage a composite primary key with an incremental id using JPA and Oracle


The regular way of doing this is to  have a Composite Key Class and mapping the field to use oracle sequence. In most of the scenarios this will fail.
The alternative is to 
1. Create the Composite Primary key as a regular java POJO with no @Column mappings.
2.Create/Override the equals() and hashCode() methods in this class.
3. Now go to the main POJO and instead of defining this composite key as @EmbeddedId, declare the composite primary key attributes your entity, associate the ID class with the entity and define the ID generation strategy,
o   @Id
o   @Column mappings
o   and for the sequence number field alone add like the following
  @Column(name="<>")   @GeneratedValue(strategy=GenerationType.AUTO, generator="<>")   @SequenceGenerator(name="<>", sequenceName="<>") 4.When you create the records, you don’t need to set this field at all. 5. DB will generate the sequence for you. 6.It’s a known defect from JPA that composite keys cannot accept sequence numbers  

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();
    }
 
}