Java Reference
In-Depth Information
4.1. Treating POJOs like POGOs
POGOs have more capabilities than POJOs. For example, all POGOs have a map-based
constructor that's very convenient for setting properties. The interesting thing is that even
if a class is written in Java, many of the same conveniences apply as long as it's accessed
from Groovy.
Consider a simple POJO representing a person, possibly created as part of a domain model
in Java, shown in the next listing. To keep it simple I'll only include an ID and a name.
I'll put in a toString override as well but won't include the inevitable equals and
hashCode overrides.
Listing 4.1. A simple POJO representing a person
public class Person {
private int id;
private String name;
public Person() {}
public Person(int id, String name) {
this.id = id;
this.name = name;
}
public void setId(int id) { this.id = id; }
public int getId() { return id; }
public void setName(String name) { this.name = name; }
public String getName() { return name; }
@Override
public String toString() {
return "Person [id=" + id + ", name=" + name + "]";
}
}
Any typical Java persistence layer has dozens of classes just like this, which map to rela-
tional database tables ( figure 4.2 ) .
Search WWH ::




Custom Search