JPA learning-Section 3: Introduction to building a JPA development environment and global transactions

Source: Internet
Author: User

1. Create a Java Project

1. Import the required jar package:

2. Create a persistence. xml file with the following code:

<? XML version = "1.0" encoding = "UTF-8"?>
<Persistence version = "2.0"
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>
<Persistence-unit name = "learn_jpa" transaction-type = "resource_local">
<Provider> org. hibernate. EJB. hibernatepersistence </provider>

<Properties>
<! -- Database Dialect -->
<Property name = "hibernate. dialect" value = "org. hibernate. dialect. mysql5dialect"/>
<! -- Database-driven -->
<Property name = "hibernate. Connection. driver_class" value = "com. MySQL. JDBC. Driver"/>
<! -- Database username -->
<Property name = "hibernate. Connection. username" value = "root"/>
<! -- Database Password -->
<Property name = "hibernate. Connection. Password" value = "hwl901121"/>
<! -- Database connection URL -->
<Property name = "hibernate. Connection. url" value = "JDBC: mysql: // localhost: 3306/learn_jpa? Useunicode = true & amp; characterencoding = utf8 "/>
<! -- Maximum capture depth -->
<Property name = "hibernate. max_fetch_depth" value = "3"/>
<! -- Create a database table by updating -->
<Property name = "hibernate. hbm2ddl. Auto" value = "Update"/>
<! -- Display SQL -->
<Property name = "hibernate. show_ SQL" value = "false"/>
<! -- Format SQL -->
<Property name = "hibernate. format_ SQL" value = "true"/>
</Properties>
</Persistence-unit>
</Persistence>

Details:

Persistence-unit: persistence unit. To put it simply, it represents a collection of entity beans. Then, these entity beans are called Entity Bean units. We have learned from Hibernate that they are a general application dedicated to database ing.
Java objects. In our JPA, these objects are called object beans. A persistence unit is a collection of entity beans. We can name the collection. <persistence-unit name = "... "> <persistence-unit/>, there can be several such persistence units, that is, they will deal with different databases.

The name attribute is used to define the name Of the persistence unit (name is required, and the null value is also valid); transaction-type specifies the transaction type (optional resource_local or JTA)

Hibernate. hbm2ddl. Auto:

The parameter is mainly used for: automatic creation | update | verification of Database Table Structure

Hibernate. hbm2ddl. Auto value:

Create: each time Hibernate is loaded, the last generated table will be deleted, and then the new table will be re-generated based on your model class, even if there are no changes twice, it will be executed like this, this is an important cause of data loss in database tables.
Create-drop: each time Hibernate is loaded, a table is generated based on the model class. However, when sessionfactory is disabled, the table is automatically deleted.
UPDATE: The most common attribute. When Hibernate is loaded for the first time, the structure of the table is automatically created based on the model class (the premise is that the database is created first). When Hibernate is loaded, the table structure is automatically updated based on the model class, even if the table structure is changed, the row in the table still does not delete the previous row. Note that after the application is deployed to the server, the table structure will not be created immediately. It will not be created until the application runs for the first time.
Validate: each time Hibernate is loaded, the system verifies the structure of the created database table and only compares it with the table in the database. A new table is not created, but a new value is inserted.

Ii. Differences between resource_local and JTA management transactions:

1. JTA transaction (Java transaction API) is the transaction standard in J2EE specifications. It is a container-level transaction and can only run on a J2EE server. Its biggest advantage is that it supports distributed transactions. If the system uses a distributed database, you can only choose JTA to manage entitymanager transactions.

When using JTA to manage entitymanager transactions, pay attention to the following issues.

-JTA transactions can only run in the J2EE environment, that is, the EJB container and the Web Container. In the j2se environment, you can only use resource_local to manage transactions.

-The entitymanager object hosted by the container can only use JTA transactions, rather than resource_local transactions.

2. resource_local transaction local transaction of the database. It is a database-level transaction. It can only be used for one type of database and does not support distributed transactions. For small and medium-sized applications, resource_local can be used to manage entitymanager transactions.

Pay attention to the following issues when using resource_local to manage entitymanager transactions.

-In the j2se environment, you can only use resource_local to manage entitymanager transactions, and the entitymanager object is obtained through application hosting.

-When Using resource_local in the code to manage transactions, you must call the gettransac-tion () method of entitymanager to obtain the local transaction object.

Http://brushupo.blog.sohu.com/95340446.html

JPA learning-Section 3: Introduction to building a JPA development environment and global transactions

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.