středa 27. června 2012

java.io.NotSerializableException: How to debug Java serialization

When you got NotSerializableException and don't know where is problem, there is one solution - switch Java serialization to debug mode.

  • Start application with parameter extendedDebugInfo
-Dsun.io.serialization.extendedDebugInfo=true
  • Run your serialization routines to reproduce exceptions
  • You will get usual exception with debug infromation containing stack of serialization
WARNING: Cannot serialize session attribute com.vaadin.terminal.gwt.server.WebApplicationContext for session 088634D700DB384445C8C941855BD7A9
java.io.NotSerializableException: xx.vaadin.security.SecuredValue
 - field (class "xx.base.view.BaseApplication", name: "goliId", type: "class xx.vaadin.security.SecuredValue")
 - object (class "xx.view.paymentorder.ServletPaymentOrderApplication", xx.view.paymentorder.ServletPaymentOrderApplication@1837b90c)
 - custom writeObject data (class "java.util.HashMap")
 - object (class "java.util.HashMap", {xx.view.paymentorder.ServletPaymentOrderApplication@1837b90c=com.vaadin.terminal.gwt.server.CommunicationManager@3cf94dad})
 - field (class "com.vaadin.terminal.gwt.server.AbstractWebApplicationContext", name: "applicationToAjaxAppMgrMap", type: "class java.util.HashMap")
 - root object (class "com.vaadin.terminal.gwt.server.WebApplicationContext", com.vaadin.terminal.gwt.server.WebApplicationContext@28cc5c6c)
 at java.io.ObjectOutputStream.writeObject0(ObjectOutputStream.java:1161)
 at java.io.ObjectOutputStream.defaultWriteFields(ObjectOutputStream.java:1518)
 at java.io.ObjectOutputStream.writeSerialData(ObjectOutputStream.java:1483)
 at java.io.ObjectOutputStream.writeOrdinaryObject(ObjectOutputStream.java:1400)
 at java.io.ObjectOutputStream.writeObject0(ObjectOutputStream.java:1158)
 at java.io.ObjectOutputStream.writeObject(ObjectOutputStream.java:330)
 at java.util.HashMap.writeObject(HashMap.java:1000)
 at sun.reflect.GeneratedMethodAccessor35.invoke(Unknown Source)
 at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
 at java.lang.reflect.Method.invoke(Method.java:597)
 at java.io.ObjectStreamClass.invokeWriteObject(ObjectStreamClass.java:940)
 at java.io.ObjectOutputStream.writeSerialData(ObjectOutputStream.java:1469)
 at java.io.ObjectOutputStream.writeOrdinaryObject(ObjectOutputStream.java:1400)
 at java.io.ObjectOutputStream.writeObject0(ObjectOutputStream.java:1158)
 at java.io.ObjectOutputStream.defaultWriteFields(ObjectOutputStream.java:1518)
 at java.io.ObjectOutputStream.writeSerialData(ObjectOutputStream.java:1483)
 at java.io.ObjectOutputStream.writeOrdinaryObject(ObjectOutputStream.java:1400)
 at java.io.ObjectOutputStream.writeObject0(ObjectOutputStream.java:1158)
 at java.io.ObjectOutputStream.writeObject(ObjectOutputStream.java:330)
 at org.apache.catalina.session.StandardSession.writeObject(StandardSession.java:1671)
 at org.apache.catalina.session.StandardSession.writeObjectData(StandardSession.java:1077)
 at org.apache.catalina.session.StandardManager.doUnload(StandardManager.java:411)
 at org.apache.catalina.session.StandardManager.unload(StandardManager.java:353)
 at org.apache.catalina.session.StandardManager.stopInternal(StandardManager.java:497)
 at org.apache.catalina.util.LifecycleBase.stop(LifecycleBase.java:232)
 at org.apache.catalina.core.StandardContext.stopInternal(StandardContext.java:5462)
 at org.apache.catalina.util.LifecycleBase.stop(LifecycleBase.java:232)
 at org.apache.catalina.core.ContainerBase$StopChild.call(ContainerBase.java:1581)
 at org.apache.catalina.core.ContainerBase$StopChild.call(ContainerBase.java:1571)
 at java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:303)
 at java.util.concurrent.FutureTask.run(FutureTask.java:138)
 at java.util.concurrent.ThreadPoolExecutor$Worker.runTask(ThreadPoolExecutor.java:886)
 at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:908)
 at java.lang.Thread.run(Thread.java:662)

středa 4. dubna 2012

How to convert HQL to SQL in Hibernate

If you want to get native SQL from HQL in Hibernate you can use following snippet.

final Query query = sessionFactory.getCurrentSession().createQuery(hql).setParameter(paramName, paramValue);

final QueryTranslatorFactory ast = new ASTQueryTranslatorFactory();
final QueryTranslatorImpl newQueryTranslator = (QueryTranslatorImpl) ast.createQueryTranslator(queryId, query.getQueryString(), Collections.EMPTY_MAP, (SessionFactoryImplementor) sessionFactory);
newQueryTranslator.compile(null, false);
sql = newQueryTranslator.getSQLString();

if (youWantToHaveParametersWithTheirNamesNotWithQuestionMarks) {
 final List parameterSpecifications = newQueryTranslator.getCollectedParameterSpecifications();
 if (!CollectionUtils.isEmpty(parameterSpecifications)) {
  for (NamedParameterSpecification parameter : parameterSpecifications) {
   sql = StringUtils.replaceOnce(sql, "?", ":" + parameter.getName());
  }
 }
}