Hi, i search a neat solution for the following requirement.
Code:
@Entity
UserIdentity {
...
}
@Entity
TrainerIdentity {
@ManyToOne(fetch = FetchType.LAZY, optional = false)
UserIdentity getUserIdentity()
...
}
@Entity
SportsTeam {
@ManyToMany(fetch = FetchType.LAZY)
Set<TrainerIdentity> getTrainerIdentity()
....
Set<UserIdentity> getTrainerUserIdentity() // <----- how?
}
the simplest solution:
Code:
SportsTeam {
...
@Transient
Set<UserIdentity> getTrainerUserIdentity() {
Set<UserIdentity> result = new HashSet<UserIdentity>();
Set<TrainerIdentity> holder = getTrainerIdentity();
for (TrainerIdentity trainerIdentity : holder) {
result.add(trainerIdentity.getUserIdentity());
}
return result;
}
}
but this solution is very ugly: 1.) unnecessary data loading 2.) breaks persistence chain, therefore i get json serialization troubles with lazy UserIdentity childs!
what is the best solution for this use case?
i work with hibernate 3.6.10, JPA 2.0, use EntityManager and EclipseLink for typesafe criteria queries.
thanks!