Hibernate and JDO work side by side

Source: Internet
Author: User
Tags getzip

On theserverside.com, some people claim that the JDO developer has taken the JDO to the wrong path and pointed out that hibernate is relatively better. However, based on my experience, Hibernate and JDO are both excellent o/R Mapping Technologies.

They have many common features, including:

  • Support for almost transparent persistent layers of plain old Java object (pojo)
  • Object/relational ing Based on XML
  • Both have an "entitymanager" API-hibernate session and JDO persistencemanager
  • You can run applications in or out of the container.
  • Transaction-level and application-level cache
  • Rich query languages)
  • Ability to load associated objects in both positive and negative ways
  • Efficient management of big data sets

Therefore, the same application of JDO and Hibernate is often very similar.

Let's take a look at the example below for how to load objects and execute queries. There are two versions of the restaurantrepository class: one is JDO and the other is hibernate. The antrepository class defines how to find a hotel:

  • Findrestaurant ()-find a single hotel (similar to findbyprimarykey ()).
  • Findavailablerestaurants ()-run the query to find the hotels in the specified region at a specific time.

List 1 shows the jsonantrepository class of JDO, and list 2 shows the hibernate version.

List 1-JDO

Public class jdorestaurantrepositoryimpl
Implements implements antrepositoryimpl {

Public restaurant findrestaurant (string parameter antid ){
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 + "(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.exe cutewitharray (
New object [] {
Deliveryaddress. getzip (),
New INTEGER (dayofweek ),
New INTEGER (hour ),
New INTEGER (minute )});
Return result;
}
}

 

List 2-Hibernate

Public class hibernaterestaurantrepositoryimpl
Implements implements antrepositoryimpl {

Public restaurant findrestaurant (string parameter antid ){
Try {
Session session = hibernatesessionregistry. getsession ();
Restaurant restaurant =
(Restaurant) Session. Load (
Hibernaterestaurant. Class,
New Long (repeated antid ));
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 consists of two methods. The two methods call the corresponding persistence framework API:

  • Findrestaurant ()-JDO calls persistencemanager. getobjectbyid (), while hibernate calls session. Load ()
  • Findavailablerestaurants ()-both versions use Query Interfaces to query famous parameters. the difference is that in hibernate, named query is used, and named query is defined in a ing file, while in JDO, the query is embedded in the class.

The two classes use threadlocal-based registry to obtain the hibernate session and JDO persistencemanager. As you can see, except for the method and class names, other codes are very similar.

 

 

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.