Hibernate version: 3.1.2
Mapping documents:
Code:
<list name="movieRolePerson" table="movie_roleperson">
<cache usage="read-write" />
<key column="movieid"/>
<index column="index"/>
<composite-element class="FilmRolePerson">
<parent name="movie"/>
<property name="role" column="role" type="string"/>
<many-to-one name="person" class="Person"
column="personid" fetch="select"/>
</composite-element>
</list>
This is a classic n-n join table with attributes.
For examples : Robert work for a movie A as an actor, and he work for a movie B as an executive producer.
Now for a movie, i wont all the actors, or i wont to iter on every role and every person.
Code:
public Map<Role,List<Person>> getAllPerson() {
Map<Role,List<Person>> map = new HashMap<Role,List<Person>>();
Iterator<MovieRolePerson> iter = this.getMovieRolePerson().iterator();
MovieRolePerson frp;
while(iter.hasNext()){
frp = iter.next();
if(! map.containsKey(frp.getRole())){
map.put(frp.getRole(), new ArrayList<Person>());
}
map.get(frp.getRole()).add(frp.getPerson());
}
return map;
}
It works very nice if i don't use lazy loading, and I think it's better for the database than first asking for distinct role, and after asking for person with that role. The problem is the lazy loading. The lazy stuff is done in a proxy, with cglib, but my method is in the proxy, it can't proxied getter, so the
Code:
this.getMovieRolePerson()
return an empty list.
The doc talk about proxy generation :
Tuplizers but don't say how, there's a big TODO for later.
Is it stupid to do the work in the POJO, is it better to use nice HQL query in the DAO with explicit join? Is it easy to explain to the proxi that I wont to fetch this lazy list?