Some configuration problems of Hibernate and Spring Integration (i)--localsessionfactorybean

Source: Internet
Author: User

Objective

Novice Learning to Hibernate and spring integration, if there is no good tutorial examples, it is easy to get overwhelmed, this time will instinctively go to the network to find relevant examples. However, the network of case-level, some very old, some of the framework and integration of a lot, rarely meet their own development phase of the case, let people look at the big head. Here, I briefly introduce some of the Localsessionfactorybean and Hibernate DAO layer developments. I hope to give some help to those who need it.

Body

Some of the examples on the web use Localsessionfactorybean in the spring configuration file. Some people may not notice, only see this bean ID is sessionfactory, think it is sessionfatory. At first, I also thought that it might be a specific implementation class of some sessionfactory, and did not care. When I looked closely, I found that it was not. Let's take a look at this picture first.

Owning file: Spring-dao.xml

  

This is part of the spring configuration file that I do with the curriculum design, from the above diagram, the first eye, you can see the path of the reference class is Org.springframework.orm ... As you can see, here we need to introduce spring-orm, a package that can be introduced through MAVEN. Also can see I injected a configured datasource (data source), is actually the database connection pool, used to manage the connection, the current is more popular is druid. CONFIGLOCATION Specifies the address of the configuration file, which loads the contents of the configuration file to generate a real sessionfactory.

Owning file: Hibernate.cfg.xml

    

In addition, the path of the scan package is configured. We know that Hibernate makes certain updates to the database tables every time sessionfactory is generated. The update must be based on Pojo. I write my own SSH project is written pojo through hibernate mapping to build the table (it is said to have from the existing database in turn built Pojo, but it has not been used to), generally do not have a complete design to do, so, Pojo may be often changed in the early days, Database tables should also be changed accordingly. Again, since you want to map, you have to let hibernate know which pojo to map to the database table. So to specify which package to scan, Spring's package-scan will recognize the annotated @component, @Service, @Repository, @Entity ... class, configured as a bean to inject into the IOC container, and wait until it is used, @Autowired. However, Hibernate will only recognize @entity classes here. Map to a database table.

We go back to Localsessionfactorybean, and now we can see that it is not sessionfactory, but it is injected into the sessionfactory. But, why its ID is sessionfactory, we @autowired the time to introduce it, and the obtained is indeed sessionfactory.

Owning file: Basedaoimpl.java

  

In fact, because Localsessionfactorybean implements the Factorybean interface, the class that implements this interface has a feature, that is, when referencing this class, spring returns not the class itself, Instead, it calls the GetObject () method of the Factorybean interface to get the bean, and Localsessionfactorybean's GetObject () method returns its sessionfactory.

Some configuration problems of Hibernate and Spring Integration (i)--localsessionfactorybean

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.