Eclipse Community Forums
Forum Search:

Search      Help    Register    Login    Home
Home » Eclipse Projects » EclipseLink » Difference between EntityManager and SessionFactory(Difference between EntityManager and SessionFactory)
Difference between EntityManager and SessionFactory [message #726935] Tue, 20 September 2011 01:54 Go to next message
Sridhar Baratam is currently offline Sridhar BaratamFriend
Messages: 22
Registered: September 2011
Junior Member
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
Re: Difference between EntityManager and SessionFactory [message #728085 is a reply to message #726935] Thu, 22 September 2011 14:06 Go to previous messageGo to next message
James Sutherland is currently offline James SutherlandFriend
Messages: 1939
Registered: July 2009
Location: Ottawa, Canada
Senior Member

JPA defines the EntityManager interface and it should be used with JPA whenever possible.

The Session API is EclipseLink's native API that existed pre-JPA.

Using JPA is recommend, as it is the standard. If required, you can still access the EclipseLink Session from the JPA EntityManager to access advanced functionality.



James : Wiki : Book : Blog : Twitter
Re: Difference between EntityManager and SessionFactory [message #728142 is a reply to message #728085] Thu, 22 September 2011 15:43 Go to previous messageGo to next message
Sridhar Baratam is currently offline Sridhar BaratamFriend
Messages: 22
Registered: September 2011
Junior Member
Thanks James. It clears a lot of confusion for me. Thanks again.
(no subject) [message #728163 is a reply to message #728085] Thu, 22 September 2011 15:43 Go to previous message
Sridhar is currently offline SridharFriend
Messages: 12
Registered: July 2009
Junior Member
Thanks James. It clears a lot of confusion for me. Thanks again.
Previous Topic:GeneratedValue on SecondaryTable
Next Topic:(no subject)
Goto Forum:
  


Current Time: Mon Dec 22 01:37:07 GMT 2014

Powered by FUDForum. Page generated in 0.09026 seconds
.:: Contact :: Home ::.

Powered by: FUDforum 3.0.2.
Copyright ©2001-2010 FUDforum Bulletin Board Software