Using hibernate to map the relationship of objects (1)

Source: Internet
Author: User
Tags jboss websphere application server
Hibernate is the industry's preferred ORM (O/R mapping), the current version is very stable and mature, and his documents are extremely rich.
Http:// and These two technical website to hibernate discussion a lot of also very subtlety.
This article is an article about getting out of the scholar.
--The translator presses

There are many ways to persist data in Java technology, and the persistence layer is one of the most important parts of Java applications. In today's relational database as a mainstream database, I personally think that the persistence layer technology is often not applied to the attention. In this article, there will be several well-known persistence frameworks implemented in Java technology, and then we'll talk about some of the most recent persistence frameworks, such as: Hibernate, let's start with the most common APIs from those well-known frameworks.

1. Jdbc
Most Java developers use JDBC to communicate with the database, which can be improved and improved with the DAO (Data Access Object) pattern, which in large applications creates a "high consumption" of maintenance. Most developers, however, are very familiar with JDBC operations and their APIs, so I believe it is still one of the most widely used data persistence technologies in any case. (Don't worry "the nightmare will be over soon", translator)

2. Ejb
The survey of EJB is usually the second choice in data persistence technology, which is to persist the data through entity beans, which sounds like a "silver sheet" in the Java Persistence Framework (really silver bullet. I mean in a huge market (a potential big piece of cake). )。 But it's not true: first you need to buy a reasonably priced EJB container--J2EE application Server, it's a good choice to use a free EJB container for open source projects: for example, JBoss (if you'll excuse me); second, the full use of entity beans requires "a lot" Time to understand the EJB specification. Before using EJB you usually want to be proficient in its API; again, you need to know the deployment descriptor that is proprietary in every container in addition to Ejb-jar.xml, and the performance and technical support of many commercial EJB containers are not flattering. For Java developers, it is also more complex to implement JDBC in EJBS. A stateless session bean (stateless-session beans) and message-driven bean (messaging driver beans) may be the most closely watched in EJBs

3. More durable frameworks
After a flurry of "dithering and hesitation" over the previous two specifications, you may find that they are not the perfect solution. There seems to be some improvement in JDO's appearance, but JDO1.0, whether it's operational or functional, seems to be "sky or grey" for Java Developers, "and without a good free product," JDO1.0 still hasn't brought much change, and people still stay in the stage of learning with it. , a situation that needs to be resolved by JDO's maturity. At present, Sun has joined to begin JDO2.0, we only wait for the dawn. )
So what should we do? If you put aside mainstream non-mainstream concepts, you will find that you will have more choices (perhaps better). If so, you will not be aware of such two names "Hibernate" and "OJB", which are very different from the previous mentioned JDBC, EJB, and jdo in some ways, they do not require any containers, provide simplicity and are compliant with Odmg3-style APIs, and they are free, open source, rich in documentation and a stable development background.
Maybe all you have to do is choose.

4 "Hibernation" Hibernate
Let's start now, I chose hibernate in these "fresh" enduring frameworks, and at the outset I included OJB in my choice, but in the end the reason for choosing Hibernate was due to its rich documentation, and I know that many others have been successful with OJB. In any case, hibernate and OJB will allow you to completely manipulate the persistence layer.
Visit the to obtain a hibernate feature introduction.
I'd like to start with my environment: Hibernate 2.0 RC1 (2.0 Beta 5) + Tomcat 4.1.18+websphere application Server 5.0.1 (please go to the appropriate official website to get the latest version, translator note) , my example would work in other containers, but I didn't test it. The Hibernate Web site has information about the configuration under JBoss, and typically almost all application servers and databases can integrate Hibernate.

4. 1 Mapping relationships with Hibernate
Here we discuss how to use hibernate to map the relationship of objects. I'll explain how to use hibernate for "one-to-one", "One-to-many", "Many-to-many" mappings. In the code I provided, it was just for testing, but not for more "patterns, rules," etc. this is to say that the example here is just an enlightenment, let us learn hibernate API from the code and how to develop, please note the necessary patterns and coding specifications when you write yourself.

And then we'll look at what's in my example and let's start modeling for them.
(to be continued)    

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: 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.