Hibernate 3.0 Rule Application points

Source: Internet
Author: User
Tags expression final implement key sql object model reference
   First, the preface

Hibernate and spring are two well-known open source frameworks that are increasingly used in the development of Java EE applications. Although they are targeted differently, they all have a common feature: dependency mapping. Spring focuses on helping to pick out dependencies between objects before returning objects to customers, which greatly reduces the client's coding effort. Hibernate is focused on picking out the dependencies of the object model description before returning the complete object model to the customer. When using JDBC directly to map a data model to an object model, we usually need to write a lot of code to build an object model. Hibernate can help eliminate most of this coding effort.

The Hibernate 2.x provides basic table to object mapping, normal association mappings (including one-to-one, one-to-many and many-to-many relationships), polymorphic mappings, and so on. Hibernate 3.x has advanced this technology again-by using rules, filtering, sub-selection, and so on to enhance the flexibility of mappings-they provide a good granularity of interpretation.

In this article, we will show you how the different characteristics of the rule help to implement the model transformation. Before Hibernate 3.x, rule properties can only appear in a property element. Although you can do this, however, the Hibernate 3.x provides a rule attribute or element (which, in fact, is equivalent when it comes to rule usage) and can be applied to many elements, including discriminator,many-to-one,one-to-one , Element,many-to-many,map-key,map-key-many-to-many and property. This strengthens the flexibility of object-relational (O-r) mappings, and thus allows for better granularity interpretation of more complex data models.

There are generally two scenarios in which rules must be used:

1. The place where the rule results are to be calculated. The rules used in conjunction with element Discriminator,element,map-key,map-key-many-to-many and property are of this type.

2. The purpose of linking is through the help of rules. The rules used in conjunction with element Many-to-one,one-to-one and Many-to-many belong to this type.

   two, type 1: Obtain a rule of the calculation result

(i) discriminator

In the actual data schema, a table is often used to describe the situation of another table. Rules help to achieve flexible polymorphism in o-r mappings.

In the example shown in Figure 1, there are two tables: Product and Productrelease. Each product record has a productreleaseid that is used to refer to its corresponding product release record, including the product release name, type, release date, and so on.

An interesting attribute in the Productrelease table is subproductallowable, whose value can be 0 or 1. A value of 1 means that any distribution of the product can have a corresponding child product, and a value of 0 means that no child products are allowed. For example, some products are configured with multiple seed products, and some products can only be themselves.

Figure 2 shows an object model that is interpreted from the data model. wherein, the nested interface defines the Getsubproducts and Setsubproducts methods. Class Nestedproduct extends the base class product and implements nested interfaces. Whether a product data record belongs to or belongs to the subproductallowable value of the nestedproduct dependent on its corresponding product release record.


Figure 1. Product and product release data model

Figure 2. Product and product release object domain model

To successfully complete this model transformation, we used a hibernate3.x mapping, as follows:






If the rule expression evaluates to 0-that is, the corresponding child product is not allowed-then the object will be class product. If the result is 1, the object will be a nestedproduct. In tables 1 and 2, for the first record in the Product table (PRODUCTID=10000001), the initialization class will be a nestedproduct because it passes through the subproductallowable= 1 reference refers to a productrelease record. For the second record in the Product table (PRODUCTID=20000001), the initialization class will be product because it references a productrelease record through the subproductallowable=0 reference.

s/n Productreleaseid Subproductallowable ...
1 11 1 ...
2 601 0 ...
Table 1. Records in table Productrelease

s/n ProductID Productreleaseid ...
1 10000001 11 ...
2 20000001 601 ...
Table 2. Records in Table product
(b) Property

A rule in a property element allows an object attribute to contain the results of a derived value, such as Sum,average,max, and so on, as follows:


In addition, a rule that helps retrieve values from another table, based on some of the property values of the current record. For example:


This helps to retrieve the name of a currency from the current Currency table. Obviously, these direct mappings can eliminate a lot of coding transformation problems.

(iii) Map-key

The rule also allows Map-key to fetch any possible values. In the following example (Figure 3), we want to make Role_roleid the Map-key of the object model (Figure 4).


Figure 3. User role Data patterns

Figure 4. User Role Object Model
In the above data schema, user and role are linked through a many-to-many relational table called User_has_role. To get a user who has been assigned to all roles, we use the following mappings:




Table= "Userrole"/>

Formula= "Role_roleid"
Type= "string"/>
Column= "Role_roleid"
class= "Role"/>




Role_roleid is used as a MANY-TO-MANY element join column column value. However, Hibernate does not allow Role_roleid to appear in Map-key and Many-to-many column properties. But with the help of rules, Role_roleid can also be used for Map-key.

(d) Other situations: Element,map-key-many-to-many and more

An element such as a property can be assigned to the computed value of any valid rule expression.

Rules are used in conjunction with map-key-many-to-many similar to Map-key. However, Map-key-many-to-many is often used in Sanchong relationship expression, when a map key itself is a reference object, not a reference attribute.

However, the use of rules is not supported in some cases. Some databases, such as ORACLE7, do not support embedded SELECT statements (for example, a select SQL statement is embedded in the select part of an SQL statement), and the rule is not supported for the result of the operation. Therefore, you need to first check to see if the embedded select SQL statement is supported.

Now that the SQL statement that Hibernate maps uses as part of its select target, a full understanding of the SQL syntax of your database will help enrich your use of the rules, although it may weaken the portability of your code.

  Iii. Type 2: Using rules to implement Union

(a) multiple to one

In real world data modeling, another typical occasion is the proprietary relationship mapping, which is different from the basic pair of one-to-many and Many-to-many relational mappings. The example in Figure 5 shows that a company may have multiple contacts, but only one is the default contact. The case of a company with multiple contacts is a typical one-to-many relationship. However, in order to identify the contact person in the vacant province, the ContactPerson table uses a property Defaultflag (here 1 is represented, 0 is not).


Figure 5. User role Data patterns

Figure 6. User Role Object Model

To interpret the default contact relationship as an object model (Figure 6), we use the following mappings:



Name= "Defaultcontactperson"
Property-ref= "Defaultcontactperson" >

1









On top, we combine CompanyID and defaultflag as an attribute element, named Defaultcontactperson, which forms a unique key to the person table. A many-to-many element in the company class is associated with the Defaultcontactperson attribute element in the person class. As a result, the final SQL statement resembles the following:

Select C.id, p.id from company C, the person p where p.companyid=c.id and p.defaultflag=1

(ii) one-to-one

In hibernate, one-to-one is primarily used for situations where two tables share the same primary key. For foreign bond relationships, we usually use multiple pairs instead. However, with the help of rules, one-to-one can also connect tables through a foreign key. For example, the multiple pairs of examples above can be mapped on a one-to-one basis as follows:



Property-ref= "Defaultcontactperson" >
ID
1









(c) Other: Many-to-many

Rules can be used in a MANY-TO-MANY element to implement special joins from relational tables to entity tables, although this is not often required.

   Iv. Summary

The examples in this article show most of the places where rules are used. When a rule evaluates a value, the rule expression appears in the SELECT statement in the result SQL. When a rule is federated, it appears in the where part of the result SQL. Also, regular expressions can use any personalized SQL, as long as the target database supports it. Our final conclusion is that the flexible application of rules will greatly help build a good granular mapping from the data model to the object model, which does not require any coding work.



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.