Java Reference
In-Depth Information
Listing A-11. Accessing a Hibernate Session in Map Mode
package sample.map;
import java.util.*;
import org.hibernate.EntityMode;
import org.hibernate.*;
import org.hibernate.cfg.Configuration;
public class AccessAsMap {
private static final SessionFactory sessionFactory = new Configuration()
.configure().buildSessionFactory();
public static void main(String[] args) throws Exception {
System.out.println("Preparing the Session objects");
Session session = sessionFactory.openSession();
Session mapSession = session.getSession(EntityMode.MAP);
System.out.println("Reading the map entries for XXX");
session.beginTransaction();
Map entity = (Map)mapSession.get("sample.entity.Category",new Long(2));
System.out.println("Category Title: " + entity.get("title"));
System.out.println("Contains Adverts:");
Set adverts = (Set)entity.get("adverts");
Iterator adIt = adverts.iterator();
while(adIt.hasNext()) {
Map advert = (Map)adIt.next();
System.out.println(advert.get("title"));
}
session.getTransaction().commit();
session.close();
System.out.println("Done.");
}
}
This mode works much the same as the Dom4J mode—changes written to the Map objects
will be persisted exactly as if a normal persistent POJO object had been updated. Note that
only the entities themselves will be represented as Map s—not any of their attributes having a
value type, or associations using Collection types. For example, in Listing A-11, the Category
entity is represented as a Map , but its title attribute is represented as a String and its adverts
attribute is represented as a Set —however, the Set itself contains Advert entities represented
as Map s.
Search WWH ::




Custom Search