JPA Development Summary < one >

Source: Internet
Author: User

What is JPA

The JPA (Java Persistence API) is Sun's official Java Persistence specification, which provides Java developers with an object/relational mapping tool to manage relational data in Java applications. Its appearance is mainly to simplify the existing persistent development work and integrate ORM technology, to end now hibernate, TopLink, JDO and other ORM framework respectively as the camp situation. It is worth noting that JPA is developed on the basis of fully absorbing the existing hibernate, TopLink, JDO and other ORM frameworks (JPA development is the founder of Hibernate), has the advantages of ease of use and strong scalability, and from the reaction of the current development community, JPA received great support and praise, including the spring and EJB3.0 development team. Looking at the technology trend for the next few years, JPA should not be difficult to achieve as a standardised integrator of the ORM domain, and the existing cloud computing framework requires JPA consolidation because the product database operations of each large company are different and are in need of this specification.

The overall idea of JPA is broadly consistent with existing ORM frameworks such as Hibernate, TopLink, JDO, and so on.

In general, the JPA consists of 3 aspects (as mentioned in the previous article):

1. ORM Mapping meta-data

JPA supports the form of XML and JDK5.0 annotations or annotated two metadata, which describes the mapping between objects and tables, and the framework then persists the entity objects in the database.

2. Java Persistence API

Used to manipulate entity objects, perform CRUD operations , and the framework will do everything for us in the background, and developers can get out of the tedious JDBC and SQL code.

3. Query Language

This is an important aspect of persistent operations, querying data through object-oriented rather than database-oriented query language to avoid tightly coupling the SQL statements of the program.

Here's a hint:JPA is not a new ORM framework, it's only used to standardize existing ORM technologies , and he can't replace existing ORM frameworks such as Hibernate, TopLink, JDO, and so on. Instead, we will still use these ORM frameworks at the time of JPA development, where the application developed at this time is not dependent on a persistence provider (the framework that implements the JPA specification is its product, including hibernate). Applications can run in any JPA environment without modifying the code, truly low-coupling, extensible programming.

to implement JPA hibernate development as an example

This example part of the code I will be placed in the common tool class, convenient for later use directly, at the same time, ORM Mapping I use annotations to do, XML configuration of their own development projects using too much, it is time to change a new technology implementation.

The first is to develop a JPA-dependent jar package, I upload 14 common jar packages to my resources, interested students download it themselves, and then the new Java Project Project, The JPA specification requires that persistence.xml be placed under the Meta-inf directory under the Classpath, and the file name is fixed, and the configuration template is as follows:

<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_1_0.xsd "version=" 1.0 "> <!--Persistence unit name Name=jpatest1--<persistence-unit name=" JP Atest1 "transaction-type=" resource_local "> <properties><property name=" hibernate.dialect "value=" Org.hibernate.dialect.MySQL5Dialect "/><property name=" hibernate.max_fetch_depth "value=" 3 "/>< Property Name= "Hibernate.connection.driver_class" value= "Org.gjt.mm.mysql.Driver"/><property name= " Hibernate.connection.username "value=" root "/><property name=" Hibernate.connection.password "value=" 123456 "/ ><property name= "Hibernate.connection.url" value= "jdbc:mysql://localhost:3306/jpa?useunicode=true& Characterencoding=utf-8 "/><property name=" Hibernate.show_sql "value=" true "/><property name=" Hibernate.hbm2ddL.auto "value=" Update "/><property name=" Hibernate.format_sql "value=" true "/></properties> </ Persistence-unit></persistence>

project Development Two common ways of development (we support the latter)1. First build the table, then configure the configuration file and entity bean according to the table, the developers who use this scheme are affected by the traditional database modeling, but are used by most people, but not oop
2. Write the configuration file and the entity Bean first, and then in the build table, the developers using this scheme are using the domain modeling idea, which is more oop than the previous idea, but the novice is prone to problems.
transaction configuration in Persistence.xml: We know there are two types of transactions (transaction-type): Local Transactions (RE source_local) and global Transactions (JTA)
Generally we use local transactions, and when to use global transactions that is, when the two connections or objects simultaneously operation, need to use, see the following example, 1 banks to 2 bank transfers, This involves the reduction of 1 bank operations, 2 bank plus operation:
1.mysql  2.oracle
Transfer
1>update MySQL set amount=amount-xx where id=xxx (MySQL);
1>update MySQL set amount=amount+xx where ID=YYY (Oracle); The problem with the
is that we create two connection connnection, and the two statements above are executed in different connections, how to control the commits, and how to determine the result of the submission, which requires that the global transaction be used. The
Global transaction API is provided by JTA, see how the code is implemented,
Jta.getusertransation (). Begin ();
connection = MySQL;
Connection2 = Oracle;
Connection->update MySQL set amount=amount-xx where id=xxx (MySQL);
Connection2->update MySQL set amount=amount+xx where ID=YYY (Oracle);
Jta.getusertransation (). Summit ();
During this period, uses two commit protocols, which simply means that when the first statement is executed, it is pre-committed to the database , which commits the returned Booblean data to the list, The global commit is then only true, and if any of them is false, the transaction is not actually committed, but the data is rolled back and the data is restored to its original state.
The following specific development code I will open another article in detail, the content explained here.

JPA Development Summary < one >

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.