Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

Example Analysis of Hinerbate single-ended Association Agent

2025-01-15 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/02 Report--

Editor to share with you the Hinerbate single-ended association agent example analysis, I believe that most people do not know much about it, so share this article for your reference, I hope you will learn a lot after reading this article, let's go to know it!

In Hinerbate, the delay fetching of collections is implemented in its own way. However, for the delayed fetching of Hinerbate single-ended association, other different mechanisms are needed. Hinerbate single-sided associated target entities must use proxies, and Hihernate implements deferred loading proxies for persistent objects at the run-time binary level (through the excellent CGLIB library).

By default, Hibernate3 will generate proxies for all persistent objects (during the startup phase) and then use them to implement delayed fetching of many-to-one (many-to-one) associations and one-to-one (one-to-one) associations.

In the mapping file, you can declare an interface for use by the proxy interface for the target class by setting the proxy property.

By default, Hibernate will use a subclass of this class. Note: the proxied class must implement a default constructor that is at least visible to the package, and we recommend that all persistent classes have such a constructor

There are many common problems that deserve attention when defining a polymorphic class in this way.

For example:

. .

First, an Cat instance can never be cast to DomesticCat, even if it is itself an DomesticCat instance.

Cat cat = (Cat) session.load (Cat.class, id); / / instantiate a proxy (does not hit the db) if (cat.isDomesticCat ()) {/ / hit the db to initialize the proxy DomesticCat dc = (DomesticCat) cat; / / Error!. }

Second, the "=" of the agent may no longer be valid.

Cat cat = (Cat) session.load (Cat.class, id); / / instantiate a Cat proxy DomesticCat dc = (DomesticCat) session.load (DomesticCat.class, id); / / acquire new DomesticCat proxy! System.out.println (cat==dc); / / false

Even so, the actual situation is not as bad as it seems. Although we now have two different references to these two different proxy objects, in fact, the underlying object should be the same instance object:

Cat.setWeight; / / hit the db to initialize the proxy System.out.println (dc.getWeight ()); / / 11.0

Third, you cannot use CGLIB proxies for "final classes" or "classes with final methods".

* if your persistent object needs some resources for instantiation (for example, in the instantiation method, the default constructor), then the proxy object also needs to use these resources. In fact, the proxy class is a subclass of the persistence class.

These problems stem from the inherent limitations of Java's single-root inheritance model. If you want to avoid these problems, then each of your persistent classes must implement an interface in which its business methods have been declared. Then, you need to specify these interfaces in the mapping document. For example:

. .

Here CatImpl implements the Cat interface and DomesticCatImpl implements the DomesticCat interface. The proxy objects for Cat and DomesticCat are returned in the load () and iterate () methods. (note that list () does not return a proxy object.)

Cat cat = (Cat) session.load (CatImpl.class, catid); Iterator iter = session.iterate ("from CatImpl as cat where cat.name='fritz'"); Cat fritz = (Cat) iter.next ()

Here, the relationship between objects will also be delayed. This means that you should declare the property as Cat, not CatImpl.

However, there are some methods in which proxies are not required.

For example:

The ◆ equals () method, if the persistent class does not overload the equals () method.

The ◆ hashCode () method, if the persistent class does not overload the hashCode () method.

The getter method of the ◆ marker.

Hibernate will recognize persistent classes that overload the equals (), or hashCode () methods.

If we choose lazy= "no-proxy" instead of the default lazy= "proxy", we can avoid the problems caused by type conversion. However, this requires compile-time bytecode enhancement, and all operations result in immediate proxy initialization.

The above is all the contents of the article "sample Analysis of Hinerbate single-ended Association Agent". Thank you for reading! I believe we all have a certain understanding, hope to share the content to help you, if you want to learn more knowledge, welcome to follow the industry information channel!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report