A detailed explanation of the Auto-import attribute of hibernate

Source: Internet
Author: User

What does the Auto-import mean?

We often write such a HQL statement:

From User u where u.name= ' Rotan ';

Most of the time, this writing is not going to be a problem.

When hibernate handles this hql, it translates it into an SQL statement that the database can recognize. The basis of translation is of course the mapping relationship between the entity and the database table.

Now we're going to create some problems for him, and we let hibernate manage two entities with the same name: Org.mysoa.security.model.User and Com.kedacom.ksoa.security.model.User.

At this point, we will be the top of the HQL to hibernate resolution, he can also successfully translated into a SQL statement. The answer is of course negative, he does not know you want to check Org.mysoa.security.model.User or Com.kedacom.ksoa.security.model.User.

So, a correct hql should be this:

From Org.mysoa.security.model.User u where u.name= ' Rotan ';

However, most of the time, there is no entity with the same name in a system, and it is not good to ask all hql to write that way. So hibernate provides a auto-import attribute, and when you do not specify a specific entity (only the from User is specified), he automatically finds a unique entity map named User that complements it to org.mysoa.security.model.User.

When you really need two entities with the same name in your system

When you really need two entities with the same name in your system, there are two things we need to do: To set the mapping file of both entities with the same name to auto-import= "false" all about the hql of the two entities, All need to explicitly specify their fully qualified name (such as Org.mysoa.security.model.User)

Someone to ask, as long as you do the second is enough Ah, as long as you ensure that all hql have written fully qualified name, then hibernate parsing will not be wrong, the system should be able to run. Fact Hibernate how do you know that all of your hql have been written fully qualified names. In fact, hibernate will throw an exception and stop loading if there are two entities with the same name found in the system load, but if any one is not set Auto-import=false, In this way he makes sure that your auto-import problem is exposed when the system is loaded, rather than delaying the problem by actually executing a problematic hql.

Org.hibernate.DuplicateMappingException exception information is reported when the auto-import= "false" setting is not done

This exception is hibernate when mapping, there are two ways to resolve this exception:

1. The direct method is to change the name of your class name.

2. Apply auto-import= "false" to set the daily mapping file

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.