JPA (3): JPA + Hibernate basic learning, jpahibernate

Source: Internet
Author: User

JPA (3): JPA + Hibernate basic learning, jpahibernate

In the first article of this series, we have mentioned JPA and Hibernate. Is the role of the two in the system architecture:


We can draw two conclusions from a piece of film: first, the main role of JPA is persistence operations; secondly, JPA is only a specification, and it requires an implementation, as shown in, Hibernate, oPenJPA, and so on. Simply put, it can be said that JPA is only a set of interfaces and cannot accomplish anything.
The main content of this blog post is a summary of JPA and Hibernate learning. First, let's take a look at the simplest getting started demo.

Jar package:

Hibernate3.jar

Hibernate-cglib-repack-2.1_3.jar

Slf4j-api-1.5.2.jar

Javassist-3.4.GA.jar

Jta-1.1.jar

Antlr-2.7.6.jar

Commons-collections-3.1.jar

Dom4j-1.6.1.jar

Ejb3-persistence.jar

Hibernate-annotations.jar

Hibernate-commons-annotations.jar

Hibernate-entitymanager.jar

Log4j. jar

Slf4j-log4j12.jar

 

Create object class:

 

/*************************************** * ********************************* Module: buildType. java * Author: Jones * Description: Defines the Class BuildType * Date: december 2, 2014 20:56:11 ************************************** * *******************************/package com. tgb. itoo. basic. entity; import java. util. hashSet; import java. util. set; import javax. persistence. cascadeType; import javax. persistence. column; import javax. persistence. entity; import javax. persistence. generatedValue; import javax. persistence. id; import javax. persistence. onetovel; import javax. persistence. table; import org. codehaus. jackson. annotate. jsonIgnore; import org. hibernate. annotations. genericGenerator;/*** building type **/@ Entity @ Table (name = "TB_BuildType") // by default, the Table name is created based on the object class name, name: you can modify the table name public class BuildType extends EntityDelete {@ Id @ GenericGenerator (name = "uuidGenerator", strategy = "com. tgb. itoo. base. util. uuid. base58UuidGenerator ") @ GeneratedValue (generator =" uuidGenerator ") @ Column (name =" id ", length = 22) private String id; /*** type code **/@ Column (name = "buildTypeCode", length = 255) private String buildTypeCode; /*** type name */@ Column (name = "buildTypeName", length = 255) private String buildTypeName; /*** building information in the building type */@ JsonIgnore @ onetoetype (cascade = CascadeType. ALL, mappedBy = "buildType", targetEntity = Build. class) private Set <Build> builds = new HashSet <Build> (); // ************************** get/set Method ********* * ********************* // ***** building information in the building type */public Set <Build> getBuilds () {return builds;}/*** building information in the building type */public void setBuilds (Set <Build> builds) {this. builds = builds;}/*** type code */public String getBuildTypeCode () {return buildTypeCode;}/*** type code */public void setBuildTypeCode (String buildTypeCode) {this. buildTypeCode = buildTypeCode;}/*** type name */public String getBuildTypeName () {return buildTypeName;}/*** type name */public void setBuildTypeName (String buildTypeName) {this. buildTypeName = buildTypeName;}/*** primary key id */public String getId () {return id;} public void setId (String id) {this. id = id ;}}

In the primary key generation policy @ GeneratedValue (strategy = GenerationType. AUTO:

(1) If the value is AUTO, the policy is automatically selected by Hibernate based on the database. You can also omit @ GeneratedValue.

(2) If the value is "IDENTITY", the primary key automatically increases.

(3) If the value is SEQUENCE, the primary key adopts the SEQUENCE method.

(4) The value is that TABLE is common to all databases, but the efficiency is low.

 

After creating an object class, you need to write the JPA configuration file. The JPA specification requires that the configuration file be placed under the META-INF directory of the class path with a fixed name, namely persistence. xml

<? Xml version = "1.0" encoding = "UTF-8"?> <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, transaction-type the transaction types include global transaction type JTA and local transaction type RESOURCE_LOCAL --> <persistence-unit name = "basic-entity" transaction-type = "JTA"> <provider> org. hibernate. ejb. hibernatePersistence </provider> <jta-data-source> java: jboss/datasources/JcMysqlDS </jta-data-source> <properties> <! -- Database Dialect --> <property name = "hibernate. dialect "value =" org. hibernate. dialect. mySQLDialect "/> <property name =" hibernate. hbm2ddl. auto "value =" update "/> <property name =" hibernate. show_ SQL "value =" true "/> </properties> </persistence-unit> </persistence>

<Property name = "hibernate. hbm2ddl. auto "value =" update "/> indicates the table creation method. If the value is creat-drop, the table is created when the application is created, and the table is automatically deleted when the application ends; if the value is update, the table is created if the ing metadata does not exist. If the ing metadata exists and a new field is added, it is added to the database table.


Test class:

Public class PersonTest {@ BeforeClass public static void setUpBeforeClass () throws Exception {}@ Test public void save () {// Persistence. createEntityManagerFactory ("jpa") must be the same as the persistent unit name in the configuration file. EntityManagerFactory factory = Persistence. createEntityManagerFactory ("jpa"); EntityManager em = factory. createEntityManager (); em. getTransaction (). begin (); // save (persistence) method em. persist (new Person ("Tom"); em. getTransaction (). commit (); em. close (); factory. close ();}}

EntityManagerFactory is equivalent to sessionFactory in Hibernate, and EntityManager is equivalent to session in Hibernate. However, it is worth noting that there are two different ways to obtain EntityManager. One is @ PersistenceContex injection, and the other is obtained by JNDI.

With this simple JPA + Hibernate, you can perform database persistence operations. You understand?

Related Article

E-Commerce Solutions

Leverage the same tools powering the Alibaba Ecosystem

Learn more >

Apsara Conference 2019

The Rise of Data Intelligence, September 25th - 27th, Hangzhou, China

Learn more >

Alibaba Cloud Free Trial

Learn and experience the power of Alibaba Cloud with a free trial worth $300-1200 USD

Learn more >

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.