Curious facts about Hibernate

Hibernate is a very useful framework to do Object-Relational mapping in Java. It saves a lot of time, gives the programmer a lot of flexibility and allows you to do amazing things like mapping object inheritance.

Nevertheless, it has some “strange” behaviours that, in my opinion, seem a bit courterintuitive. I’m going to tell you about the way Hibernate translates object relations into joins.

Let’s imagine we have the class Person, with attributes id and name, and the class Car, with attributes id and brand. Class Person has a one-to-one relationship with Car named car, and there’ll be persons who have a car, and persons who haven’t.
If we want to query about the cars each people have, the “intuitive” query would be this:

select p.name, p.car from Person p

And the “intuitive” result would be: (John, Car instance #1); (Peter, null); (Mary, Car instance #2), isn’t it?

It isn’t. The real thing is that Hibernate translates the HQL sencence to a SQL one that selects from Person and does an (inner) join with Car, in order to instantiate a Car with all its attribute values set up. As a consequence of that inner join, all the people having a null car are missing from the result.

Intuitive solution: force the outer join by hand

select p.name, c.id, c.brand from Person p left join p.car c

Non intuitive (but valid) solution: use the required attributes directly

select p.name, p.car.id, p.car.brand from Person p

In this last query, I would expect a NullPointerException when the person has no car, since if there is no car, there is no attribute to evaluate, but the exception doesn’t get thrown, the problem doesn’t arise and you get the work done.

Quite strange, isn’t it?

My own flavour of network config automatic choosing

Until a couple of days ago, I used to use Gnome network-admin’s ability of having multiple network profiles to configure my wifi card at work and at home. But yesterday, the program went crazy and started to crash without any logical reason and I decided to shift to another solution: logical network interface mapping.

Interface mapping is a feature of ifupdown (/etc/network/interfaces). You can define some logical interfaces and call a script in order to choose which one will be mapped to a physical interface. The /etc/network/interfaces could be like this one (being HomeNET and WorkNET two existing ESSID network identifiers):

auto eth1
mapping eth1
script /root/WIFI/wldetect.sh
map HomeNET HomeNET
map WorkNET WorkNET

iface HomeNET inet static
address 192.168.1.2
netmask 255.255.255.0
gateway 192.168.1.1
wireless-essid HomeNET
wireless-key s:mysecretpass1 open
dns-nameservers 127.0.0.1 192.168.1.1

iface WorkNET inet dhcp
wireless-essid WorkNET
wireless-key s:mysecretpass2 open
dns-nameservers 127.0.0.1 192.168.100.1

I’m using resolvconf to activate the dns-nameservers directive in the interfaces file, and dnsmasq to take care of some dns issues with a couple of VPNs I use. That explains the first 127.0.0.1 entry.

The script /root/WIFI/wldetect.sh lists the available networks and chooses one of them being in a whitelist (HomeNET|WorkNET):

#!/bin/sh

# Config
WL_IFACE=`iwconfig 2>/dev/null | { read A _; echo $A; };`

# Reset the interface
ifconfig $WL_IFACE down
ifconfig $WL_IFACE 0.0.0.0
ifconfig $WL_IFACE up

# Search networks
NETWORKS=`iwlist $WL_IFACE scanning | grep ESSID | sed -e ‘s/.*”(.*)”/1/’`

# Bring down the interface
ifconfig $WL_IFACE down

# Select preferred networks
for NET in $NETWORKS
do
case $NET in
HomeNEt|WorkNET)
echo $NET;
exit 0;
;;
esac
done
exit 1;

And that’s all! I hope that this configuration could be helpful for someone.

Logitech QuickCam Express (USB ID 046d:092f) on Ubuntu Edgy

I’ve just bought the webcam in the title of this post at Alcampo and had a hard time trying to set it working.

In the pkg-spca5xx-devel Debian mailing list, Dmitry Semyonov says that this camera model is supported by the spca5xx driver, but its ID isn’t added to the driver source code yet. He’s published a patch, and I tried to apply it, but the patch utility complained. That was because the tabs would have been replaced with spaces or something. Then I discovered the “-l” option, which solved my problem and I could finally apply the patch.

At help.ubuntu.com I found detailed instructions about how to build the spca5xx source code, so, finally I had the work done.

I’ve written a recipe to perform all the steps. At first, copy the patch into the file /tmp/qcexpress.patch. Next, execute these instructions:

apt-get install spca5xx-source
cd /usr/src
tar jxvf spca5xx-source.tar.bz2
cd modules
patch -lp1 < /tmp/qcexpress.patch
cd spca5xx
make
make install

That’s all!
And, at the end, a living proof: MSN video chat using Kopete 🙂
MSN video chat with Kopete

Cryptographic Filesystem

Yesterday’ve been playing around with CFS in my Ubuntu. I’ve never tried a cryptographic filesystem before, altough I knew there were many flavours laying out there.

CFS has two main advantages, as far as I know:

  • It doesn’t require any special kernel patch (at least, not for Ubuntu), because it uses NFS to do loopback mounting of crypted directories.
  • It uses directly the underlying filesystem, avoiding the need of creating fs images to mount by loopback. Crypted directories and files are mapped to normal directories and files with its name and contents crypted.

The package provides some utilities:

  • cmkdir: Creates and initializes a crypted directory on the host filesystem.
  • cattach: Attaches a crypted directory, making it available (as cleartext) under /crypt/*.
  • cdetach: Dettaches a previously mounted crypted directory.

I’ve tried CFS successfully, and added it to my list of useful tools. It’s clean, easy and powerful. 🙂

Limited lifetime scripts

While helping Xavi with a small task (batch converting ODT documents to PDF via a web form), I’ve remembered a little recipe I’ve written some time ago.

It’s a way to force some script to die after a defined timeout. So, you can perform a command by ssh inside a script or perform any blocking task you want, and it will always return.

The only thing you have to do is to put these single 3 lines of code at the start of your bash script:

SECURITY_TIMEOUT=60
CMD_PID="$$";
{ sleep $SECURITY_TIMEOUT; kill -9 $CMD_PID; } 2>/dev/null &
Enjoy it!

SMTP and Cisco PIX firewall

Some months ago, I configured a complete mail gateway and other services for a client. As for almost every deployment, there were things that had to be investigated and one always learn something new.

One of the most surprising “curiosities” I found was about SMTP. When I telneted the SMTP gateway at port 25 from inside the intranet or localhost, the usual welcome message was displayed. But when I did the same from the internet, I received this weird welcome string:

220 **************************************

And when I tried to issue an ESMTP command (like EHLO), the server said that it wasn’t supported. What the hell??!! It can’t be! Someone or something is changing my packets!

After doing some searching at Google, I discovered who was the little guilty: a Cisco PIX firewall configured with the “fixup protocol smtp 25” option turned on. That was preventing internet users from authenticating and using TLS.

Thank you debian-administration.org guys!

Linex and Iberia

Past week I’ve travelled to Valencia to take a rest and see the city. A big city, by the way, altough people’s timetable was a bit “strange”, but that’s a different story…

One of the most surprising things happened in the flight: in the plane there were glass coasters, napkins and even headrest covers with the advertisment “sé legal, copia GNU/Linex” (“be legal, copy GNU/Linex”).

It seems like Junta de Extremadura has made an agreement with Iberia to show Linex advertisments and offer Linex CDs and leaflets in some regional flights. What a good thing!

linex.jpeg

Summer practices on PhpReport

PhpReport is a GPL web application that tracks tasks performed by a company staff or workgroup and allows administrators to launch queries and get statistics about the tasks and time spent on each one. This give company managers the proper information to do cost analysis and decission making. PhpReport is used internally by Igalia staff on daily basis since December 2003.

This summer Igalia has planned a “company practices” hiring to improve PhpReport. Jesús Pérez is the student selected, and he will make use of XP Tracker to manage his tasks. At this link you can follow his progress.

The improvements assigned to this development iteration satisfy new functional needs and publishing & community issues. Some of those improvements are:

  • Internationalization.
  • Migration of existing OpenOffice.org spreadsheets into PhpReport, to make result extraction and business decission making easier.
  • XML import module rewriting, to improve mantainability and extensibility of the application.
  • Administration and usage documentation.
  • Package improvements.

As a result of this iteration, a new release is planned for the end of the summer. Stay tuned!

NeoSudoku 1.2.1 GPL released!

I’ve worked a little more in NeoSudoku and at last it can:

  • Solve human targeted sudokus using the limited resources that the cell phone has.
  • Learn new sudokus (new board configurations input by hand)
  • It can’t save new sudokus to persistent storage, but it will be the next feature to come.

The rest of the features can be found on my past post. You can download it from here:

Enjoy it!