Create a JPA project 1. New Java Project
In fact, you can create a new JPA project directly, because I have a problem creating a JPA project here, so I created the Java project, the effect is the same. The difference is that the JPA project helps us create the JPA configuration file, and Java Engineering wants us to create it manually
If you are creating a JPA project directly, version please select 2.0
2. Create a Lib folder
Create a new Lib folder, add a jar package, and append to the Classpath
3. Create a new configuration file
Create a folder Meta-inf under SRC, and create a persistence.xml under this folder
Configure Persistence.xml (pre-built database)
<?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="JPA" transaction-type="RESOURCE _local "> <!--configuration with what ORM Framework 1. Actually configuring the implementation class for the Javax.persistence.spi.PersistenceProvider interface 2. If there is only one JP in the JPA project The implementation of a product, you can not configure the node-- <provider>Org.hibernate.ejb.HibernatePersistence</provider> <properties> <!--Configuring data source information -- < property name= "javax.persistence.jdbc.driver" value=" Com.mysql.jdbc.Driver "/> < property name="Javax.persistence.jdbc.url" value="jdbc:mysql:/// JPA "/> < property name="Javax.persistence.jdbc.user" value="root" /> < property name="Javax.persistence.jdbc.password" value= ""/> <!--Configure JPA to implement the properties of the product, namely Hibernate properties -- < property name="Hibernate.format_sql" value="true"/><!--whether to format SQL statements -- < property name="Hibernate.show_sql" value="true"/> <!--whether to print SQL statements in the console -- < property name="Hibernate.hbm2ddl.auto" value="Update" /> </Properties> </persistence-unit></Persistence>
4. Create an entity class
PackageCom.jpa.helloworld;ImportJavax.persistence.Column;Importjavax.persistence.Entity;ImportJavax.persistence.GeneratedValue;ImportJavax.persistence.GenerationType;ImportJavax.persistence.Id;Importjavax.persistence.Table;@Table(name="USER")@Entity Public class User { PrivateInteger ID;PrivateString name;PrivateString email;@Column(name="ID")@GeneratedValue(Strategy=generationtype.auto)@Id PublicIntegergetId() {returnId } Public void setId(Integer ID) { This. id = ID; }@Column(name="NAME") PublicStringGetName() {returnName } Public void SetName(String name) { This. name = name; }@Column(name="EMAIL") PublicStringGetemail() {returnEmail } Public void Setemail(String email) { This. email = email; }}
Annotations:
@Entity: Indicates that this is an entity class
@Table: Table name corresponding to database
@Id: Indicate that this is a primary key (for Get method)
@Column: This property corresponds to the field name in the database table, and if the names of the two are the same, they may not be written (for Get methods)
@GeneratedValue (Strategy=generationtype.auto) indicates the primary key self-growth (for Get method)
Later blogs will have more explanations of the annotations
5. Adding entity classes to Persistence.xml
<!-- 添加持久化类 --><class>com.jpa.helloworld.User</class>
The complete configuration file is as follows:
<?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="JPA" transaction-type="RESOURCE _local "> <!--configuration with what ORM Framework 1. Actually configuring the implementation class for the Javax.persistence.spi.PersistenceProvider interface 2. If there is only one JP in the JPA project The implementation of a product, you can not configure the node-- <provider>Org.hibernate.ejb.HibernatePersistence</provider> <!--Add Persistence class -- <class>Com.jpa.helloworld.User</class> <properties> <!--Configuring data source information -- < property name= "javax.persistence.jdbc.driver" value=" Com.mysql.jdbc.Driver "/> < property name="Javax.persistence.jdbc.url" value="Jdbc:mysql :///jpa "/> < property name= "javax.persistence.jdbc.user" value=" Root "/> < property name= "javax.persistence.jdbc.password" value=""/ > <!--Configure JPA to implement the properties of the product, namely Hibernate properties -- < property name="Hibernate.format_sql" value="true"/ ><!--whether to format SQL statements -- < property name="Hibernate.show_sql" value="true"/ > <!--whether to print SQL statements in the console -- < property name="Hibernate.hbm2ddl.auto" value="Update"/> </Properties> </persistence-unit></Persistence>
7. Testing
PackageCom.jpa.helloworld;ImportJavax.persistence.EntityManager;ImportJavax.persistence.EntityManagerFactory;ImportJavax.persistence.EntityTransaction;ImportJavax.persistence.Persistence; Public class Main { Public Static void Main(string[] args) {//1. Creating EntitymanagerfactoryString Persistenceunitname ="JPA"; Entitymanagerfactory factory = Persistence.createentitymanagerfactory (Persistenceunitname);//2. Creating EntitymanagerEntitymanager Entitymanager = Factory.createentitymanager ();//3. Opening a transactionEntitytransaction transaction = Entitymanager.gettransaction (); Transaction.begin ();//4. Persistent OperationsUser User =NewUser (); User.setname ("Tom"); User.setemail ("[email protected]");//Add user to database, equivalent to Hibernate's Save ();Entitymanager.persist (user);//5. Committing a transactionTransaction.commit ();//6. Close EntitymanagerEntitymanager.close ();//7. Close EntitymanagerfactoryFactory.close (); }}
Attention:
String persistenceunitname = "JPA"; This is going to be with persistence.xml .
<persistence-unit name="jpa" transaction-type="RESOURCE_LOCAL">consistent (can be modified)
8. Results
In fact, before the test, I did not create the user table in the database. After running the main method, it automatically helps me create a user table based on the user entity class.
Some people may think that the main method to write so much, very troublesome. In fact, any ORM framework is like this. As long as spring is integrated in the back, it is much more convenient.
The complete project structure is as follows:
Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.
JPA Learning Notes (2)--Create a JPA project