Tuesday, October 9, 2012

ClassNotFoundException: org.hibernate.hql.ast.HqlToken

When using Hibernate to read from a Database using Weblogic 10 and Springframework 2.5.6, I encountered the following error:

ClassNotFoundException: org.hibernate.hql.ast.HqlToken

The reason for this is that it by default will use Weblogic's default classloader to load the antlr jar file and doesn't use the version included within your .war file.

To solve this you need to wrap your .war in an ear file and within the META-INF/weblogic-application.xml file within you .ear file include the following:
 <prefer-application-packages>
    <package-name>antlr.*</package>
 </prefer-application-packages>
 

This should resolve this issue.


Sunday, August 26, 2012

SQL String functions

Some STRING functions in SQL:

select RIGHT(column, chars) from table;
select SUBSTRING_INDEX(column, char, which char (i.e, 1,2...)) from table;
select REVERSE(string);
select LTRIM(string);
select RTRIM(string);
select LENGTH(string);

Monday, July 30, 2012

Java Pitfalls Book


This book "Java™ Puzzlers: Traps, Pitfalls, and Corner Cases" gives insight into some Java "fails" that one needs to watch out for when developing.

It explains what is wrong with this piece of code (It doesn't print out AB as one would expect):

public class PrintingSomeLetters {
  public static void main(String[] args) {
    System.out.println('A' + 'B');
  }
}

Friday, June 22, 2012

Design Patterns Book

Tuesday, May 15, 2012

Java 5 autoboxing fail on equality check

  Double d1 = 25d;
  Double d2 = 25d;

  if (d1 == d2) {
            System.out.println("equal (==)");
  } else if (d1.equals(d2)) {
            System.out.println("equal (using equals() method)");
  }

The output is:
equal (using equals() method)

Wednesday, May 2, 2012

Reverse search for a previous command on Linux

Use CTRL-r to reverse search through a history of your previous commands executed in Linux e.g.
$(reverse-i-search)`':

Type in the text for a command you are searching for e.g. 'cat'
$(reverse-i-search)`cat': cat test.txt

In this case the last command executed with the substring 'cat' is 'cat test.txt'.

To exit the reverse search just use CTRL-g.

Thursday, April 26, 2012

Using SED to Append or Prepend characters

You can use sed to Append/Prepend characters to a String e.g.

We have a file (test.txt) below and we would like to Prepend each capital letter with a space:
$ cat test.txt
Hello.ThisIsATestFile.

$ sed "s/[A-Z]/ &/g" test.txt
Hello. This Is A Test File.

sed makes use of a special character "&" which implies the pattern found.

Similarly we could Append a sentence after the "Hello." string:
$ sed "s/Hello./&HowAreYou?/g" test.txt 
Hello.HowAreYou?ThisIsATestFile.

Thursday, March 8, 2012

Configuring maven to autogenerate Serializable JAXB classes

 First you need to create a bindings file:

<jxb:bindings version="1.0"
xmlns:jxb="http://java.sun.com/xml/ns/jaxb"
xmlns:xs="http://www.w3.org/2001/XMLSchema">

<jxb:globalBindings>
  <jxb:serializable uid="1"/>
</jxb:globalBindings>

</jxb:bindings>
Save this file under "src/main/resources/" of your maven module. You can name it whatever you want e.g. jaxb-bindings.xjb.

<plugin>
  <groupId>org.jvnet.jaxb2.maven2</groupId>
  <artifactId>maven-jaxb2-plugin</artifactId>
  <executions>
    <execution>
      <id>Request</id>
      <goals>
      <goal>generate</goal>
      </goals>
      <configuration>
      <schemaDirectory>src/main/resources/xsd</schemaDirectory>
      <schemaIncludes>
      <include>*/*.xsd</include>
      </schemaIncludes>
      <generatePackage>mygeneratedfiles</generatePackage>
      <generateDirectory>src/main/java</generateDirectory>
      <bindingDirectory>src/main/resources</bindingDirectory>
      <bindingIncludes>
      <bindingInclude>jaxb-bindings.xjb</bindingInclude>
      </bindingIncludes>
      </configuration>
    </execution>
  </executions>
</plugin>