Use the List-index attribute of the list set in Hibernate search

Source: Internet
Author: User

This article will introduce you to the List-index attributes and problems of using the list set in Hibernate search. I hope this tutorial will be helpful to you.

Hibernate provides the built-in index () function:

The Code is as follows: Copy code

Select item, index (item) from Order order join Order. items item where index (item) <5

A problem in hibernate using list set ing

Main Problem: list-index in the ing list in hibernate:
See a configuration file:

The Code is as follows: Copy code

<Class name = "User" table = "l_user">
<Id column = "id" name = "id"/>
<Property name = "name" column = "username"/>
<List name = "userInfoList" inverse = "true" cascade = "all" lazy = "false">
<Key column = "userid" not-null = "true"/>
<List-index column = "userinfoid"/>
<One-to-learn class = "UserInfo"/>
</List>
</Class>

<Class name = "UserInfo" table = "l_userinfo">
<Id name = "userinfoid" column = "userinfoid"/>
<Property name = "infoType" column = "infotype"/>
<Property name = "startDate" column = "startdate" type = "timestamp"/>
<Property name = "endDate" column = "enddate" type = "timestamp"/>
<Role-to-one name = "includeUser" column = "userid" class = "User"/>
</Class>

In the configuration file of the User class, a List is configured to specify its association with UserInfo. There is a key, which is the foreign key of the table associated with the User class in the table corresponding to the specified UserInfo class. UserId is used, and List-index is the content to be configured in an ordered set. Here we configure userinfoid as the primary key of the table corresponding to UserInfo. But there is a problem here: list-index plays a very important role when we get the User object and then get the List of UserInfo associated with it. List-index will start from 0 and then auto-increment. If a User has 100 associated UserInfo objects. Assume that the IDs of these UserInfo instances are from 0 to 99. He will get the 100 associated objects. If the UserInfo IDs are from 0-50 100-149, then there will be problems, when we access UserInfoLIst through a User, hibernate will automatically issue an SQL statement to obtain the UserInfo object associated with the User. But he will get a UserInfoLIst with a size of 150, but not every one of this list is a complete UserInfo object. In addition, the redundant UserInfo objects are null in this list. This will lead to a very serious problem. If our UserInfo has an Id of 10000, the List returned by UserInfo is a list with a size of 10000. But there are many empty objects in it. In addition, we will do a lot of useless work when traversing and accessing the objects in it. Therefore, it is necessary to create another attribute as the list-index of List ing. However, it is very necessary for us to maintain his value and ensure that he is continuous. In this case, it is not advisable to maintain a bidirectional one-to-many relationship using a foreign key. Or we can replace it with set directly.

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.