Friday, July 1, 2011

Hibernate "import.sql" Encoding when Running Unit Tests in Maven

Ok, the title is quite horrible. The story goes like this: We have decided to use HSQLDB in memory database for our unit tests. It's just a matter of JDBC configuration. And when you tell hibernate to create your database (, it looks for import.sql file that runs after database is created. If you have it, it can populate your database.
But if you use different than default platform encoding in your sql file, you may have troubles.

You must then set your encoding (-Dfile.encoding=UTF8). Sounds easy, but setting the encoding for Maven does not help. The trick is that maven-surefire-plugin runs in separate JVM.

So what I needed is just one more line:


Friday, February 25, 2011

XmlGregorianCalendar, what you get isn't what you want

Question: What you get when you do such thing?

variable calendar is an instance of XMLGregorianCalendar (year=2011,month=2,day=1, hour, minute and seconds are not set).

Calendar result = Calendar.getInstance();
result.set(calendar.getYear(), calendar.getMonth() - 1, calendar.getDay(), calendar.getHour(), calendar.getMinute(),calendar.getSecond());
result.set(Calendar.MILLISECOND, calendar.getMillisecond());

You might be thinking that you will get:

Month=1 (February)
Hour, minute, seconds = 0

Be aware of doing this! While some field in gregorian calendar is not set then it's value is "-2147483648"

So, If you do such a conversion then you endup in 247120 years B.C.E ;-)


use method:

Implementation of this method:

if(month != -2147483648)
gregoriancalendar.set(2, month - 1);
if(day != -2147483648)
gregoriancalendar.set(5, day);
if(hour != -2147483648)
gregoriancalendar.set(11, hour);
if(minute != -2147483648)
gregoriancalendar.set(12, minute);
if(second != -2147483648)
gregoriancalendar.set(13, second);
if(fractionalSecond != null)
gregoriancalendar.set(14, getMillisecond());
return gregoriancalendar;

It's a kind of magic :-)

Thursday, February 17, 2011

JAXB, Equals and hashCode methods

I have no need to have a equals and hashCode methods on generated java class yet but I need it now.

It is really simple. You have to use a specific jaxb plugin (in case of Maven) and plugins for this plugin ;-)

- Maven2 plugin: org.jvnet.jaxb2.maven2:maven-jaxb2-plugin
- configuration: -XtoString, -Xequals, -XhashCode
- plugin for Maven2 plugin: org.jvnet.jaxb2_commons:jaxb2-basics

While java classes are being generated this plugin hooks at the process and add equals and hashCode methods. My pom definition:



That's it!

Tuesday, February 15, 2011

JAXB, XJC and unmappable chars

Say we have a XSD contains documentation with special characters:

<xs:documentation>Tady je použita čeština se vším všudy<xs:documentation>

Once you setup maven build and "jaxb2-maven-plugin", goal "xjc" and let JAXB to generate Java classes according to your schema you may endup with following error:

unmappable character for encoding UTF-8

Well, you have schema in UTF8 already, you have setup maven build to use UTF8, so why it says that some characters are not in UTF8?

I've found solution described here.


com.sun.codemodel.writer.CodeWriter#openSource uses the OutputStreamWriter encoding to set the CharsetEncoder to use :

{ OutputStreamWriter bw = new OutputStreamWriter(openBinary(pkg,fileName)); (...) CharsetEncoder encoder = EncoderFactory.createEncoder(bw.getEncoding()); }

It SHOULD instead build a CharsetEncoder based on the user-requested encoding (may fallback to default platform encoding) and THEN create the OutputStreamWriter with this encoder...

{ CharsetEncoder encoder = EncoderFactory.createEncoder( getUserDefinedEncoding() ); OutputStreamWriter bw = new OutputStreamWriter(openBinary(pkg,fileName), encoder); }


You have to setup build to use native encoding for your OS.

I've added profile (for mac):