Search code examples
javahibernatedictionary

Hibernate why are my objects becoming detached?


Map<String, Type> myMap = new HashMap<String, Type>();

Query query = session.createQuery("" +
        "SELECT a " +
        "FROM Articulo a ");
List<Object> list = query.list();

// Add all query elements to the hashmap
for (Object entrada : lista) {
    myMap.put(entrada.getKey(), (Type) entrada);
// Type someOtherObject = entrada.getReferencedProperty();
}

Type someObject = (Type) myMap.get("someValidKey").getReferencedProperty(); //normal getter

Here someObject comes out null and the object is detached. However if I uncomment the someOtherObject line. relationship is fetched correctly.

What is going on?


Solution

  • I found the problem. Deeper in my code, I had a call to session.clear(). That was causing all my entities to be detached.