JPA Object Relational Mapping Summary (i)---persistence.xml file configuration essentials

Source: Internet
Author: User

1. <property name= "Hibernate.hbm2ddl.auto" value= "Update"/&GT;
The function represented here is: auto Create | update | Validate database table structure. If this is not the requirement, set value= "None" is recommended. There are several parameters that can be set:
Validate: Each time hibernate is loaded, validation creates a database table structure that is only compared to the tables in the database and does not create a new
Table, but the new value is inserted.
Create: Each time hibernate is loaded, the last generated table is deleted, and then the new one is generated based on your model class.
Table, even if two times without any change to do so, this is the result of database table data loss is an important reason.
Create-drop: Each time the load is generated from the entity table, sessionfactory the end to delete the table, the development of the previous table using
Update: The most commonly used property, the first time you load hibernate, the structure of the table is automatically built according to the model class (provided that the first
database), and automatically update the table structure according to the model class when you load hibernate later, even if the table structure changes but the rows in the table still
There is no previous row deleted. It is important to note that when deployed to a server, the table structure is not immediately established, it is to wait for the application
It will not work until the first time. Post-development use.


2. <persistence-unit><persistence-unit/> Persistence unit, simply put, represents a collection of entity beans, then this heap
Entity beans, we call them entity bean units. We know that we are learning hibernate and that they are specifically used to map the database
Java objects, in our JPA, these objects are called entity beans. The persistence unit is a collection of entity beans that we
The collection takes a name, <persistence-unit name= "..." &GT;&LT;PERSISTENCE-UNIT/&GT; there can be several such persistence units, that is, they deal with different databases.


3. Global transaction Local transaction
Global transactions: Resource Manager manages and coordinates transactions that can span multiple databases and processes. The resource manager typically uses the XA two phase to raise
The Enterprise Information System (EIS) or the database.
Local transaction: A transaction that is local to a single EIS or database and is restricted within a single process. A local transaction does not involve multiple data sources.

The <persistence-unit><persistence-unit/> tag also has a property, which is Transaction-type (the type of transaction), which has
Two values, respectively JTA (global transaction) and Resource_local (local transaction).

Here we are configured to Transaction-type= "Resource_local", because we only operate on a database, also say that only the needle
Operates on a transactional resource.

The types of transactions we learned previously were local transactions. What is the difference between JTA (global transaction) and Resource_local (local transaction)?
In some applications, you can use only global transactions, such as:
There are two databases:

1.mysql 2.oracle There is now a business need--transfer
Step 1> Update mysql_table set amount=amount-xx where id=aaa happens to deduct money, assuming that the MySQL database is deducted
Of
Step 2> Update oracle_table set amount=amount+xx where ID=BBB adds money, assuming the money is deducted from the Oracle database.
Now how do you make sure that two statements are executed in the same transaction?


I used to do that in JDBC.
connection = mysql connection mysql
Connection.setautocommit (FALSE); Do not submit automatically
1> Update mysql_table set amount=amount-xx where id=aaa happens to deduct money, assuming it is deducted from the MySQL database.
2> Update oracle_table Set amount=amount+xx where ID=BBB occurs in the Oracle database
Connection.commit ();
Execute these two statements, and then commit the transaction through the connection object. We do this just to make sure that both statements are in the same database MySQL
Inside the implementation within the same transaction. But the problem is that we are now connecting to the Oracle database, is it necessary to connection2?

connection = mysql connection mysql
Connection2 = Oracle Connectivity Oracle
Connection.setautocommit (FALSE); Do not submit automatically
1> Update mysql_table set amount=amount-xx where id=aaa happens to deduct money, assuming it is deducted from the MySQL database.
2> Update oracle_table Set amount=amount+xx where ID=BBB occurs in the Oracle database
Connection.commit ();
Connection2.setautocommit (FALSE);
Connection2.commit ();
Transactions can only be opened in one connection, and make sure that all two statements are executed in the connection, so that two statements can be made in the same
Transaction execution, now the problem is that Connection2 is connected to the Oracle database, so is it meaningful for Connection2 to open a transaction? It
Can you make sure? No, so in this case you can only use global transactions.
In this case, the normal JDBC operation is not sufficient for this business requirement, the business requirement can only use global transaction, the local transaction is unable to support
The lifecycle of a transaction should not be limited to the life cycle of the connection object.
What does a global transaction do?
Jpa.getusertransaction (). Begin (); First of all the global transaction API, do not need us to write, usually the container has been provided to us,
We just need to begin.
connection = mysql connection mysql
Connection2 = Oracle Connectivity Oracle
connection--> Update mysql_table set amount=amount-xx where id=aaa happens to deduct money, assuming that the MySQL data
The library buckles the money.
connection2--> Update oracle_table Set amount=amount+xx where ID=BBB occurs in the Oracle database
Jpa.getusertransaction (). commit ();
So how does it know that a transaction should be committed or rolled back?

At this time it used two commit protocols. Two commits the protocol simply says this: If you execute the first statement first, the result of the execution is pre-submitted first
To the database, pre-commit to the database, the database executes this statement, and then returns a result of the execution, if we use Boolean
A value indicates that success is true, and failure is false. Then put the results of the execution into one (assuming a list) object, and then
Executes the second statement, after executing the second statement (also preprocessing, the database does not really implement the data to commit, just say that the statement sent
Into the database, it simulates execution, gives you a result of execution, and if the results of these two statements are true in the list
, then the transaction considers the statement to be successful, and the global transaction commits. Two commit agreements, the database is submitted for the first time in this
Statement, only preprocessing is done, no real data changes occur, and when we commit to the global transaction, a second
The second time, the data changes will actually occur.

If there is an error in executing these two statements, then there is an element in the list set that is false, so the global transaction thinks you
Transaction is a failure, it will be rolled back and rolled back, even if your second statement was successful at the time of the first commit, it
When two commits are rolled back, the first change will revert to the previous state, which is the two commit protocol. (You can check
Database-related documentation to understand two commit agreements)

Back to the Persistence.xml configuration, there are two types of transactions, when should the global transaction (JTA) be used? When to use a local transaction
(resource_local)? There should be a need for your business application, and most of our applications only require local transactions. The global transaction is typically
Used in application servers, such as Weblogic,jboss. What kinds of transaction types are there? What are the scenarios for each?

Post a complete example


<?xml version= "1.0"?>
<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 is a collection of entity beans that are currently local transactions--
<persistence-unit name= "Company" transaction-type= "Resource_local" >
<provider>org.hibernate.ejb.HibernatePersistence</provider><!--Drive Implementation class, Ingress class--
<properties>

<!--Common Properties--
<property name= "Hibernate.connection.username" value= "Cnfatal"/>
<property name= "Hibernate.connection.password" value= "qapasswd"/>
<property name= "Hibernate.max_fetch_depth" value= "3"/>
<property name= "Hibernate.hbm2ddl.auto" value= "Update"/> <!--Dev When set to Update,prod should be removed in order to not exist when this table is added, when present , the test is in accordance with the entity description--
<property name= "hibernate.jdbc.fetch_size" value= "/>"
<property name= "hibernate.jdbc.batch_size" value= "/>"
<property name= "Hibernate.show_sql" value= "true"/>
<property name= "Hibernate.format_sql" value= "false"/>



<!--for MySQL, it's best to use MYSQL5D, which offers many advanced features
<property name= "Hibernate.dialect" value= "Org.hibernate.dialect.MySQL5Dialect"/>
<property name= "Hibernate.connection.driver_class" value= "Com.mysql.jdbc.Driver"/>
<property name= "Hibernate.connection.url" value= "jdbc:mysql://localhost:3306/fatals?useunicode=true&amp; Characterencoding=utf-8 "/>
-

<!--Oracle 10g conf---
<property name= "Hibernate.dialect" value= "Org.hibernate.dialect.Oracle10gDialect"/>
<property name= "Hibernate.connection.driver_class" value= "Oracle.jdbc.driver.OracleDriver"/>
<property name= "Hibernate.connection.url" value= "Jdbc:oracle:thin: @localhost: 15003:QAPP1CN"/>





<!--connection Pool default
<property name= "Hibernate.connection.driver_class" value= "Org.gjt.mm.mysql.Driver"/>--





<property name= "Hibernate.connection.provider_class" value= "Org.hibernate.connection.C3P0ConnectionProvider"/ >
<property name= "C3p0.min_size" value= "5"/>
<property name= "c3p0.max_size" value= "/>"
<property name= "c3p0.maxidletime" value= "/>"
<property name= "c3p0.timeout" value= "1800"/>
<property name= "c3p0.max_statements" value= "/>"
<property name= "c3p0.idle_test_period" value= "/>"
<property name= "C3p0.acquire_increment" value= "1"/>
<property name= "C3p0.validate" value= "false"/>
<property name= "C3p0.preferredtestquery" value= "SELECT 1 from Dual"/>
<property name= "C3p0.testconnectiononcheckout" value= "true"/>

</properties>
</persistence-unit>
</persistence>

JPA Object Relational Mapping Summary (i)---persistence.xml file configuration essentials

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.