Hướng dẫn mùa xuân,,en,Quét thành phần trong khuôn khổ Spring,,en – JPA (Java persistence API) with spring framework

Spring Data JPA

Hướng dẫn mùa xuân,,en,Quét thành phần trong khuôn khổ Spring,,en – JPA (Java persistence API) with spring framework

Tổng quan

Ánh xạ quan hệ đối tượng,,en,Doanh nghiệp muốn dành ít thời gian nhất có thể để sửa đổi các truy vấn SQL khi,,en,các loại được thay đổi,,en,dựa trên ORM,,en,làm cho cơ sở dữ liệu truy vấn SQL trở nên bất khả tri dựa trên các điều kiện nhất định,,en,Với khả năng của JPA,,en,nó đi trước các giải pháp ổn định dữ liệu khác hiện có trên thị trường,,en,ORM là gì,,en,Ánh xạ mối quan hệ đối tượng,,en,là một phương pháp để ánh xạ các bảng cơ sở dữ liệu với các lớp,,en,ORM đại diện cho một cách mới để truy vấn cơ sở dữ liệu,,en,Sự khác biệt chính giữa cách truy vấn cơ sở dữ liệu ORM và truy vấn giống SQL là cách sau chỉ áp dụng trong trường hợp SQL nhưng truy vấn ORM có thể hoạt động trên bất kỳ cơ sở dữ liệu nào.,,en,ORM làm cho các ứng dụng phần mềm hướng đối tượng,,en,Khi phương pháp ORM được áp dụng,,en,mỗi bảng cơ sở dữ liệu được ánh xạ tới một lớp,,en (ORM) concept is much-favoured these days because of the relative advantages it offers over relational database systems. ORM makes managing database a simpler task than do relational database systems. Enterprises want to spend as less time as possible on modifying SQL queries when database types are changed. mùa xuân JPA, which is ORM-based, makes SQL queries database agnostic based on certain conditions. Given the capabilities of JPA, it is ahead of other data persistence solutions available in the market.








What is ORM?

Object Relationship Mapping (ORM) is a method for mapping database tables to classes. ORM represents a new way of querying database. The main difference between the ORM-way of database querying and SQL-like querying is that the latter is applicable only in the case of SQL but ORM query can work on any database. ORM makes software applications object oriented.

When the ORM method is applied, each database table is mapped to a class. Hai khuôn khổ có sẵn để triển khai phương pháp ORM,,en,Hibernate Framework và Java Persistence API,,en,Để hiểu lợi ích chính của ORM,,en,hãy để chúng tôi sử dụng một ví dụ,,en,Một dự án đang sử dụng MySQL nhưng quyết định được đưa ra sau một vài ngày để sử dụng Oracle thay thế,,en,không cần phải thay đổi hoàn toàn truy vấn vì truy vấn ORM có tính chất toàn cục,,en,Các thay đổi trong truy vấn sẽ là tối thiểu về bản chất,,en,Tại sao JPA,,en,Có một số tùy chọn có sẵn cho các nhà phát triển phần mềm để lưu trữ và truy xuất dữ liệu liên tục trong cơ sở dữ liệu bao gồm tuần tự hóa,,en,công cụ lập bản đồ quan hệ đối tượng độc quyền,,en,cơ sở dữ liệu đối tượng,,en,đậu thực thể,,en,ngoại trừ JDO,,en,tất cả các tùy chọn đều có giới hạn,,en,JPA khắc phục những hạn chế này và cung cấp một giải pháp toàn diện về tính ổn định của dữ liệu,,en: Hibernate Framework and the Java Persistence API. To understand the main benefit of ORM, let us use an example. A project is using MySQL but a decision is made after some days to use Oracle instead. Trong trường hợp này, there is no need to change the query totally because the ORM query is global in nature. The changes in query will be minimal in nature.

Why JPA?

There are several options available to software developers for storing and retrieving persistent data in the database which include serialization, JDBC, JDO, proprietary object-relational mapping tools, object databases, và EJB 2 entity beans. However, except JDO, all options are limited. JPA overcomes these limitations and provides a comprehensive data persistence solution. Nó cũng làm giảm đáng kể các nhiệm vụ tổng quát mà nhà phát triển phần mềm cần thực hiện khi họ quản lý các hoạt động cơ sở dữ liệu,,en,Dưới đây là một vài lý do thuyết phục để JPA là một giải pháp toàn diện về tính ổn định của dữ liệu,,en,Tất cả các khả năng vòng,,en,Mặc dù hầu hết tất cả các công cụ hoặc khuôn khổ độ bền dữ liệu đều bị hạn chế về khả năng của chúng,,en,JPA vượt xa nhiều bước,,en,Để đưa ra một vài ví dụ về những hạn chế của các công cụ khác,,en,với Serialization,,en,dễ dàng lưu trữ thông tin đồ thị đối tượng một cách dễ dàng,,en,nó không thể quản lý khối lượng lớn dữ liệu,,en,JDBC API là một lựa chọn tốt hơn Serialization nhưng khó sử dụng,,en,Các khuôn khổ ánh xạ ORM bị giới hạn vì chúng bị ràng buộc bởi cơ sở dữ liệu dành riêng cho nhà cung cấp,,en,JPA kết hợp những điểm mạnh của các hệ thống nêu trên nhưng cũng khắc phục những hạn chế của chúng,,en,Không có nhiệm vụ chi phí,,en. Provided below are a few compelling reasons JPA is a comprehensive data persistence solution.

All round capabilities

While almost all data persistence frameworks or tools are limited in their capabilities, JPA goes many steps beyond. To give a few examples of the limitations of the other tools, with Serialization, it is easy to store object graph information easily, it is unable to manage large volumes of data. JDBC API is a better option than Serialization but is difficult to use. ORM mapping frameworks are limited because they are bound by vendor-specific databases. JPA combines the strengths of the systems mentioned above but also overcomes their limitations.

No overhead tasks

Quản lý hoạt động cơ sở dữ liệu là một đề xuất đầu tư cao vì nó đòi hỏi thời gian và nỗ lực,,en,JPA cho phép các nhà phát triển phần mềm tập trung vào công việc cốt lõi của họ và quản lý cơ sở dữ liệu,,en,Doanh nghiệp có thể dành nhiều thời gian hơn cho hoạt động cốt lõi,,en,công việc ưu tiên cao,,en,Cơ sở dữ liệu bất khả tri,,en,Sửa đổi,,en,các truy vấn tùy thuộc vào các cơ sở dữ liệu khác nhau là một đề xuất tiêu tốn tài nguyên,,en,Mặc dù tất cả các cơ sở dữ liệu đều chấp nhận SQL là tiêu chuẩn,,en,Phương ngữ SQL khác nhau,,en,JPA cung cấp một cơ sở dữ liệu độc lập-bất khả tri trên SQL,,en,Điều này loại bỏ sự phụ thuộc của SQL vào cơ sở dữ liệu,,en,Spring JPA là gì,,en,còn được gọi là Spring Data JPA,,en,là một API hỗ trợ triển khai các kho lưu trữ dựa trên JPA,,en,Việc triển khai các lớp truy cập dữ liệu là một vấn đề khá khó khăn vì các tác vụ liên quan đến,,en. JPA allows software developers focus on their core jobs and takes care of database management. Enterprises are able to devote more time towards core, high-priority jobs.

Database agnostic

Modifying SQL queries depending on different databases is a resource-consuming proposition. Though all databases accept SQL as standard, SQL dialects differ. JPA provides a database independent-agnostic on top of SQL. This removes the dependency of SQL on databases.

What is Spring JPA?

mùa xuân JPA, also known as Spring Data JPA, is an API that facilitates implementing JPA-based repositories. It has been quite a problem to implement data access layers because of the tasks involved. Có quá nhiều mã được triển khai chỉ để thực hiện các truy vấn đơn giản,,en,thực hiện kiểm tra dữ liệu và phân trang,,en,Spring JPA giải quyết những điểm khó khăn này bằng cách cho phép nhà phát triển tập trung vào các nhiệm vụ chính của họ - viết giao diện kho lưu trữ bao gồm các phương thức công cụ tìm tùy chỉnh - và quan tâm đến việc triển khai,,en,Các tính năng nổi bật của,,en,Chúng tôi,,en,Hỗ trợ ánh xạ thực thể dựa trên XML,,en,Kiểm tra lớp miền minh bạch,,en,Hỗ trợ phân trang và thực thi truy vấn động,,en,Hỗ trợ tích hợp mã truy cập dữ liệu tùy chỉnh,,en,Cấu hình kho lưu trữ dựa trên JavaConfig bằng cách giới thiệu @EnableJpaRepositories,,en,Các thành phần ORM là gì,,en,Đã có sự thay đổi từ cơ sở dữ liệu quan hệ sang cơ sở dữ liệu đối tượng vì cái sau mang lại lợi thế so sánh hơn cái trước,,en; perform data auditing and pagination. Spring JPA solves these pain points by letting the developer focus on their main tasks – writing repository interfaces which include custom finder methods – and taking care of the implementation. The salient features of Spring Data JPA are:

  • Support for XML-based entity mapping.
  • Transparent domain class auditing.
  • Support for pagination and dynamic query execution.
  • Support for integrating custom data access code.
  • JavaConfig based configuration of repository by introducing @EnableJpaRepositories.

What are the ORM components?

There has been a shift from relational database to object database because the latter offers comparative advantages over the former. Doanh nghiệp gặp khó khăn đáng kể trong việc bảo trì dữ liệu với cơ sở dữ liệu quan hệ,,en,Cơ sở dữ liệu đối tượng thực hiện lưu trữ,,en,lấy lại,,en,đang cập nhật,,en,và duy trì cơ sở dữ liệu,,en,Cốt lõi của cơ sở dữ liệu đối tượng là tệp orm.xml,,en,dựa trên XML,,en,Vì không cần biên dịch,,en,dễ dàng chỉnh sửa nhiều nguồn dữ liệu với quản trị tối thiểu,,en,Bản đồ quan hệ giữa các đối tượng,,en,là một kỹ thuật lập trình để chuyển đổi dữ liệu quan hệ thành kiểu đối tượng và ngược lại,,en,Các thành phần ORM chính được mô tả bên dưới,,en,Khả năng viết các lớp bền bỉ với sự trợ giúp của các lớp hướng đối tượng,,en,Đối tượng ổn định và đáng tin cậy,,en,thực thể và lưới,,en,Kiến trúc hiệu suất cao,,en,Thiết lập thư viện Spring cùng với tệp jar JPA,,en,Bây giờ môi trường của bạn đã sẵn sàng để bắt đầu phát triển Spring JPA với các ứng dụng Java,,en. Object databases perform storing, retrieving, updating, and maintenance of databases. At the core of object databases is the orm.xml file which, obviously, is XML-based. Since there is no need to compile XML, it is easy to edit multiple data sources with minimal administration. Object Relational Mapping (ORM) is a programming technique to convert relational data into object type and vice versa. The main ORM components are described below:

  • Ability to write persistence classes with the help of object oriented classes.
  • Stable and reliable objects, entities and grids.
  • High performing architecture.

Environment setup:

Setting up the environment for Spring based application development involves just three major steps.

  • Setting up Java Development Kit

Tải về JDK from the Oracle site and then, cài đặt và cấu hình nó. Then the PATH and JAVA_HOME environment variables must be set.

  • Setting of Eclipse IDE

Eclipse can be downloaded from the trang web chinh thưc. Once downloaded, unpack the binaries and then set the PATH as well.

  • Setting up Spring libraries along with JPA jar files

The Spring libraries may be obtained from http://repo.spring.io/release/org/springframework/spring/. Again, it is important to set the CLASSPATH correctly.








Now your environment is ready to start developing Spring JPA with Java applications.

Sample application:

Trong phần này, chúng tôi sẽ tạo một đối tượng sản phẩm và điền vào nó với dữ liệu,,en,Chúng tôi sẽ sử dụng Spring JPA để duy trì dữ liệu,,en,Đây là lớp POJO chứa dữ liệu sản phẩm,,en,techalpine.spring.jpa.com,,en,Sản phẩm,,en,Đây là lớp POJO,,en,giá,,en,id = id,,en,name = tên,,en,price = giá,,en,getId,,en,setId,,id,id = id,,en,getPrice,,en,setPrice,,nl,price = giá,,en,Sau đây là tệp cấu hình XML để ánh xạ bảng cơ sở dữ liệu với các trường PODJ,,en,Đây là ánh xạ XML với bảng,,en,phiên bản ánh xạ thực thể =,,en,java.sun.com/xml/ns/persistence/orm,,en,java.sun.com/xml/ns/persistence/orm_1_0.xsd,,en,lớp thực thể =,,en,spring.jpa.com. Sản phẩm,,en,tên bảng =,,en,bàn,,en,id name =,,en,tên cột =,,en,tên cơ bản =,,en,căn bản,,en,thực thể,,en,ánh xạ thực thể,,en,Sau đây là lớp DAO cho đối tượng sản phẩm,,en,Đây là lớp DAO cho sản phẩm,,en,gói techalpine.spring.jpa.com,,en,import org.springframework.orm.jpa.JpaTemplate,,en. We will be using Spring JPA for data persistence.

Listing 1: This is a POJO class containing product data

package techalpine.spring.jpa.com;

công khai lớp Product {

//This is the POJO class

riêng tư int id;

riêng tư String name;

riêng tư double price;

công khai Product(int id, String name, double price)

{

this.id=id;

this.name=name;

this.price=price;

}

công khai int getId() {

return id;

}

công khai làm mất hiệu lực setId(int id) {

this.id = id;

}

công khai String getName() {

return tên;

}

công khai làm mất hiệu lực setName(String name) {

this.name = name;

}

công khai double getPrice() {

return price;

}

công khai làm mất hiệu lực setPrice(double price) {

this.price = price;

}

}

Following is the XML configuration file for mapping database table with the PODJ fields.

Listing 2: This is XML mapping with the table

<?xml version=“1.0” encoding=“UTF-8”?>

<entity-mappings version=“1.0”

xmlns=“http://java.sun.com/xml/ns/persistence/orm”

xmlns:xsi=“http://www.w3.org/2001/XMLSchema-instance”

xsi:schemaLocation=“http://java.sun.com/xml/ns/persistence/orm

http://java.sun.com/xml/ns/persistence/orm_1_0.xsd “>

<entity class=techalpine.spring.jpa.com.Product”>

<table name=“product”></table>

<thuộc tính>

<id name=“id”>

<column name=“id”/>

</id>

<basic name=“tên”>

<column name=“tên”/>

</basic>

<basic name=“price”>

<column name=“price”/>

</basic>

</thuộc tính>

</entity>

</entity-mappings>

Following is the DAO class for the product object.

Listing 3: This is DAO class for the product

package techalpine.spring.jpa.com;

import java.util.List;

import org.springframework.orm.jpa.JpaTemplate;

import org.springframework.transaction.annotation.Transactional,,en,@Transactional,,en,lớp công khai ProductDao,,en,Mẫu JpaTemplate,,en,public void setTemplate,,co,this.template = mẫu,,en,public void createProduct,,en,int id,,en,giá gấp đôi,,en,Sản phẩm sản phẩm = Sản phẩm mới,,en,template.persist,,en,getAllProducts,,nl,products = template.find,,en,chọn chuyên nghiệp từ Sản phẩm chuyên nghiệp,,en,trả lại sản phẩm,,en,Đây là bối cảnh ứng dụng để ánh xạ tất cả các bean có liên quan,,en,Đây là ngữ cảnh ứng dụng,,en,tx =,,en,www.springframework.org/schema/tx,,en,www.springframework.org/schema/tx/spring-tx-3.0.xsd,,en,tx,,en,annotation-driven transaction-manager =,,en,jpaTxnManagerBean,,eu,proxy-target-class =,,en,dataSrcBean,,gd,org.springframework.jdbc.datasource.DriverManagerDataSource,,en,oracle.jdbc.driver.OracleDriver,,en,tiên tri,,en,gầy,,en,@localhost,,en,xe,,vi,test123,,en,hbAdptrBean,,en,org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter,,en,showSql,,en,createDdl,,en,databasePlatform,,en,org.hibernate.dialect.OracleDialect,,en,cmfBean,,en;

@Transactional

public class ProductDao {

JpaTemplate template;

public void setTemplate(JpaTemplate template) {

this.template = template;

}

public void createProduct(int id,String name,double price){

Product product = new Product(id,tên,price);

template.persist(product);

}

public List<Product> getAllProducts(){

List<Product> products =template.find(“select pro from Product pro”);

return products;

}

}

This is the application context for mapping all the relevant beans.

Listing 4: This is application context

<?xml version=“1.0” encoding=“UTF-8”?>

<đậu xmlns =“http://www.springframework.org/schema/beans”

xmlns:xsi=“http://www.w3.org/2001/XMLSchema-instance”

xmlns:tx=“http://www.springframework.org/schema/tx”

xsi:schemaLocation=“http://www.springframework.org/schema/beans

http://www.springframework.org/schema/beans/spring-beans-3.0.xsd

http://www.springframework.org/schema/tx

http://www.springframework.org/schema/tx/spring-tx-3.0.xsd”>

<tx:annotation-driven transaction-manager=“jpaTxnManagerBean” proxy-target-class=“đúng”/>

<bean id =“dataSrcBean” class =“org.springframework.jdbc.datasource.DriverManagerDataSource”>

<property name=“driverClassName” value=“oracle.jdbc.driver.OracleDriver”></tài sản>

<property name=“url” value=“jdbc:oracle:thin:@localhost:1524:xe”></tài sản>

<property name=“Tên người dùng” value=“thử nghiệm”></tài sản>

<property name=“password” value=“test123”></tài sản>

</hạt đậu>

<bean id =“hbAdptrBean” class =“org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter”>

<property name=“showSql” value=“đúng”></tài sản>

<property name=“generateDdl” value=“đúng”></tài sản>

<property name=“databasePlatform” value=“org.hibernate.dialect.OracleDialect”></tài sản>

</hạt đậu>

<bean id =“cmfBean” class =“org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean,,en,jpaVendorAdapter,,en,jpaTmpltBean,,en,org.springframework.orm.jpa.JpaTemplate,,ms,entityManagerFactory,,en,productDaoBean,,en,spring.jpa.com.ProductDao,,pt,org.springframework.orm.jpa.JpaTransactionManager,,ms,đây là lớp chính để kiểm tra dân số của đối tượng sản phẩm và truy xuất nó từ cơ sở dữ liệu,,en,Đây là lớp chính để kiểm tra kết quả đầu ra,,en,org.springframework.context.support.FileSystemXmlApplicationContext,,en,ProductTest,,en,FileSystemXmlApplicationContext,,en,META-INF / applicationContext.xml,,en,ProductDao productDao = context.getBean,,en,ProductDao,,es,productDao.createProduct,,en,product1,,en,product2,,en,Sản phẩm được tạo thành công,,en,products = productDao.getAllProducts,,en,Chi tiết sản phẩm là,,en,sản phẩm. kích thước,,en,Product pro = products.get,,en,pro.getId,,en,pro.getName,,en,pro.getPrice,,en,Một lớp này được thực thi,,en,kết quả sau sẽ được hiển thị,,en,Vì JPA và ORM đều dựa trên XML,,en”>

<property name=“dataSource” ref=“dataSrcBean”></tài sản>

<property name=“jpaVendorAdapter” ref=“hbAdptrBean”></tài sản>

</hạt đậu>

<bean id =“jpaTmpltBean” class =“org.springframework.orm.jpa.JpaTemplate”>

<property name=“entityManagerFactory” ref=“cmfBean”></tài sản>

</hạt đậu>

<bean id =“productDaoBean” class = techalpine.spring.jpa.com.ProductDao”>

<property name=“template” ref=“jpaTmpltBean”></tài sản>

</hạt đậu>

<bean id =“jpaTxnManagerBean” class =“org.springframework.orm.jpa.JpaTransactionManager”>

<property name=“entityManagerFactory” ref=“cmfBean”></tài sản>

</hạt đậu>

</đậu>

Now, this is the main class to check the population of the product object and retrieve it from the database.

Listing 5: This is the main class to check the output

package techalpine.spring.jpa.com;

nhập khẩu java.util.List;

nhập khẩu org.springframework.context.ApplicationContext;

nhập khẩu org.springframework.context.support.FileSystemXmlApplicationContext;

công khai lớp ProductTest {

công khai tĩnh làm mất hiệu lực chính(Chuỗi[] args){

ApplicationContext context = mới FileSystemXmlApplicationContext(

“META-INF/applicationContext.xml”);

ProductDao productDao = context.getBean(“productDaoBean”,ProductDao.lớp);

productDao.createProduct(123,”product1″,233.55);

productDao.createProduct(20,”product2″,350.70);

Hệ thống.ra.println(“Products created successfully”);

List<Product> products = productDao.getAllProducts();

System.out.println(“Products details are:”);

cho (int i = 0; tôi < products.size(); i ) {

Product pro = products.get(tôi);

Hệ thống.ra.println(pro.getId() + ” : ” + pro.getName() + ” (” + pro.getPrice() + “)”);

}

}

}

One this class is executed, following output will be displayed.

Products created successfully

Products details are:

123:product1:233.55

20:product2:350.70








Kết luận

Since JPA and ORM are both based on XML, sự phụ thuộc của họ vào các công nghệ hoặc giải pháp khác ít hơn so với các giải pháp bền bỉ khác,,en,ý tưởng về ORM và JPA sẽ mất một khoảng thời gian để tìm được lực kéo bởi vì các doanh nghiệp sẽ cần thời gian để di chuyển khỏi các hệ thống cơ sở dữ liệu kế thừa,,en,bồi thẩm đoàn không xác định liệu JPA có phải là giải pháp cho tất cả các vấn đề về cơ sở dữ liệu hay không vì nó được dự đoán là,,en. However, the idea of ORM and JPA will take some time to find some traction because enterprises will require time to move away from legacy database systems. Also, the jury is out on whether the JPA is a solution for all database problems as it is projected to be.

 

============================================= ============================================== Mua sách Techalpine tốt nhất trên Amazon,en,Thợ điện CT Hạt dẻ,en
============================================== ---------------------------------------------------------------- electrician ct chestnutelectric
error

Thưởng thức blog này,,en,làm ơn mở rộng vốn từ,,en,techalpine.com/apache-mahout-and-machine-learning,,en? Please spread the word :)

Follow by Email
LinkedIn
LinkedIn
Share