Cannot simultaneously fetch multiple bags exception Solution

Source: Internet
Author: User

From http://howsun.blog.sohu.com/119020926.html

EJB persistence layer cannot simultaneously
Troubleshooting of fetch multiple bags exceptions

Pathological Features: caused by: org. hibernate. hibernateexception:
Cannot simultaneously fetch multiple bags,


Stack information: org. springframework. Beans. Factory. beancreationexception: Error
Creating bean with name 'initsystemdatabyspringlisteren': injection
Resource fields failed; Nested exception is
Org. springframework. Beans. Factory. beancreationexception: Error creating
Bean with name 'service': injection of resource fields failed; Nested
Exception is org. springframework. Beans. Factory. beancreationexception:
Error creating bean with name 'assistantdao ': injection of Resource
Fields failed; Nested exception is
Org. springframework. Beans. Factory. beancreationexception: Error creating
Bean with name 'sessionfactory 'defined in class path Resource
[Spring-core-config.xml]: Invocation of init method failed; Nested
Exception is org. hibernate. hibernateexception: cannot simultaneously
Fetch multiple bags

......

Cannot simultaneously fetch multiple
A bags exception occurs when the persistent layer loads too many similarities and differences objects in real time. For example, when a user logs on, the user's role objects and permission objects are synchronously loaded in real time. These relationships are usually many-to-many relationships, and they are in a single column.
The multiple
Bags. I searched for such results online and almost all proposed a solution-using delayed loading, that is, fetch = fetchtype. Lazy, which binds the powerful functions of the framework,
I have also mentioned using @ indexcolumn, but @ indexcolumn is a thing of hibernate, not a JPA specification (discussed below ). Both real-time loading and guaranteed
There will be no theme issues. This is what we will discuss in this article.


In the JPA specification, one-to-many or many-to-many multi-party data must be stored in the container class after being captured, such as set, list, and map. Beginners may not carefully study these containers, take one
. In fact, these containers are quite different. This article does not specifically discuss the differences and functions of these containers, however, there is a special difference -- the topic of this article -- whether repeated content is allowed in the container
Value, which gives us a simple understanding of the features of this container:

Set:

_ Duplicate elements cannot be stored in set;
_ The elements in the set are unordered.

List:

_ List can store duplicate elements;
The element in _ list is an ordered set. You can access the element in list.

Map:

_ Map stores data by key/value, so it has high access performance;
_ Repeated keys are not allowed in map, but repeated values can exist.


With these differences, we can start to look for theme issues. When the persistent framework captures objects on one side and loads objects on multiple sides into the container, multiple parties may be associated with other objects.
Implemented JPA. By default, the maximum capture depth includes level 4 (it has an attribute configuration of 0-3). If multiple parties (level 2) have repeated values, the value captured in level 3 cannot be mapped. According to this principle,
An exception should be reported that multiple packages cannot be loaded at the same time. Since ejb3.0 and later in China have few teaching materials, and there are no well-developed JPA manuals, this understanding may cause doubts, but the facts
In this way, I solve the problem:


That is to say, the handler of @ manytomany or @ onetomany must use the set container to store the data, instead of the list set.


However, some functions of hibernate go beyond the JPA specifications and support real list sets. The method of ing sets is the same as before,Only add
@ Indexcolumn annotation,

This annotation allows you to specify fields for storing index values. But it is actually the creation of a unique index, and the results of capturing multiple parties are also unique, that is, the above
The Set container does not allow repeated elements.


If this exception occurs, first check whether the list set is used and the problem occurs. If the set is used, check whether there are other similar questions in the class of the object in the Set container.
Question.

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.