JPA或JDBC,它们有何不同? [英] JPA or JDBC, how are they different?

查看:105
本文介绍了JPA或JDBC,它们有何不同?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在学习Java EE,我也用玻璃鱼下载了eclipse。我看了一些示例,并阅读了Oracle文档以了解Java EE 5的所有内容。连接到数据库非常简单。我打开了一个动态Web项目,创建了一个会话EJB,我使用了EntityManager,并且get方法可以访问存储的数据表。

I am learning Java EE and I downloaded the eclipse with glassfish for the same. I saw some examples and also read the Oracle docs to know all about Java EE 5. Connecting to a database was very simple. I opened a dynamic web project, created a session EJB , I used EntityManager and with the get methods could access the stored data table.

对于我的下一个项目,我创建了一个简单类然后访问一些DB表。我遇到的第一个问题是PersistenceUnit属性只能被EJB,Servlet等识别,而不是简单的java类。那么我就无法使用EntityManager方式(或者我可以吗?)

For my next project I had create a simple class and then access some DB table. The very first problem I encountered was that the PersistenceUnit attribute would only be recognized by EJB,Servlet etc and not a simple java class. So then I could not use the EntityManager way(or can I?)

我被要求通过JDBC方式。我遇到的第一个问题是获得与DB的连接。似乎这一切都必须硬编码。我有一个persistence.xml,我可以轻松地配置数据库连接。即使为DB设置驱动程序也很容易。 JDBC中也没有用于访问表实体的get / set方法。

I was asked to go via the "JDBC" way. The very first problem I encountered was to get the connection to the DB. It seems all this must be hardcoded. I had a persistence.xml with which I could easily configure the data base connection. Even setting up a driver for the DB was easy. Also there no get/set methods in the JDBC for accessing table entities.

如何理解与JDBC相关的JPA和持久性? JPA想到了什么?为什么有set / get方法?有人能否对这两者的本质有所了解,没有术语的优点/缺点是什么?还请建议一些链接。一个简单的谷歌搜索JPA和JDBC差异导致我的一些网站充满了我无法遵循的术语:(

How do I understand JPA and persistence in relation to JDBC? What was JPA thought for? Why is there set/get methods? Can someone throw some light on the essence of these two and what are the pros/cons without "jargons"?? Please also suggest some links. A simple google search for JPA and JDBC differences led me to some sites full of "terminology" I couldn't follow :(

推荐答案

通俗地说:


  • JDBC是数据库访问的标准

  • JPA是一个标准ORM

JDBC是直接连接数据库并对其运行SQL的标准 - 例如 SELECT * FROM USERS 等。您可以在应用中处理数据集,并且可以执行所有常见操作,例如 INSERT DELETE ,运行存储过程等。它是大多数Java数据库访问(包括JPA提供程序)背后的基础技术之一。

JDBC is a standard for connecting to a DB directly and running SQL against it - e.g SELECT * FROM USERS, etc. Data sets can be returned which you can handle in your app, and you can do all the usual things like INSERT, DELETE, run stored procedures, etc. It is one of the underlying technologies behind most Java database access (including JPA providers).

传统JDBC应用程序的一个问题是,您经常会遇到一些糟糕的代码,其中数据集和对象之间存在大量映射,逻辑与SQL等混合在一起。

One of the issues with traditional JDBC apps is that you can often have some crappy code where lots of mapping between data sets and objects occur, logic is mixed in with SQL, etc.

JPA是对象关系映射的标准。这是一种允许您在代码和数据库表中的对象之间进行映射的技术。这可以隐藏开发人员的SQL,以便他们处理的所有内容都是Java类,并且提供程序允许您保存它们并神奇地加载它们。大多数情况下,getter和setter上的XML映射文件或注释可用于告诉JPA提供程序您的对象映射到DB中的哪些字段。最着名的JPA提供商是 Hibernate ,因此这是一个开始具体示例的好地方。

JPA is a standard for Object Relational Mapping. This is a technology which allows you to map between objects in code and database tables. This can "hide" the SQL from the developer so that all they deal with are Java classes, and the provider allows you to save them and load them magically. Mostly, XML mapping files or annotations on getters and setters can be used to tell the JPA provider which fields on your object map to which fields in the DB. The most famous JPA provider is Hibernate, so it's a good place to start for concrete examples.

其他例子包括OpenJPA,toplink等。

Other examples include OpenJPA, toplink, etc.

在幕后,Hibernate和大多数其他JPA提供者编写SQL并使用JDBC来读取并从DB写入。

Under the hood, Hibernate and most other providers for JPA write SQL and use JDBC to read and write from and to the DB.

这篇关于JPA或JDBC,它们有何不同?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆