The instance demonstrates how to integrate hibernate5.0.2 in spring4.2.2 and create sessionFactory
Purpose: To use spring4.2.2 to integrate hibernate5.0.2 and create a sessionFactory instance.
Development Environment: eclipse (jee_mars) JDK1.8 MYSQL5.6
Spring: https://repo.spring.io/list/release/org/springframework/spring/4.2.2.RELEASE/
I. XML configuration method
Environment setup: Create a java project xmlBasedspringhelloword and introduce the following jar package:
1. Introduce the jar package required by hibernate: Antlr-2.7.7.jar, dom4j-1.6.1.jar, geronimo-jta_1.1_spec-1.1.1.jar, hibernate-core-5.0.2.Final.jar, hibernate-jpa-2.1-api-1.0.0.Final.jar, jandex-1.2.2.Final.jar, javassist-3.18.1-GA.jar
2. Create the hibernate configuration file hibernate. cfg. xml.
The configuration file is as follows:
com.mysql.jdbc.Driver
jdbc:mysql://localhost:3306/basichibernatepractice?characterEncoding=UTF8
root
123123
1
org.hibernate.connection.C3P0ConnectionProvider
5
20
120
3000
org.hibernate.dialect.MySQL5Dialect
org.hibernate.cache.internal.NoCacheProvider
true
update
3. Import the jar package required by spring: spring-beans-4.2.2.RELEASE.jar, spring-context-4.2.2.RELEASE.jar, spring-core-4.2.2.RELEASE.jar, spring-expression-4.2.2.RELEASE.jar, spring-orm-4.2.2.RELEASE.jar, spring-tx-4.2.2.RELEASE.jar
4. Create spring container ApplicationContext. xml
The content is as follows:
5. The imported jar package is as follows.
6. Create a test class Spring4WithHibernate5HelloTest
The implementation is as follows:
package com.sushengmiyan.hellospring.hellohibernate5;import org.hibernate.SessionFactory;import org.springframework.context.ApplicationContext;import org.springframework.context.support.ClassPathXmlApplicationContext;public class Spring4WithHibernate5HelloTest {public static void main(String[] args) {@SuppressWarnings(resource)ApplicationContext context = new ClassPathXmlApplicationContext(ApplicationContext.xml);SessionFactory sf = context.getBean(localSessionFactoryBean, SessionFactory.class);System.out.println(sf);}}
Run the test and sessionFactory is output successfully.
Implementation:
1. Configure hibernate in Normal Mode
2. Create a spring container and add a reference to hibernate to the container.
3. Get it from the container. Note that org. springframework. orm. hibernate5.LocalSessionFactoryBean is added to the container, but the SessionFactory generated by it is obtained when it is retrieved from the container.
Otherwise, an Exception occurs in thread main org. springframework. beans. factory. beanNotOfRequiredTypeException: Bean named 'localsessionfactorybean' must be of type [org. springframework. orm. hibernate5.LocalSessionFactoryBean], but was actually of type [org. hibernate. internal. sessionFactoryImpl]
II. Java configuration method
1. Create a new project javaBasedspringhelloword and port the above hibernate configuration to this project.
2. Add a new jar package spring-aop-4.2.2.RELEASE.jar
3. Fill in the configuration file java Appconfig. java
The content is as follows:
package com.sushengmiyan.hellospring.helloHibernate5;import org.springframework.context.annotation.Bean;import org.springframework.context.annotation.Configuration;import org.springframework.core.io.ClassPathResource;import org.springframework.orm.hibernate5.LocalSessionFactoryBean;@Configurationpublic class AppConfig {@Beanpublic LocalSessionFactoryBean localSessionFactoryBean(){LocalSessionFactoryBean localSessionFactoryBean = new LocalSessionFactoryBean();localSessionFactoryBean.setConfigLocation(new ClassPathResource(hibernate.cfg.xml));return localSessionFactoryBean;}}
This is equivalent to the Application. xml file in the preceding xml configuration.
4. Test writing
package com.sushengmiyan.hellospring.helloHibernate5;import org.hibernate.SessionFactory;import org.springframework.context.ApplicationContext;import org.springframework.context.annotation.AnnotationConfigApplicationContext;public class Spring4WithHibernate5HelloTest {public static void main(String[] args) {ApplicationContext context = new AnnotationConfigApplicationContext(AppConfig.class);SessionFactory sf = context.getBean(localSessionFactoryBean, SessionFactory.class);System.out.println(sf);}}
You can also see the normal output.