Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

JPA depth analysis version of Bao Bao Liu

2025-04-07 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Database >

Share

Shulou(Shulou.com)06/01 Report--

Liu Baobao's explanation has begun.

Lecture 1 introduction to JPA

JPA

The concept of 1.JPA:

Java Persistence API: API for object persistence

The standard ORM specification of the Java EE 5.0platform enables applications to access the persistence layer in a unified manner

The relationship between 2.JPA and hibernate

Functionally, JPA is a subset of Hibernate functionality

Advantages of 3.JPA:

Standardization: provide the same API, which ensures that enterprise applications developed based on JPA can run under different JPA frameworks with a few modifications.

Easy to use and easy to integrate: one of the main goals of JPA is to provide a simpler programming model. Creating entities under the JPA framework is as simple as creating Java classes, and you only need to use javax.persistence.Entity for annotation; the framework and interface of JPA are also very simple.

Comparable to the query capabilities of JDBC: JPA's query language is object-oriented, JPA defines a unique JPQL, and can support batch updates and modifications, JOIN, GROUP BY, HAVING and other advanced query features that usually only SQL can provide, and even support subqueries.

Support for object-oriented advanced features: JPA can support advanced object-oriented features, such as inheritance between classes, polymorphism and complex relationships between classes, and maximize the use of object-oriented models

4.JPA includes three aspects of technology.

ORM mapping metadata: JPA supports two forms of metadata annotated by XML and JDK 5.0. the metadata describes the mapping relationship between objects and tables, according to which the framework persists entity objects into database tables.

JPA's API: used to manipulate entity objects, perform CRUD operations, the framework does everything in the background, and developers are freed from tedious JDBC and SQL code.

Query language (JPQL): this is an important aspect of persistence operations to query data through an object-oriented rather than database-oriented query language to avoid tight coupling between the program and the specific SQL.

The 1.persistence.xml file is fixed

two。 When spring and hibernate are integrated, the persistence.xml can be left out or left.

3.persistence.xml

Org.hibernate.ejb.HibernatePersistence

Com.baidu.jpa.helloworld.Customer

Lesson 2 steps to create a JPA project

1. Create a database name

two。 Create a jpa project selection 2.0

3. Create a lib file add Build Path

Antlr-2.7.7.jar

Dom4j-1.6.1.jar

Ehcache-core-2.4.3.jar

Hibernate-commons-annotations-4.0.2.Final.jar

Hibernate-core-4.2.4.Final.jar

Hibernate-ehcache-4.2.4.Final.jar

Hibernate-entitymanager-4.2.4.Final.jar

Hibernate-jpa-2.0-api-1.0.1.Final.jar

Javassist-3.15.0-GA.jar

Jboss-logging-3.1.0.GA.jar

Jboss-transaction-api_1.1_spec-1.0.1.Final.jar

Mysql-connector-java-5.1.7-bin.jar

Slf4j-api-1.6.1.jar

4. Persistence.xml

5. Create the package name com.baidu.jpa

Create persistent classes, entity classes

@ Entity

@ Table (name= "Table name")

@ Id

@ @ GeneratedValue (strategy=GenerationType.AUTO)

6. Go to the configuration file and add a persistence class

Com.baidu.jpa.helloworld.Customer

7. Create a main method class

/ / 1. Create EntitymanagerFactory

/ / persistenceUnitName persistence unit name

String persistenceUnitName = "jpa-1"

EntityManagerFactory emf = Persistence.createEntityManagerFactory (persistenceUnitName)

/ / 2. Create EntityManager

EntityManager em = emf.createEntityManager ()

/ / 3. Open a transaction

EntityTransaction et = em.getTransaction ()

Et.begin ()

/ / 4. Perform persistence operation

Customer c = new Customer ()

C.setAge (12)

C.setEmail ("123@qq.com")

C.setLastName ("Zhang San")

Em.persist (c); / / Save

/ / 5. Commit transaction

Et.commit ()

/ / 6. Close EntityManager

Em.close ()

/ / 7. Close EntityManagerFactory

Emf.close ()

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 225

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Database

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report