Using JPA to build the ssh (sturts2_spring_hibernate) framework instead of hibernate, springjpahibernate

Source: Internet
Author: User

Using JPA to build the ssh (sturts2_spring_hibernate) framework instead of hibernate, springjpahibernate

I. JPA is used to replace hibernate

   The full name of runtime JPA is JAVA Persistence API. JPA describes the ing between objects and Relational Tables through annotations or XML, and persists the running object in the database.

Runtime JPA is a specification and hibernate is an implementation. The same mybatis is also an implementation in jpa.

 

2. Build Steps:

   Creating a persistence. xml file:

(1) first create a MATA-INF folder under the src directory in your project, the folder name cannot be arbitrary, must be MATA-INF:

(2) create an xml file named persistence. xml:

(3) configure the persistence. xml file as follows:

<? Xml version = "1.0" encoding = "UTF-8"?> <Persistence xmlns = "http://xmlns.jcp.org/xml/ns/persistence" xmlns: xsi = "http://www.w3.org/2001/XMLSchema-instance" xsi: schemaLocation = "http://xmlns.jcp.org/xml/ns/persistence http://xmlns.jcp.org/xml/ns/persistence/persistence_2_1.xsd" version = "2.1"> <! -- Persistence unit. The name attribute is indispensable. --> <Persistence-unit name = "myJpa" transaction-type = "RESOURCE_LOCAL"> <! -- Optional attribute: Set the persistence provider --> <provider> org. hibernate. jpa. HibernatePersistenceProvider </provider> <! -- Optional attribute: entity class --> <class> entity. BookCard </class> <! -- Database vendor Attributes --> <properties> <property name = "hibernate. dialect "value =" org. hibernate. dialect. SQLServer2008Dialect "/> <property name =" hibernate. connection. driver_class "value =" com. microsoft. sqlserver. jdbc. SQLServerDriver "/> <property name =" hibernate. connection. username "value =" sa "/> <property name =" hibernate. connection. password "value =" 123456 "/> <property name =" hibernate. connection. url "value =" jdbc: sqlserver: // localhost: 1433; DatabaseName = webLastExam_20160727 "/> <property name =" hibernate. show_ SQL "value =" true "> </property> <property name =" hibernate. hbm2ddl. auto "value =" update "/> </properties> </persistence-unit> </persistence>

·Name: The name of the persistence unit. This attribute must be included and used for generating the Instance name in EntityManagerFactory.

·Transaction-type: The physical type used. The default value is RESOURCE_LOCAL, and a JTA transaction.

·Provide: An Implementation class of EJB Persistence provide, which can be divided into different ORM types, such as MyBatis and Hibernate.

·Properties: Configure the attributes of the database vendor, such as SQL-server, mysql, and Oracle.

·Class: Indicates the class to be mapped. If you do not write, you cannot automatically create tables in the database when the program is running.

 

    Annotations are added to the object class:

     (1,Add Annotation on ing object class:

           

@ Entity: identifies this class as a JPA Entity

@ Table (name = "BookCard"): Specifies the name of the generated Table in the Database Based on this entity class.

(2,Set the primary key:

           

@ Id: Set the primary key of the database table to Id.

@ GeneratedValue: the custom primary key generation policy is implemented by default.

@ GenericGeneratorHibernate: it is extended based on JPA. hibernate has its own primary key generation policy.

   

         

(3,Get the definition in the data class:(Red indicates the important code)

      

Package dao; import java. util. list; import javax. persistence. entityManager; import javax. persistence. entityManagerFactory; import javax. persistence. persistence; import javax. persistence. query; import entity. bookCard; // defines the implementation class of the database operation interface to implement the methods in the interface. Public class MyDaoImp implements MyDao {// method for obtaining all data in the interface public List <BookCard> getAllBookCard (){EntityManagerFactory emf = Persistence. createEntityManagerFactory ("myJpa"); EntityManager em = emf. createEntityManager (); em = emf. createEntityManager (); em. getTransaction (). begin ();// Query dataList <BookCard> list = em. createQuery ("from BookCard", BookCard. class ). getResultList (); em. getTransaction (). commit (); em. close (); emf. close ();Return list ;}}

 

       From the above we can find that the EntityManagerFactory in JPA is similar to SessionFactory in Hibernate; EntityManager is similar to Session, and EntityManager is the entity class manager, which is created by EntityManagerFactory. Every EntityManagerFactory is called a persistent unit, each persistence unit can be considered as a data source ing (the data source can be understood as a database. We can configure different data sources on the application server, that is, use different persistenceUtil to map these data sources, in this way, operations can be performed between different databases .)      

 

 

      Thank you!

    

 

    

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.