Spring + hibernate + JPA configuration

Source: Internet
Author: User

Recently, we are interested in the JPA implementations of Hibernate, which are documented in the configuration method.


First, MAVEN relies on package configuration, where spring3.1.2 and hibernate3.6.0 are used.

<dependencies>                 <dependency>            <groupid >org.hibernate</groupId>            < artifactid>hibernate-entitymanager</artifactid>             <version>3.6.0.Final</version>         </dependency>        <dependency>             <groupId>org.springframework</groupId>             <artifactId>spring-webmvc</artifactId>             <version>3.1.2.release</ version>        </dependency>        < Dependency>            <groupid> org.springframework</groupid>             <artifactId>spring-orm</artifactId>             <version>3.1.2.release</version>        </ dependency>        <dependency>             <groupId>com.microsoft.sqlserver</groupId>             <artifactId>sqljdbc</artifactId>             <version>4.0</version>         </dependency>        <dependency>             <groupId>org.slf4j</groupId>             <artifactid>slf4j-log4j12</ artifactid>            <version>1.6.1< /version>        </dependency>         <dependency></dependencies>

The

is then the spring configuration Applicationcontext.xml, it is important to note that the data source here is Jndi, which is recommended in a production environment, rather than the properties file, because the code provided to the implementers is a variety of war packages, and it is not possible for others to peel off your war package to modify the properties. Instead, it directly changes the data source configuration of the middleware. Some other configurations, such as paths, are best left to the container to configure, rather than placing frequently-changed configuration files in the project.

    <jee:jndi-lookup id= "MSSQL"  jndi-name= "Jdbc/test"  />            <bean id= "EMF"          class= "Org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean" >         <property name= "DataSource"  ref= "MSSQL" ></property>                 <property  name= "Persistenceunitname"  value= "Sample" ></property>             </bean>    <bean id= "TransactionManager"  class= "Org.springframework.orm.jpa.JpaTransactionManager" >         <property name= "Entitymanagerfactory"  ref= "EMF"  />             </bean>    <mvc:annotation-driven />     <tx:annotation-driven />

Finally is the JPA configuration class path under Meta-inf/persistence.xml, multiple database dialects, please configure multiple units!

<?xml version= "1.0"  encoding= "UTF-8"? ><persistence xmlns= "http://java.sun.com/xml/ns/ Persistence "    xmlns:xsi=" Http://www.w3.org/2001/XMLSchema-instance "     xsi:schemalocation= "http://java.sun.com/xml/ns/persistence http://java.sun.com/xml/ns/persistence/ Persistence_2_0.xsd "    version=" 2.0 ">    <persistence-unit  name= "Sample"  transaction-type= "Resource_local" >         <provider>org.hibernate.ejb.HibernatePersistence</provider>         <properties>            < Property name= "Hibernate.dialect"                  value= "Org.hibernatespatial.sqlserver.SQLServerSpatialDialect"  />       &nbsP;     <property name= "Hibernate.show_sql"  value= "true"  />             <property name= " Hibernate.hbm2ddl.auto " value=" Update " />        </ Properties>    </persistence-unit></persistence>

Inject the JPA Entitymanager object into the Java bean, take DAO as an example, so that you can properly integrate JPA in spring

@Transactionalpublic Abstract class Basedao<t> implements ibasedao<t> {@PersistenceContext protected En        Titymanager em; ...}


Finally, on the understanding of the individual, JPA is more conducive to ORM development than Sessionfactory, which is unique to hibernate. The most recently viewed Graniteds is a more recommended way to do ORM using JPA. In the end is not good, but also need to follow up the accumulation to verify.

The purpose of doing ORM is mainly one, that is, the code is simple and flexible. At present, the company is still using spring jdbctemplate, it is relatively tedious to manage SQL statements, do CRUD operations and generate objects or collections. And when the amount of engineering is large, the maintenance of SQL statements is a troublesome thing. Not as fast as hibernate.

Spring + hibernate + JPA configuration

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.