Showing posts with label java. Show all posts
Showing posts with label java. Show all posts

Sunday, May 02, 2010

Groovy many-to-many relationships

Hoje finalmente desvendei um mistério que tava tirando meu sono: como mapear no Grails relações muitos-pra-muitos. Procurei por toda internet e não consegui encontrar uma solução que resolvesse essa questão. Então eis que hoje por acaso encontro no 'The Definite Guide to Grails' (o melhor livro do mundo) como fazê-lo na seção de mapeamento usando banco de dados legados.

É impressionantemente simples, esse é tipo de coisa que você pensa: como não tentei isso antes. É só declarar normalmente o map hasMany dos dois lados e escolher um como owner da relação, por exemplo a relação entre Compositor e Música ficaria assim:

class Compositor {
String nome
static hasMany = [musicas:Musica]
}

class Musica {
String nome
static hasMany = [compositores:Compositor]
static belongsTo = Compositor
}

Deixando o grails fazer o resto pra você (com o atributo scaffold nos respectivos controllers) você tem seu crud com a relação muitos-pra-muitos pronto pra usar. ;)

Tuesday, April 24, 2007

Ruby in 20 minutes

I was looking around the Ruby website and I found an interesting Ruby tutorial: the Ruby in Twenty Minutes. It shows cool features of that awesome programming language. It's really incredible what we can do with Ruby and with a few lines of code. For example, let's compare the Java code with Ruby code:

String[] anArray = new String[]{ "1", "2", "3", "4", "5" };
for(String elem : anArray) {
System.out.println("Number: " + elem);
}

That's a simple iteration over a String array written in Java5 or Java6. In Ruby the same thing could be written like this:

an_array = ["1", "2", "3", "4", "5"]
an_array.each {|num| puts "Number #{num}"}

That's really more simple than Java code, but it's not complex, the code between the curly braces is executed for each element in the "an_array" and the "|num|" is a reference to each element of the array. In Groovy, a Ruby-based scripting language, the code between curly braces is also known as "Closure":

an_array = ["1", "2", "3", "4", "5"]
an_array.each({ num -> println "Number ${num}" })

Closures are simple structures that can carry an implementation, it's like inner classes, but Closures are more simple and clear, see Closures for the Java Programming Language for a complete description of Closures and its adoption by Java.

Thursday, April 19, 2007

Ubuntu Feisty Fawn

It was released today Ubuntu Linux 7.04 a.k.a. Feisty Fawn which can be downloaded from http://mirrors.cat.pdx.edu/ubuntu-iso/feisty/. There are several enhancements, which you can see at Ubuntu Site. The great surprise was the inclusion of some Java Packages to Ubuntu multiverse repository. The list of sotwares includes:

  • GlassFish J2EE Container;
  • Java SE (JDK 6);
  • Derby-based Java DB 10.2;
  • NetBeans IDE 5.5.

That's one more step of the partnership between Sun Microsystems and Canonical. I hope more good surprises like that.