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

pátek 7. ledna 2011

How to start HSQLDB with Maven

To start HSQLDB with maven you have to add HSQLDB dependancy to your pom.xml file.


<dependency>
 <groupId>org.hsqldb</groupId>
 <artifactId>hsqldb</artifactId>
 <version>2.0.0</version>
 <scope>runtime</scope>
 </dependency>

After this go to command line and start this command:


mvn exec:java -Dexec.mainClass=org.hsqldb.Server -D"exec.args=-database.0 file:target/data/caveat-emptor"


  • exec:java will start JVM
  • -D is telling that given parameter is directly send to JVM (it is not for MVN)
  • exec.mainClass will start main method from class org.hsqldb.Server
  • exec.args will send given arguments to main method of Server class

čtvrtek 6. ledna 2011

Where are Spring framework sample application's source codes

According to "Spring Security - Tutorial: Adding Security to Spring Petclinic" web page you can find source codes for Spring's sample applications at SVN:

https://src.springframework.org/svn/spring-samples

Your find there following samples:
  • spring-samples/configuration-basic
  • spring-samples/jpetstore
  • spring-samples/mvc-ajax
  • spring-samples/mvc-basic
  • spring-samples/mvc-showcase
  • spring-samples/petcare
  • spring-samples/petclinic
  • spring-samples/petclinic-groovy
  • spring-samples/task-basic
  • spring-samples/templates
  • spring-samples/travel
  • spring-samples/webflow-primefaces-showcase
  • spring-samples/webflow-richfaces-showcase

středa 8. prosince 2010

How to add custom repository to Maven with M2Eclipse plugin

In your users settings.xml file create new profile with repositories tag filled and default activation enabled. After this you will get new repository in command line maven and in your Eclipse environment.


<settings xmlns="http://maven.apache.org/SETTINGS/1.0.0"
 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
 xsi:schemaLocation="http://maven.apache.org/SETTINGS/1.0.0 http://maven.apache.org/xsd/settings-1.0.0.xsd">
 <profiles>
  <profile>
   <id>java.net-repository</id>
   <activation>
    <activebydefault>true</activeByDefault>
   </activation>
   <repositories>
    <repository>
     <id>maven2-repository.dev.java.net</id>
     <name>Java.net Repository for Maven</name>
     <url>http://download.java.net/maven/2/</url>
     <layout>default</layout>
    </repository>
   </repositories>
  </profile>
 </profiles>
</settings>

Note: There is difference between <activebydefault>true</activeByDefault> and <activebydefault/>.

čtvrtek 3. prosince 2009

The first HIT!

More later...