Hibernate with JDO

Source: Internet
Author: User

On theserverside.com, there are those who claim that JDO developers have taken JDO to the wrong path and point out that hibernate is relatively superior. However, according to my experience hibernate and JDO are excellent technologies for O/R mapping.

They have many common characteristics, including: the nearly transparent persistence layer that supports plain old Java Object (POJO) is based on object/relational mappings of XML both have a "Entitymanager" Api-hibernate Se The cache-rich query Language (query language) that ssion and JDO PersistenceManager can run either at the application transaction level and at the application level in the container is capable of loading the associated objects in both positive and negative ways Efficient handling of large data sets

As a result, JDO and Hibernate two versions of the same application are often very similar.

For an example of how to load objects and execute queries, let's take a look at the following examples. There are two versions of the Restaurantrepository class, one is JDO and the other is hibernate. The Restaurantrepository class defines a way to find a restaurant: Findrestaurant ()-Find a single hotel (similar to Findbyprimarykey ()). Findavailablerestaurants ()-Execute a query to find a restaurant that is open at a specific time in a specific area

Listing 1 shows the Restaurantrepository class for the JDO version, and listing 2 shows the version of Hibernate.

List 1-JDO version

public class Jdorestaurantrepositoryimpl
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.openhour = = Hour && tr.openminute <= minute)) && "
            +" (Tr.closehour > Hour | | (Tr.closehour = = Hour && tr.closeminute > Minute))) ";

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;
}
}

List 2-hibernate version

public class Hibernaterestaurantrepositoryimpl
Implements 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);
Query.setinteger ("Hour", hour);
Query.setinteger ("Minute", minute);
Query.setcacheable (TRUE);
return query;
}
}

Each repository is made up of two methods. The two methods invoke the corresponding persistence framework api:findrestaurant ()-JDO version to invoke Persistencemanager.getobjectbyid () and Hibernate version invoke Sessio N.load () findavailablerestaurants ()-Two versions use query interface to execute queries with well-known parameters. There is a different place, Hibernate version with named query, named query will be defined in a mapping file, and the JDO version with the embedded in the class query

Two classes get Hibernate session and JDO PersistenceManager via threadlocal-based registry. As you can see, other code is very similar except for the names of the methods and classes.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.