Bradox wrote:
Quote:
eager fetching is impossible
You should then reattach the object graph (to a new session) when you object is ready to be viewed. I even wrote some code a while ago that looks at an object , see if it has null property and reattaches if so, something like:
Code:
protected Object relinkEntity(Object entity) {
...
Class clazz = entity.getClass();
Field fields[] = clazz.getDeclaredFields();
if (log.isTraceEnabled())log.trace(Arrays.toString(fields));
boolean mustFetch = false;
for (Field f : fields) {
...
if (mappedClasses.contains(f.getType())) {
// We have a mapped entity, lets check it.
Object fieldValue = null;
try {
fieldValue = PropertyUtils.getSimpleProperty(entity, f.getName());
} catch (Exception e) {
log.debug("could not get value for field:" + f);
}
if (fieldValue != null) {
Object newEntity = relinkEntity(fieldValue);
// We don't have the same one, it has been replaced by a
// hibernate proxy.
if (newEntity != fieldValue) {
try {
PropertyUtils.setSimpleProperty(entity, f.getName(), newEntity);
} catch (Exception e) {
log.debug("could not set field:" + f + " to value" + newEntity);
...
if (mustFetch) {
Serializable key = HibernateUtils.getIdentifierValue(entity.getClass(), entity);
if (key != null) {
Object newEntity = loadEntity(entity, key);
return newEntity;
}else{
log.debug("key was null for:"+entity);
}
...
* rate if this helps *[/code]