Difference between EntityManager and SessionFactory [message #726935] |
Mon, 19 September 2011 21:54  |
Eclipse User |
|
|
|
Hello All:
I am new to JPA and often while reading documentation, I see different mechanisms to create queries. One with EntityManager and the other mechanism deals with Sessions. Which is the preferred mechanism?
Session example:
---------------
ReadAllQuery query = new ReadAllQuery(Employee.class);
query.setJPQLString("SELECT OBJECT(emp) FROM Employee emp");
...
List employees = (List)session.executeQuery(query);
Entity Manager example:
----------------------
factory = Persistence.createEntityManagerFactory(PERSISTENCE_UNIT_NAME);
EntityManager em = factory.createEntityManager();
// Read the existing entries and write to console
Query q = em.createQuery("select t from Todo t");
List<Todo> todoList = q.getResultList();
for (Todo todo : todoList) {
System.out.println(todo);
}
What is the preferred mechanism? Can someone throw somelight?
Thanks
Sridhar
|
|
|
|
|
|
Powered by
FUDForum. Page generated in 0.07579 seconds