About Hibernate Multi-entity (multi-table) join query map to entity discussion

Source: Internet
Author: User
Tags one table

Prerequisite table and Entity information:

In hibernate queries, we encounter three scenarios:

1, inquiry entities, such as: Select B from TableB b; An instance of the entity TableB is encapsulated in the returned list.

2, query a single property, such as: Select b.ID from TableB b; Hibernate returns a list in which the Ojbect object is encapsulated.

3, when querying more than one attribute and not the whole entity, such as: Select B.id,b.mes from TableB b; The returned list encapsulates a object[] array, that is, list is the number of rows, and object[] is the number of columns in a row.

Then we will have a demand, how to query multiple attributes when the value of the object, directly call the entity's Get method to obtain the property values conveniently, many people will be the most primitive, get object[] after the loop to take value,

This processing is more cumbersome, so that the code becomes lengthy, readability is poor, maintenance is not convenient.

Let's take a look at how hibernate implements our functionality, as follows:

For example, if we query TableA's id,name, then our TableA will have a constructor that has both parameters, such as the first part of the code snippet above. HQL is written in the following notation:

Select New Table (ta.id,ta.name) from TableA ta; In this way, Hibernate returns a list in which the TableA object is encapsulated.

When more than one table joins the query, we want to enclose the other table properties in the object, in fact, similar, if we want to TableB in the MES attribute also mapped to TableA, at this time, we will add attributes to the class TableA: Mes, and add the corresponding Set,get method, Note that this property does not exist in TableA, so it is important to ignore the annotations mapped to the database as follows:

@Transient
Private String mes;

The constructor to use when adding a query:

Public TableA (Long id,string name,string mes) {
This.id = ID;
THIS.name = name;
This.mes = mes;
}

Now let's see how HQL is written: select New TableA (ta.id,ta.name,tb.mes) from TableA Ta,tableb TB where ta.relid=tb.id;

In this way, Hibernate returns the TableA object that we want to encapsulate in the list.

In this way, there is a bad place is some temporary property intrusion into the TableA entity, if the addition of more and more properties, such entities will become unrecognizable, so we look at the following more elegant approach.

Introduce temporary objects to store the entity tablec,hql as in the code snippet above:

Select New Com.pt.test.TableC (ta.id,ta.name,tb.id,tb.mes) from TableA Ta,tableb TB where ta.relid=tb.id;

So the list we're returning is encapsulated in the TableC entity.

Note: Com.pt.test.TableC here must add the package name, or will not be reported to find TableC this class of fault, it and already mapped entity class or there is a difference, many people because without the name of the package, mistakenly think that this method is not feasible,

This is wrong.

The above is a personal understanding, if there are errors, please correct me, if there is a better way to handle, but also look forward to the computer before you, share it!

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.