Spring dynamically loads the Hibernate ing File

Source: Internet
Author: User

Recently, the general business system framework is still being integrated, and the required configurations are loaded using plug-ins. The spring and struts2 configurations have been dynamically loaded, currently, the rest of the Hibernate ing file is not dynamically loaded yet, so the source code for data search is finally implemented. 1. Override the SessionFactory class to create a class that inherits org. springframework. orm. hibernate3.LocalSessionFactoryBean, rewrite newSessionFactory (Configuration config), and call config in this method. addFile () method adds hbm files to be dynamically loaded. Main Code: [java] public class ShineSessionFactoryBean extends LocalSessionFactoryBean {@ Override protected SessionFactory newSessionFactory (Configuration config) throws HibernateException {config. addFile ("xx1.hbm. xml "); // The hbm configuration to be added here All files are loaded into config. addFile ("xx2.hbm. xml "); return super. newSessionFactory (config);} 2. Modify the Spring configuration file to change the original org. springframework. orm. change hibernate3.LocalSessionFactoryBean to the rewritten class, remove the hbm path configured here, or retain it, but do not duplicate it with the hbm file injected by the plug-in. Other attributes are the same. [Html] <bean id = "sessionFactory" class = "com. shine. spring. ShineSessionFactoryBean"> <property name = "dataSource" ref = "dataSource"/> <! -- The hbm file has been changed to <property name = "mappingLocations"> <list> <value> classpath *: com/shine/**/entity/hbm /*. hbm. xml </value> </list> </property> --> </bean> takes effect after restart. You must pay attention to the following issues during integration: 1. If newSessionFactory obtains the hbm file to be loaded from the global variables of the system, you must set the value for the global variable before the spring container initialization. 2. Dynamically Loaded hbm. make sure that the file path exists in the xml file. If the configuration file to be loaded is in the jar package or is not on the local machine, you must read the configuration file and encapsulate it. 3. If the system requires multiple SessionFactory instances, you must load different hbm configuration files for different SessionFactory instances (I add the ID attribute for SessionFactory ).

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.