JPA Learning Note-spring Integrated JPA

Source: Internet
Author: User
Tags throw exception

Spring Consolidated JPA

Three ways to integrate:

-localentitymanagerfactorybean: For projects that use JPA for data access only, the Factorybean will work according to the JPA Persistenceprovider Auto-detect configuration file, typically from the " Meta-inf/persistence.xml "reads configuration information in the simplest way, but cannot set DataSource defined in spring and does not support spring-managed global transactions

-Obtained from Jndi: used to obtain the specified entitymanagerfactory from the Java EE server, which is typically used for JTA transaction management in Spring transaction management

-localcontainerentitymanagerfactorybean: Factorybean for all environments with full control of entitymanagerfactory configuration, such as specifying Spring-defined DataSource Wait a minute.


Directory structure

650) this.width=650; "src=" Http://s3.51cto.com/wyfs02/M02/75/21/wKiom1YzDouTzwslAAC2tMz38P0014.jpg "title=" Sping.jpg "alt=" Wkiom1yzdoutzwslaac2tmz38p0014.jpg "/>


Applicationcontext.xml

<?xml version= "1.0"  encoding= "UTF-8"? ><beans xmlns= "http://www.springframework.org/ Schema/beans "xmlns:xsi=" http://www.w3.org/2001/XMLSchema-instance " xmlns:context="/HTTP/ Www.springframework.org/schema/context " xmlns:tx=" Http://www.springframework.org/schema/tx "xsi: Schemalocation= "http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/ spring-beans.xsd    http://www.springframework.org/schema/context      http://www.springframework.org/schema/context/spring-context-4.1.xsd    http:// www.springframework.org/schema/tx     http://www.springframework.org/schema/tx/ Spring-tx-4.0.xsd ><!--  enable annotation injection  --><!-- <context:annotation-config />  --><!--  Component Scan  --><context:component-scan base-package= "COM.JPA" ></ context:component-scan><!--  Import resource file  --><!-- <context:property-placeholder location= "Classpath:db.properties"  /> --><!--   Configure C3P0 data source  --><bean id= "DataSource"  class= "Com.mchange.v2.c3p0.ComboPooledDataSource "><property name=" user " value=" root "></property><property name=" password "  value= "" ></property><property name= "Driverclass"  value= "Com.mysql.jdbc.Driver" > </property><property name= "Jdbcurl"  value= "Jdbc:mysql:///test" ></property>< Property name= "Initialpoolsize"  value= "5" ></property><property name= "MaxPoolSize"  value= ></property></bean><!--  Configuration Entitymanagerfactory --><bean  id= "Entitymanagerfactory"  class= "Org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean "><property name=" DataSource " ref=" DataSource "></property><!--  Configuration &NBSP;JPA The   provider's adapter .  can be &n via the internalbsp;bean  Way to configure  --><property name= "Jpavendoradapter" ><bean class= " Org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter "></bean></property><!--  Configure the package that contains the entity class  --><property name= "Packagestoscan"  value= "Com.jpa.entities" ></property ><!--  Configuration  JPA  Basic properties .  such as  JPA  implementing product Properties  --><property name= " Jpaproperties "><props><prop key=" Hibernate.show_sql ">true</prop><prop key=" Hibernate.format_sql ">true</prop><prop key=" Hibernate.hbm2ddl.auto ">update</prop> </props></property></bean><!--  Configuration  JPA  using the transaction manager  --><bean id = "TransactionManager" class= "Org.springframework.orm.jpa.JpaTransactionManager" ><property name= " Entitymanagerfactory " ref=" entitymanagerfactory "></property></bean><!--  Configuration support based on annotations is a transactional configuration  --><tx:annotation-driven transaction-manager= "TransactionManager"/></beans> 


Person.java entity class

@Table (name = "Jpa_persons") @Entitypublic class Person {@GeneratedValue @idprivate Integer ID; @Column (name = "Last_Name") ) private string Lastname;private string email;private int Age;//getter Setter}


Persondao

@Repositorypublic class Persondao {//How to get the Entitymanager object associated with the current transaction? Tag member variables with @persistencecontext annotations @persistencecontextprivate entitymanager entitymanager;public void Save (person person) {entitymanager.persist (person);}}


Personservice

@Servicepublic class Personservice {@Autowiredprivate Persondao Persondao; @Transactionalpublic void Save (person P1, Person P2) {Persondao.save (p1); int i = 10/0;//throw exception, test transaction persondao.save (p2);}}


Test

Public class jpatest {private personservice personservice = null;private  ApplicationContext applicationContext = null; {Applicationcontext = new classpathxmlapplicationcontext ("applicationcontext.xml"); Personservice = applicationcontext.getbean (Personservice.class);} @Testpublic  void testdatasource ()  {DataSource dataSource =  Applicationcontext.getbean (Datasource.class); Try {system.out.println (Datasource.getconnection ());}  catch  (sqlexception e)  {// TODO Auto-generated catch  Blocke.printstacktrace ();}} @Testpublic  void testpersonservice ()  {person p1 = new person ();p 1.setAge (21 );p 1.setEmail ("[email protected]");p 1.setLastName ("SS"); Person p2 = new person ();p 2.setAge (;p 2.setEmail ("[email protected]"); P2.setlastname ("DD"); System.out.println (Personservice.getclass (). GetName ());p Ersonservice.save (P1,&NBSP;P2);}} 


Source: Http://yunpan.cn/cFzde9uLeFLJB Access Password 1aeb


This article is from "Avatar" blog, please make sure to keep this source http://shamrock.blog.51cto.com/2079212/1708072

JPA Learning Note-spring Integrated JPA

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.