SPRING in ACTION 4th Edition notes-11th chapter persisting data with Object-relational mapping-002 set JPA entitymanagerfactory (< Persistence-unit>, <jee:jndi-lookup>)

Source: Internet
Author: User

I. Types of Entitymanagerfactory

1.TheJPA specification defines the kinds of entity managers:

? Application-managed-entity managers is created when an application directlyRequests one from an entity manager factory. With application-managed entitymanagers, the application is responsible for opening or closing entity managers
and involving the entity manager in transactions. This type of entity manager isMost appropriate for use in standalone applications that don ' t run in a Java EEcontainer.
? Container-managed-entity managers is created and managed by a Java EEcontainer. The application doesn ' t interact with the Entity manager factory atAll . Instead, entity managers is obtained directly through injection or from
JNDI. The container is responsible for configuring the entity manager factories.This type of entity manager is the most appropriate for use by a Java EE containerThat wants to maintain some control over JPA configuration beyond what ' s specified in Persistence.xml.

2.eachflavor of Entity Manager factory are produced by a corresponding Spring FACTory Bean:

? Localentitymanagerfactorybean produces an application-managed Entitymanagerfactory.
? Localcontainerentitymanagerfactorybean produces a container-managed entitymanagerfactory

second, the setting method

application-managed Entity-manager factories derive most of their configuration information from a configuration file called Persistence.xml. This file must appear in the meta-inf directory in the classpath. The purpose of the Ersistence.xml file is to define one or more persistence units. A persistence unit is a grouping of one or more persistent classes that correspond to a Single data source.

1.xml

1<persistence xmlns= "http://java.sun.com/xml/ns/persistence" version= "1.0" >2<persistence-unit name= "Spitterpu" >3<class>com.habuma.spittr.domain.spitter</class>4<class>com.habuma.spittr.domain.spittle</class>5<properties>6<property name= "Toplink.jdbc.driver" value= "Org.hsqldb.jdbcDriver"/>7<property name= "Toplink.jdbc.url" value= "Jdbc:hsqldb:hsql://localhost/spitter/spitter"/>8<property name= "Toplink.jdbc.user" value= "sa"/>9<property name= "Toplink.jdbc.password" value= ""/>Ten</properties> One</persistence-unit> A</persistence>

In another case,

1<?xml version= "1.0"?>2<persistence xmlns= "Http://java.sun.com/xml/ns/persistence"3Xmlns:xsi= "Http://www.w3.org/2001/XMLSchema-instance"4Xsi:schemalocation= "Http://java.sun.com/xml/ns/persistence5http//java.sun.com/xml/ns/persistence/persistence_1_0.xsd "version=" 1.0 ">6<persistence-unit name= "June" transaction-type= "resource_local" >7<provider>org.hibernate.ejb.HibernatePersistence</provider>8<properties>9<property name= "Hibernate.dialect"TenValue= "Org.hibernate.dialect.MySQLDialect"/><!--Database dialect--- One<property name= "Hibernate.connection.driver_class" AValue= "Com.mysql.jdbc.Driver"/><!--Database driver Class-- -<property name= "Hibernate.connection.username" value= "root"/><!--database user name-- -<property name= "Hibernate.connection.password" value= "admin"/> the<property name= "Hibernate.connection.url" -Value= "Jdbc:mysql://localhost:3306/quote;" /><!--Database Connection url--> -<property name= "Hibernate.max_fetch_depth" value= "3"/><!--outside the maximum depth of the fetch tree-- -<property name= "Hibernate.hbm2ddl.auto" value= "Update"/><!--Auto Output schema to create DDL statements-- +<property name= "hibernate.jdbc.fetch_size" value= "/><!--JDBC Acquisition Size-- -<property name= "hibernate.jdbc.batch_size" value= "/><!--turn on the batch update function of hibernate using JDBC2-- +<property name= "Hibernate.show_sql" value= "true"/><!--output SQL statements in the console-- A</properties> at</persistence-unit> -</persistence>

Then bean.xml as follows:

1<?xml version= "1.0" encoding= "UTF-8"?>2<beans xmlns= "Http://www.springframework.org/schema/beans"3Xmlns:xsi= "Http://www.w3.org/2001/XMLSchema-instance"4xmlns:context= "Http://www.springframework.org/schema/context"5xmlns:aop= "HTTP://WWW.SPRINGFRAMEWORK.ORG/SCHEMA/AOP"6xmlns:tx= "Http://www.springframework.org/schema/tx"7Xsi:schemalocation= "Http://www.springframework.org/schema/beans8http//www.springframework.org/schema/beans/spring-beans-2.5.xsd9http//Www.springframework.org/schema/contextTenhttp//www.springframework.org/schema/context/spring-context-2.5.xsd Onehttp//WWW.SPRINGFRAMEWORK.ORG/SCHEMA/AOPhttp://www.springframework.org/schema/aop/spring-aop-2.5.xsd Ahttp//Www.springframework.org/schema/txhttp://www.springframework.org/schema/tx/spring-tx-2.5.xsd"> -<!--configure which packages under which classes need to be automatically scanned-- -<context:component-scan base- Package= "Com.sanqing"/> the     -<!--here June to with Persistence.xml in <persistence-unit name= "June" transaction-type= "resource_local" > - to find the associated database connection by the name value in the -- +<bean id= "Entitymanagerfactory"class= "Org.springframework.orm.jpa.LocalEntityManagerFactoryBean" > -<property name= "Persistenceunitname" value= "June"/> +</bean> A<!--Configure Things Manager-- at<bean id= "TransactionManager"class= "Org.springframework.orm.jpa.JpaTransactionManager" > -<property name= "Entitymanagerfactory" ref= "Entitymanagerfactory"/> -</bean> -<!--configuration use annotations to manage things-- -<tx:annotation-driven transaction-manager= "TransactionManager"/> -      in</beans>

2.java

 1   @Bean  2  public   Localentitymanagerfactorybean Entitymanagerfactorybean () { 3  Localentitymanagerfactorybean EMFB = new   Localentitymanagerfactorybean ();  4  emfb.setpersistenceunitname ("Spitterpu" );  5  return   EMFB;  6 } 

The reason much of what goes to creating an application-managed Entity Managerfactory is contained in Persistence.xml have everything to does with what it means to be application-managed.

3.container-managed JPA takes a different approach. When running in a container, an entitymanagerfactory can is produced using information providedby the con tainer -spring, in this case. Instead of configuring Data-source details in Persistence.xml, can configure This information in the Spring application context.

1 @Bean2  PublicLocalcontainerentitymanagerfactorybean entitymanagerfactory (3 DataSource DataSource, Jpavendoradapter jpavendoradapter) {4Localcontainerentitymanagerfactorybean EMFB =5         NewLocalcontainerentitymanagerfactorybean ();6 Emfb.setdatasource (dataSource);7 Emfb.setjpavendoradapter (jpavendoradapter);8     returnEMFB;9 }Ten  One @Bean A  PublicJpavendoradapter Jpavendoradapter () { -Hibernatejpavendoradapter adapter =NewHibernatejpavendoradapter (); -Adapter.setdatabase ("HSQL"); theAdapter.setshowsql (true); -ADAPTER.SETGENERATEDDL (false); -Adapter.setdatabaseplatform ("Org.hibernate.dialect.HSQLDialect"); -     returnadapter; +}

4.Theprimary purpose of the Persistence.xml file is to identify the entity classes in a persistence Unit. But as of Spring 3.1, you can do this directly with Localcontainer Entitymanagerfactorybean bySetting the package Stoscan Property:

1 @Bean2  PublicLocalcontainerentitymanagerfactorybean entitymanagerfactory (3 DataSource DataSource, Jpavendoradapter jpavendoradapter) {4Localcontainerentitymanagerfactorybean EMFB =5         NewLocalcontainerentitymanagerfactorybean ();6 Emfb.setdatasource (dataSource);7 Emfb.setjpavendoradapter (jpavendoradapter);8Emfb.setpackagestoscan ("Com.habuma.spittr.domain");9     returnEMFB;Ten}

There's no need to configure details on the database in Persistence.xml. Therefore,there ' s no need for Persistence.xml whatsoever! Delete it, and let LocalcontainerEntitymanagerfactorybean handle it for you.

Iii. getting entitymanagerfactory from Jndi

1.xml

1 <jee:jndi-lookup id= "emf" jndi-name= "Persistence/spitterpu"/>

2.java

 1   @Bean  2  public   Jndiobjectfactorybean Entitymanagerfactory () { 3  jndiobjectfactorybean jndiobjectfb = new   Jndiobjectfactorybean ();  4  jndiobjectfb.setjndiname ("jdbc/spittrds" );  5  return   JNDIOBJECTFB;  6 } 

Although this method doesn ' t return an entitymanagerfactory, it would result in an entitymanagerfactory Bean. That's because it returns Jndiobjectfactorybean, which is a implementation of theFactorybean interface that pro Duces an Entitymanagerfactory.

SPRING in ACTION 4th Edition notes-11th chapter persisting data with Object-relational mapping-002 set JPA entitymanagerfactory (< Persistence-unit>, <jee:jndi-lookup>)

Related Article

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.