Wednesday, June 29, 2005

Selecting specific objects on a Hibernate Query Join

I ran into a problem when I was trying to select specific columns from multiple tables in a join. I was having trouble casting the returned fields into an object type.

I thought that I would share the two different solutions that I came up with when I found in the Hibernate documentation.

Let me know if you have questions...since I just found this, I may or may not be able to answer it, but I'll try.

Queries may return multiple objects and/or properties as an array of type Object[]

select mother, offspr, mate
from eg.DomesticCat as mother
inner join mother.mate as mate
left outer join mother.kittens as offspr


When this is done, you have to iterate over the Object array and cast it back into the object type that each one is and call the constructor of
the item you are using.

for (Iterator lIterator=lFamilyList.iterator();
lIterator.hasNext();)
{
Object[] lObjectArray = (Object[]) lIterator.next();
Family lFamily = new Family((String) lObjectArray [0],
(String) lObjectArray [1],
(String) lObjectArray [2]);
System.out.println("Mother: " + lFamily.getMother());
System.out.println("Mate: " + lFamily.getMate());
System.out.println("Offspr: " + lFamily.getOffspr());
}

or as an actual typesafe Java object

select new Family(mother, mate, offspr)
from eg.DomesticCat as mother
join mother.mate as mate
left join mother.kittens as offspr


When this is done, you just have to cast it back to it's appropriate class when you iterate.

   for (Iterator lIterator = lFamilyList.iterator();
lIterator.hasNext();)
{
Family lFamily = (Family) lIterator.next();
System.out.println("Mother: " + lFamily.getMother());
System.out.println("Mate: " + lFamily.getMate());
System.out.println("Offspr: " + lFamily.getOffspr());
}

assuming that the class Family has an appropriate constructor.

Wednesday, June 08, 2005

EL OR LA COMPUTER

A Spanish teacher was explaining to her class that in Spanish, unlike English, nouns are designated as either masculine or feminine.

"House" for instance, is feminine: "la casa."

"Pencil," however, is masculine: "el lapiz."

A student asked, "What gender is 'computer'?"

Instead of giving the answer, the teacher split the class into two groups, male and female, and asked them to decide for themselves whether "computer" should be a masculine or a feminine noun.

Each group was asked to give four reasons for its recommendation.

The men's group decided that "computer" should definitely be of the feminine gender ("la computadora"), because:

  1. No one but their creator understands their internal logic

  2. The native language they use to communicate with "other computers" is incomprehensible to everyone else

  3. Even the smallest mistakes are stored in long term memory for retrieval later

  4. As soon as you make a commitment to one, you find yourself being changed by them "for the better".


The women's group, however, concluded that computers should be Masculine ("el computador"), because:

  1. In order to do anything with them, you have to first get their attention, and then turn them on

  2. They have a lot of data but still can't think for themselves

  3. They are supposed to help you solve problems, but half the time they ARE the problem

  4. As soon as you commit to one, you realize that if you had waited a little longer, you could have gotten a better model.


The women won.