From schema to code
To demonstrate what kind of code is generated we use a snippet of the example database, that is used for the JUnit test environment.
The picture shows the entity's Person, Origin and KCountry. The generator will recognize the relation
between them and build java code accordingly.
To demonstrate the java code we use the Person entity.
According to the ER diagram the entity has 3 attributes(id, first_name and last_name).
It has a one to one relation with origin and an indexed-many-to-many relation with
KCountry(with date as index).
Furthermore there are two one-to-many relations with itself(father_id and mother_id).
The attributes are simple types in java, that are resolved from the database types.
A one-to-one relation consists on both sides as an entity reference. The one-to-many relations
are Sets on one side and entity references on the other. An indexed-many-to-many relation is a Map.
public class Person extends blomo.entity.BLoMoEntity {
private Integer id;
private String firstName;
private String lastName;
private model.Origin origin;
private Map dateCountries = new Hashtable();
private model.Person father;
private Set childrenAsFather = new HashSet();
private model.Person mother;
private Set childrenAsMother = new HashSet();
Access to the declared fields is dependent on the Type. Simple types and entity's have getter- and setter-methods.
The id is a special case where the set method is beeing protected, it will be assigned automaticall through
hibernate during a save.
/**
* generated method
* @param firstName undefined
*/
public void setFirstName( String firstName ) {
this.firstName = firstName;
}
/**
* generated method
* @return undefined
*/
public String getFirstName() {
return firstName;
}
/**
* generated method
* @return undefined
*/
public model.Origin getOrigin() {
return origin;
}
/**
* generated method
* @param origin undefined
*/
public void setOrigin( model.Origin origin ) {
this.origin = origin;
}
Sets and Maps have protected getter- and setter-methods as well as add-, remove-, clear- and getReadOnly-methods.
The reason for the protected getter- and setter-methods is to build a bi-directional model. So that whenever
an item is added to the Set or Map the other side of the relation is updated correctly.
/**
* generated method
* @return undefined
*/
protected Set getChildrenAsFather() {
return childrenAsFather;
}
/**
* generated method
* @param childrenAsFather undefined
*/
protected void setChildrenAsFather( Set childrenAsFather ) {
this.childrenAsFather = childrenAsFather;
}
/**
* generated method
* @param childrenAsFather undefined
*/
public void addChildrenAsFather( Person childrenAsFather ) {
getChildrenAsFather().add(childrenAsFather);
}
/**
* generated method
* @param childrenAsFather undefined
*/
public void removeChildrenAsFather( Person childrenAsFather ) {
getChildrenAsFather().remove(childrenAsFather);
}
/**
* generated method
*/
public void clearChildrenAsFather() {
Person array[] = new Person[getChildrenAsFather().size()];
getChildrenAsFather().toArray(array);
for ( int i=0; i getChildrenAsFatherReadOnly() {
return Collections.unmodifiableSet(childrenAsFather);
}
/**
* generated method
* @return undefined
*/
protected Map getDateCountries() {
return dateCountries;
}
/**
* generated method
* @param dateCountries undefined
*/
protected void setDateCountries( Map dateCountries ) {
this.dateCountries = dateCountries;
}
/**
* generated method
* @param key undefined
* @param value undefined
*/
public void addDateCountries( Date key, model.KCountry value ) {
getDateCountries().put(key,value);
}
/**
* generated method
* @param key undefined
*/
public void removeDateCountries( Date key ) {
model.KCountry value = getDateCountries().get(key);
getDateCountries().remove(key);
if ( value != null )
value.getDatePersons().remove(key);
}
/**
* generated method
*/
public void clearDateCountries() {
Date array[] = new Date[getDateCountries().keySet().size()];
getDateCountries().keySet().toArray(array);
for ( int i=0; i getDateCountriesReadOnly() {
return Collections.unmodifiableMap(dateCountries);
}
To make sure two entity's are equal if their id and class match, the hashcode and equals methods are
overridden. Also the method getEntityId, required by the superclass, is added to retrieve the id property.
By using the method preDelete in combination with an on-delete-hibernate-listener some mapping problems of
hibernate are resolved.