Whether you need to create a summary of the associated objects when you use Hibernate to map many-to-many association relationships

Source: Internet
Author: User

Recently in a CMS management project, when the RBAC model was created, a small question was encountered.


We know that using hibernate, you can use Many-to-many annotations or XML configurations to automate many-to-many mappings, such as: User Role Group Three objects, for example, User and group.


In a database table, there must be a t_user_group table, and hibernate can be used to complete cascade additions, deletions, queries, and so on without establishing UserGroup objects.


For the time being, it is not necessary for us to create the UserGroup object in the entity model.


However, if you encounter the following situation: The project service layer frequently requires the user and Group two objects together query use, then this time usergroup this object looks like it is necessary to establish.


Analysis to this step, only to compare in this case, the two methods to send to the database of the number of statements can be basic analysis of the need to establish a UserGroup object.


First, when you do not use UserGroup:

String hql = "Select U,gs from the User u left join u.groups gs where U.ID>? and Gs.id>? ";
Query query = sessionfactory.getcurrentsession (). CreateQuery (HQL);
Query.setparameter (0, 4);
Query.setparameter (1, 1);
list<object[]> GS = Query.list ();
For (object[] object:gs) {
User U = (user) object[0];
Group G = (group) object[1];
System.out.println (U.getusername () + "---" +g.getname ());
}

here is the SQL statement for the console output, only one , and you can

Hibernate:select user0_.id as id1_0_, group2_.id as id0_1_, User0_.email as email1_0_, User0_.password as password1_0_, u Ser0_.username as username1_0_, group2_.description as descript2_0_1_, group2_.name as name0_1_ from T_user user0_ left OU ter join T_group_user groups1_ on user0_.id=groups1_.uid left outer joins T_group Group2_ on Groups1_.gid=group2_.id where User0_.id>? and Group2_.id>?

Results:

Niuba---Group3
Niuba---group2
Wujiu---group2
Zhaoqi---Group3
zhangsan1111---Group3
zhangsan1111---group2

So there's no need to do another test of usergroup, because at least one statement is required.


Conclusion: Even if the user and group are often paired together, it is not necessary to establish a UserGroup association object. Just implement the appropriate method in the Userdao method, and then return a list collection (the contents of the collection is the data of an Object--object[]), and then remove user and Group one by one in object[].


Whether you need to create a summary of the associated objects when you use Hibernate to map many-to-many association relationships

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.