Example shows how to integrate hibernate5.0.2 and create sessionfactory in spring4.2.2

Source: Internet
Author: User

This address: http://blog.csdn.net/sushengmiyan/article/details/49388209

Article mattered M along


Purpose of this article: use spring4.2.2 to integrate hibernate5.0.2 and create sessionfactory instances.

Development environment: Eclipse (jee_mars) JDK1.8 MYSQL5.6

spring:https://repo.spring.io/list/release/org/springframework/spring/4.2.2.release/


Environment Building: Create a new Java Engineering Xmlbasedspringhelloword, introducing the following jar packages:

1. Introducing Hibernate-Required JAR packages : 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, Jboss-logging-3.3.0.final.jar


2. Create a new hibernate configuration file Hibernate.cfg.xml

The contents of the configuration file are as follows:

<?xml version= ' 1.0 ' encoding= ' utf-8 '? ><! DOCTYPE hibernate-configuration Public "-//hibernate/hibernate configuration DTD 3.0//en" "Http://www.hibern Ate.org/dtd/hibernate-configuration-3.0.dtd ">


3. Import the jar packages 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. New Spring Container Applicationcontext.xml

The contents are as follows:

<?xml version= "1.0" encoding= "UTF-8"? ><beans xmlns= "Http://www.springframework.org/schema/beans"    Xmlns:xsi= "Http://www.w3.org/2001/XMLSchema-instance"    xsi:schemalocation= "http://www.springframework.org/ Schema/beans        http://www.springframework.org/schema/beans/spring-beans.xsd ">        <!-- Configure Hibernate5 sessionfactory--><bean name = "Localsessionfactorybean" class= " Org.springframework.orm.hibernate5.LocalSessionFactoryBean "><property name=" configlocation "value=" Classpath:hibernate.cfg.xml "></property></bean></beans>

5. The imported jar package is shown below.


6. New 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 successfully output the sessionfactory.


Implementation of the explanation:

1. Normal Mode configuration Hibernate

2. Create a container for spring, and add a reference to hibernate in the container.

3. Get in the container, note that the container is org.springframework.orm.hibernate5.LocalSessionFactoryBean but when it is removed from the container, it is the sessionfactory that it generates.

Otherwise it will produce an exception exception in thread "main" Org.springframework.beans.factory.BeanNotOfRequiredTypeException:Bean named ' Localsessionfactorybean ' must is of type [Org.springframework.orm.hibernate5.LocalSessionFactoryBean], but is Actually of type [Org.hibernate.internal.SessionFactoryImpl]


Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.

Example shows how to integrate hibernate5.0.2 and create sessionfactory in spring4.2.2

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.