Spring 中的 LocalSessionFactoryBean和LocalContainerEntityManagerFactoryBean

Spring和Hibernate整合的时候我们经常会有如下的配置代码

1,非JPA支持的配置

<!-- 配置 Hibernate 的 SessionFactory 实例: 通过 Spring 提供的 LocalSessionFactoryBean 进行配置 -->

    <!-- FacotryBean 配置的时候返回的不是本身而是返回的FactoryBean 的 getObject()方法返回的对象,此处是sessionFactory(Hibernate的类),用来创建session 进行数据库操作 -->

   我们配置LocalSessionFactoryBean 得到的是LocalSessionFactoryBean 的getObject方法返回SessionFactory,SessionFactory用来创建session来进行数据库的操作

    <bean id="sessionFactory" class="org.springframework.orm.hibernate4.LocalSessionFactoryBean">
        <!-- 配置数据源属性 -->
        <property name="dataSource" ref="dataSource"></property>
        <!-- 配置 hibernate 配置文件的位置及名称 -->
        <!--  
        <property name="configLocation" value="classpath:hibernate.cfg.xml"></property>
        -->
        <!-- 使用 hibernateProperties 属相来配置 Hibernate 原生的属性 -->
        <property name="hibernateProperties">
            <props>
                <prop key="hibernate.dialect">org.hibernate.dialect.MySQL5InnoDBDialect</prop>
                <prop key="hibernate.show_sql">true</prop>
                <prop key="hibernate.format_sql">true</prop>
                <prop key="hibernate.hbm2ddl.auto">update</prop>
            </props>
        </property>
        <!-- 配置 hibernate 映射文件的位置及名称, 可以使用通配符 -->
        <property name="mappingLocations" 

            value="classpath:com/atguigu/spring/hibernate/entities/*.hbm.xml">

       </property>

 

 

       <!-- 配置 hibernate 实体Bean的映射,如果缺少此项内容则不会创建表,创建表的过程是在生成容器的时候同时生成表 -->

       <和上边配置xml类型的实体bean二选一就可以了>

       <property name="annotatedClasses">
          <list>
                  <value>com.marshallee.entities.Account</value>
          </list>
        </property>

    </bean>

 

2,JPA支持的配置

同样我们配置.LocalContainerEntityManagerFactoryBean得到的是.LocalContainerEntityManagerFactoryBean 的getObject方法返回EntityManagerFactory,EntityManagerFactory用来创建EntityManager来进行数据库的操作

<bean id="entityManagerFactory"
        class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean">

        <property name="jpaVendorAdapter">
            <bean class="org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter">
                <property name="showSql" value="true" />
                <property name="generateDdl" value="true" />
                <property name="databasePlatform" value="${db.dialect}" />
            </bean>
        </property>
        <property name="jpaProperties">
            <props>
                <!-- base -->
                <prop key="hibernate.hbm2ddl.auto">update</prop>
                <prop key="hibernate.transaction.flush_before_completion">true</prop>
                <!-- show_sql -->
                <prop key="hibernate.show_sql">false</prop>
                <!-- cache -->
                <prop key="hibernate.cache.use_query_cache">true</prop>
                <prop key="hibernate.cache.use_second_level_cache">true</prop>
                <prop key="hibernate.cache.provider_class">net.sf.ehcache.hibernate.EhCacheProvider</prop>
                <prop key="hibernate.cache.region.factory_class">org.hibernate.cache.ehcache.SingletonEhCacheRegionFactory
                </prop>
            </props>
        </property>
    </bean>

 

 

“jpaVendorAdapter属性用于指明所使用的是哪一个厂商的JPA实现。Spring提供了多个JPA厂商适配器:

EclipseLinkJpaVendorAdapter
HibernateJpaVendorAdapter
OpenJpaVendorAdapter
TopLinkJpaVendorAdapter(在Spring 3.1版本中,已经将其废弃了)”

摘录来自: [美] Craig Walls. “Spring实战(第4版)”。 iBooks. 

 

只要记住一点当我们配置FactoryBean后,当调用getBean(beanName)的时候我们得到的是FactoryBean.getObject返回的对象,如果我们要得到FactoryBean本身则需要在调用getObject的时候加上引用'&' 如  getBean(&beanName)

这里涉及到FactoryBean 和BeanFactory 的区别参考:http://chenzehe.iteye.com/blog/1481476

 


容易混淆的地方

persistence.xml是JPA规范要求的,在应用程序管理类型的JPA中我们需要配置persistence.xml 中的数据源内容用PersistenceProvider来生成一个EntityManagerFactory,但是在“容器管理类型的JPA" 中 ”当运行在容器中时,可以使用容器(在我们的场景下是Spring)提供的信息来生成EntityManagerFactory。你可以将数据源信息配置在Spring应用上下文中,而不是在persistence.xml中了,尽管数据源还可以在persistence.xml中进行配置,但是这个属性指定的数据源具有更高的优先级.

综上可以理解为如果我们选择在spring上下文中配置数据源信息,则不需要再persistence.xml中配置了,即使配置了在容器类型的JPA中,容器会优先选择容器上下文中的配置信息,但是persistence,xml这个文件还是必须要有的,可以是空的配置,但必须要有,这是JPA的要求,

转载于:https://www.cnblogs.com/onlymate/p/7568572.html

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mzph.cn/news/391325.shtml

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

如何通过建造餐厅来了解Scala差异

I understand that type variance is not fundamental to writing Scala code. Its been more or less a year since Ive been using Scala for my day-to-day job, and honestly, Ive never had to worry much about it. 我了解类型差异并不是编写Scala代码的基础。 自从我在日…

linux的/etc/passwd、/etc/shadow、/etc/group和/etc/gshadow

1./etc/passwd 存储用户信息 [rootoldboy ~]# head /etc/passwd root:x:0:0:root:/root:/bin/bash bin:x:1:1:bin:/bin:/sbin/nologin daemon:x:2:2:daemon:/sbin:/sbin/nologin 一行记录对应着一个用户&#xff0c;每行记录被冒号:分隔为7个字段&#xff0c;这7个字段的具体含…

组织在召唤:如何免费获取一个js.org的二级域名

之前我是使用wangduanduan.github.io作为我的博客地址&#xff0c;后来觉得麻烦&#xff0c;有把博客关了。最近有想去折腾折腾。先看效果&#xff1a;wdd.js.org 如果你不了解js.org可以看看我的这篇文章:一个值得所有前端开发者关注的网站js.org 前提 已经有了github pages的…

linkedin爬虫_您应该在LinkedIn上关注的8个人

linkedin爬虫Finding great mentors are hard to come by these days. With so much information and so many opinions flooding the internet, finding an authority in a specific field can be quite tough.这些天很难找到优秀的导师。 互联网上充斥着如此众多的信息和众多…

重学TCP协议(4) 三次握手

1. 三次握手 请求端&#xff08;通常称为客户&#xff09;发送一个 S Y N段指明客户打算连接的服务器的端口&#xff0c;以及初始序号。这个S Y N段为报文段1。服务器发回包含服务器的初始序号的 S Y N报文段&#xff08;报文段2&#xff09;作为应答。同时&#xff0c;将确认序…

[设计模式]State模式

《Java与模式》 又称状态对象模式。状态模式是对象的行为模式。GOF95 一个对象的行为取决于一个或者多个动态变化的属性&#xff0c;这样的属性叫做状态。这样的对象叫做有状态的对象&#xff08;stateful&#xff09;。 状态模式把一个所研究的对象的行为包装在不同的状态对象…

java温故笔记(二)java的数组HashMap、ConcurrentHashMap、ArrayList、LinkedList

为什么80%的码农都做不了架构师&#xff1f;>>> HashMap 摘要 HashMap是Java程序员使用频率最高的用于映射(键值对)处理的数据类型。随着JDK&#xff08;Java Developmet Kit&#xff09;版本的更新&#xff0c;JDK1.8对HashMap底层的实现进行了优化&#xff0c;例…

前置交换机数据交换_我们的数据科学交换所

前置交换机数据交换The DNC Data Science team builds and manages dozens of models that support a broad range of campaign activities. Campaigns rely on these model scores to optimize contactability, volunteer recruitment, get-out-the-vote, and many other piec…

aws 弹性三剑客_AWS和弹性:超越用户需求

aws 弹性三剑客I’ll assume that, one way or another, you’re already familiar with many of AWS’s core deployment services. That means you now know about:我假设您已经熟悉许多AWS的核心部署服务。 这意味着您现在知道&#xff1a; • EC2 instances and AMIs (Ama…

leetcode 368. 最大整除子集(dp)

给你一个由 无重复 正整数组成的集合 nums &#xff0c;请你找出并返回其中最大的整除子集 answer &#xff0c;子集中每一元素对 (answer[i], answer[j]) 都应当满足&#xff1a; answer[i] % answer[j] 0 &#xff0c;或 answer[j] % answer[i] 0 如果存在多个有效解子集&a…

在Centos中安装mysql

下载mysql这里是通过安装Yum源rpm包的方式安装,所以第一步是先下载rpm包 1.打开Mysql官网 https://www.mysql.com/, 点击如图选中的按钮 点击如图框选的按钮 把页面拉倒最下面,选择对应版本下载,博主这里用的是CentOS7 下载完成后上传到服务器,由于是yum源的安装包,所以…

硕士可以跟别的导师做实验吗_如何成为一名导师可以成为双刃剑

硕士可以跟别的导师做实验吗Mentoring is the ability to give advise or train someone, often times, who is less knowledgeable in a particular field. This is pretty much common place in tech companies. There you usually have senior developers who, besides bein…

linux中权限对文件和目录的意义

1.权限对文件的意义&#xff1a; 读&#xff1a;可查看文件的内容 写&#xff1a;可修改文件的内容&#xff08;但不能删除文件&#xff09; 执行&#xff1a;可执行文件 2.权限对目录的意义&#xff1a; 读&#xff1a;可以查看目录下的内容&#xff0c;即可以读取该目录下的结…

Docker 入门(1)虚拟化和容器

1 虚拟化 虚拟化是为一些组件&#xff08;例如虚拟应用、服务器、存储和网络&#xff09;创建基于软件的&#xff08;或虚拟&#xff09;表现形式的过程。它是降低所有规模企业的 IT 开销&#xff0c;同时提高其效率和敏捷性的最有效方式。 1.1 虚拟化用于程序跨平台兼容 要…

量子相干与量子纠缠_量子分类

量子相干与量子纠缠My goal here was to build a quantum deep neural network for classification tasks, but all the effort involved in calculating errors, updating weights, training a model, and so forth turned out to be completely unnecessary. The above circu…

三角函数式的化简

前言 为什么需要化简三角函数式&#xff1f; 一、什么是三角函数式的化简&#xff1f; 二、三角函数式的化简标准是什么&#xff1f; 三、三角函数式化简可能用到的变形&#xff1a; 弦切互化&#xff0c;1的代换&#xff0c;通分约分&#xff0c;配方展开&#xff0c;提取公因…

Python -- xlrd,xlwt,xlutils 读写同一个Excel

最近开始学习python,想做做简单的自动化测试&#xff0c;需要读写excel,然后就找到了xlrd来读取Excel文件&#xff0c;使用xlwt来生成Excel文件&#xff08;可以控制Excel中单元格的格式&#xff09;&#xff0c;需要注意的是&#xff0c;用xlrd读取excel是不能对其进行操作的&…

计算机工程师分级_这些是每个计算机工程师都应该知道的数字

计算机工程师分级In 2010, Jeff Dean from Google gave a wonderful talk at Stanford that made him quite famous. In it, he discussed a few numbers that are relevant to computing systems. Then Peter Norvig published those numbers for the first time on the inter…

leetcode 377. 组合总和 Ⅳ(dp)

给你一个由 不同 整数组成的数组 nums &#xff0c;和一个目标整数 target 。请你从 nums 中找出并返回总和为 target 的元素组合的个数。 题目数据保证答案符合 32 位整数范围。 示例 1&#xff1a; 输入&#xff1a;nums [1,2,3], target 4 输出&#xff1a;7 解释&…

1.4- 定时任务总结之九句箴言

1.4定时任务之九句箴言九句箴言---- 不会九句箴言别做运维1.定时任务规则之前加注释2.使用脚本代替命令行制定定时任务3.定时任务中date命令%的特殊含义定时任务中,%表示回车 -----可以使用\转义4.运行脚本一定要用/bin/sh或sh脚本不必须有x权限5.定时任务中-命令或脚本的输出…