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

How to understand Hibernate and model objects

2025-02-22 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

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

This article mainly explains "how to understand Hibernate and model objects". The content of the explanation is simple and clear, and it is easy to learn and understand. Please follow the editor's train of thought to study and learn "how to understand Hibernate and model objects".

When learning Hibernate, we often encounter some minor problems. Here we will introduce the solution to the problem that the plug-in class loading mechanism of the OSGi platform makes it impossible for Hibernate to correctly load the model objects distributed within different plug-ins and Omax R mapping files.

OpenCore is a microkernel (Microkenerl) built on the OSGi specification, based on a pure component (Pure Plugin) open source enterprise application software platform.

The OpenCore data layer implements the integration of Hibernate,Hibernate and its dependent libraries on OSGi as a separate plug-in, which brings a problem, that is, the plug-in class loading mechanism of the OSGi platform makes it impossible for Hibernate to correctly load the model objects and Hibernate R mapping files distributed within different plug-ins. This article discusses the solution:

Model object (Domain Objects) plug-in

Model objects (Domain Objects) are centralized into separate plug-ins (Bundle), and Hibernate plug-ins rely on these model object plug-ins. This is the easiest and worst way to do this, and smaller OSGi-based projects can do the same.

Eclipse-BuddyPolicy and Eclipse-RegisterBuddy mode

The platform-specific approach of Equinox (OSGi implementation provided by Eclipse) allows plug-ins (Bundle) to declare their partners and "partner plug-ins" to dynamically load the plug-in's classes, which is also the official solution for Hiberate and Equinox integration. In this way, the model object does not need to be deployed in a separate plug-in, it can be deployed with the business plug-in, and the Hibernate plug-in does not have to rely on the model object.

The specific practices are as follows:

First, the Hibernate plug-in (name, for example, org.opengoss.orm.hibernate) declares that it can be used as a partner plug-in, adding the description to the self-description file (MANIFEST.MF): Eclipse-BuddyPolicy: registered

Then, the Hibernate plug-in is added as a partner in the business plug-in of the model object, and the self-description file (MANIFEST.MF) adds the description: Eclipse-RegisterBuddy:org.opengoss.orm.hibernate

Eclipse Extension Point mode

This is the way we currently implement. Through the standard Eclipse extension point and extension mechanism, we declare the following extension point in the plugin.xml configuration file in the Hibernate plug-in and the extension in the model object plug-in, for example:

During the startup of the Hibernate plug-in, generate SessionFactory with code configuration as follows:

Public void start (BundleContext context) throws Exception {

Configuration configuration = new Configuration () .configure

(new File (". / etc/org.opengoss.database.hibernate/hibernate.cfg.xml"))

Class [] domainClasses = getDomainClasses ()

For (Class domainClass: domainClasses) {

Configuration.addClass (domainClass)

}

SessionFactory = configuration.buildSessionFactory ()

Dictionarynew Hashtable props.put ("scope", "APPLICATION")

Props.put ("uid", "Hibernate:SessionFactory")

Registration = context.registerService

(SessionFactory.class.getName (), sessionFactory, props)

}

Private Class [] getDomainClasses () throws Exception {

List domainClasses = new ArrayList ()

IExtensionPoint point = registry.getExtensionPoint

(IConstants.DOMAIN_OBJECT_EXTENSION_POINT)

IExtension [] extensions = point.getExtensions ()

For (IExtension extension: extensions) {

IConfigurationElement [] elements = extension.getConfigurationElements ()

For (IConfigurationElement configurationElement: elements) {

Bundle bundle = pluginContext.getBundleBySymbolId

(extension.getNamespaceIdentifier ())

Class domainClass = bundle.loadClass

(configurationElement.getAttribute ("class"))

DomainClasses.add (domainClass)

}

}

Return domainClasses.toArray (new Class [domainClasses.size ()])

}

Note: the class loading mechanism within Hibernate is really unsatisfactory, and although we have loaded all the model class objects in this way, Class.forName () is still called inside Hibernate to attempt to load.

Thank you for your reading, the above is the content of "how to understand Hibernate and model objects". After the study of this article, I believe you have a deeper understanding of how to understand Hibernate and model objects, and the specific use needs to be verified in practice. Here is, the editor will push for you more related knowledge points of the article, welcome to follow!

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