Hibernate and JDO shoulders

zhaozj2021-02-16  61

Some people who claimed that JDO's developers have brought the JDO to the wrong path and pointed out that Hibernate is relatively excellent. However, according to my experience, Hibernate and JDO are O / R mapping excellent technology.

They have many common features, including:

Support Plain Old Java Object (Pojo) near transparent persistence layer

XML-based Object / RELATIONAL mapping

Both have a "EntityManager" API - Hibernate Session and JDO PersistenceManager

Can run an application in or not in Container

Transaction grade and application level cache

Rich Query Language

Can load related objects in two ways to actively and negative

Efficiently handle big data collection

Therefore, both of JDO and Hibernate's two versions of the same application are often similar.

Regarding how to load objects and execute queries, let's take a look at the example below. There are two version of the RestaurantRepository class here, one is JDO, and the other is hibernate. The RestaurantRepository class defines the way to find a hotel:

Findrestaurant () - Find a single hotel (similar to FindByPrimaryKey ()).

FINDAVAILASTAURANTS () - Execute Query to find a hotel in a specific area-specific area

Listing 1 shows the JDO version of the RESTAURANTREPOSITORY class, and the list 2 shows the version of Hibernate.

Listing 1 - JDO version

Public Class JdoStaurantrepositoryImpl Implements RestaurantRepositoryImpl {

public Restaurant findRestaurant (String restaurantId) {PersistenceManager pm = PMRegistry.getPM (); return (Restaurant) pm.getObjectById (pm.newObjectIdInstance (JDORestaurant.class, restaurantId), true);}

Private static final string query_string = "ServiceareA.Contains (Zipcode) && timeranges.contains (tr) &&" "(tr.Dayofweek == day &&" "(Tr.OpenHOUR == HOUR) && tr.openminute <= minute)) && " " (Tr.CloseHOUR> Hour ||)) ";

public Collection findRestaurants (Address deliveryAddress, Date deliveryTime) {Calendar c = Calendar.getInstance (); c.setTime (deliveryTime); int dayOfWeek = c.get (Calendar.DAY_OF_WEEK); int hour = c.get (Calendar.HOUR_OF_DAY) ; int minute = c.get (Calendar.MINUTE); PersistenceManager pm = PMRegistry.getPM (); Query query = pm.newQuery (JDORestaurant.class, QUERY_STRING); query.declareVariables ( "JDOTimeRange tr"); query.declareParameters ( "String zipCode, int day, int hour, int minute"); Collection result = (Collection) query.executeWithArray (new Object [] {deliveryAddress.getZip (), new Integer (dayOfWeek), new Integer (hour), new Integer (minute)}); Return Result;}}}

Listing 2 - Hibernate

Public class hibernaterestaurantRepositRepositoryImpl imports restaurantRepositoryImpl {

public Restaurant findRestaurant (String restaurantId) {try {Session session = HibernateSessionRegistry.getSession (); Restaurant restaurant = (Restaurant) session.load (HibernateRestaurant.class, new Long (restaurantId)); return restaurant;} catch (HibernateException e) { Throw New ApplicationRuntimeException (e);}}

public Collection findRestaurants (Address deliveryAddress, Date deliveryTime) {try {Query query = makeFindRestaurantsQuery (deliveryAddress, deliveryTime); return query.list ();} catch (HibernateException e) {throw new ApplicationRuntimeException (e);}} private Query makeFindRestaurantsQuery ( Address deliveryAddress, Date deliveryTime) throws HibernateException {Calendar c = Calendar.getInstance (); c.setTime (deliveryTime); int dayOfWeek = c.get (Calendar.DAY_OF_WEEK); int hour = c.get (Calendar.HOUR_OF_DAY); int minute = c.get (Calendar.MINUTE); Session session = HibernateSessionRegistry.getSession (); Query query = session.getNamedQuery ( "findAvailableRestaurants"); query.setString ( "zipCode", deliveryAddress.getZip ()); query.setInteger ("dayofweek", dayofweek; qu Ery.setInteger ("Hour", Hour); Query.setCacheable; Return query;}

Each repository consists of two methods. These two methods call the corresponding Persistence Framework API:

Findrestaurant () - JDO version Calls PersistenceManager.getObjectByid () and Hibernate version calls session.Load ()

Findavailaerestaurants () - Two versions use query interface to perform a query of a famous parameter. There is a different place, the Hibernate version is defined in a mapping file, while the JDO version is in the Query of the JDO

Two classes get Hibernate Session and JDO PersistenceManager through Threadlocal-Based Registry. As you can see, other CODEs are very similar in addition to the methods and classes of the name.

转载请注明原文地址:https://www.9cbs.com/read-24999.html

New Post(0)