为什么使用JPA而不是使用JDBC编写SQL查询? [英] Why use JPA instead of writing the SQL query using JDBC?

查看:43
本文介绍了为什么使用JPA而不是使用JDBC编写SQL查询?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直在阅读几篇文章,什么是 JPA(Java持久性API),以及支持它的供应商(DataNucleus,JBoss Hibernate等)

I've been reading up on several articles what is JPA (Java Persistent API) and which vendor supporting it (DataNucleus, JBoss Hibernate etc)

我没有ORM(对象关系映射)的经验。

I don't have experience with ORM (object relational mapping).

到目前为止,我所做的就是编写自己的使用DTO和DAO的数据库类。到目前为止,我很高兴自己拥有什么,但想知道为什么人们使用JPA而不是包含SQL的Java文件

What I have done so far is to write my own Database classes using DTO and DAO. So far I'm happy about what I have but would like to know why people use JPA over Java file which contains SQL.

对我来说,编写DAO类可以像下面这样。

To me I feel like writing DAO class would be ok something like below.

public class DAOUsers {
     public void insertNewUser(DTO DtoUser) {
           String query = "INSERT INTO users(username, address) " +
                          "VALUES(DtoUser.username , DtoUser.address)";
           Executor.run(query);
     }

}

我了解到JPA使用JPQL ,这是Java持久查询语言,它针对实体对象
进行操作,而不是直接针对db表进行操作。

I've learned JPA uses JPQL, Java persistent query language and it operates against entity object rather than directly with db tables.

我的理解(如果我错了,请纠正我)是这里的实体对象与我的DTO对象相同(有点像豆子吗?)

My understanding (correct me if Im wrong) is that entity object here is same as my DTO object (kind of like bean?)

但是无论如何.. JPA相对于在我的文件中编写纯SQL的真正好处是什么?
好​​像使用JPA所需的注释,并使SQL不可读对我来说真的没有吸引力。

But anyhow.. what really benefit JPA gives over writing pure SQL in my file? Seems like using annotations required by JPA and make SQL not readable feels not really attractive to me..

请让我知道是否需要更多说明,我

please let me know if you need more clarification, I'm new to this topic and would like to hear some opinion.

推荐答案


为什么改用JPA
在Java文件上直接写SQL查询的示例(即
直接到JDBC)?

Why use JPA instead of directly writing SQL query on Java File (i.e. directly to JDBC) ?

某些项目需要工程师将重点更多地放在对象模型上,而不是用于访问数据存储的实际SQL查询上。这个问题实际上可以解释为

Certain projects require engineers to focus more on the object model rather than on the actual SQL queries used to access data stores. The question can actually be interpreted as


为什么应该使用ORM框架?

Why should one use an ORM framework ?

在不同的上下文中可以有不同的答案。

which can have different answers in different contexts.

大多数项目都可以从拥有域模型中受益,而持久性是第二个问题。使用JPA(实现)或大多数其他ORM框架,可以将所有实体(即数据库中的表)建模为Java中的类。此外,还可以将行为嵌入这些类中,从而获得行为丰富的域模型。此模型中的实体可以有多种用途,包括替换DTO以便跨层传输数据的目的。

Most projects can benefit from having a domain model, with persistence being a second concern. With JPA (implementations) or most other ORM frameworks, it is possible to have all entities i.e. tables in your database, modelled as classes in Java. Additionally, it also possible to embed behavior into these classes and therefore achieve a behaviorally rich domain model. The entities in this model can have multiple purposes, including the purpose of replacing DTOs for transporting data across tiers.

也就是说,在有些地方ORM框架可能不是直接适合该问题,特别是当数据模型已经建立时,或者当使用旧系统(将数据库表映射到Java类)时,这并非易事。而且在某些情况下,如果需要从ORM框架生成的SQL中完全消除问题,那么ORM框架通常是不合适的。

That said, there are places where ORM frameworks may not be a direct fit to the problem, especially when the data model is already established, or when one is working with legacy systems where mapping database tables to Java classes is a non-trivial exercise. And in certain cases, if one needs to absolutely tune the heck out of the SQL generated by the ORM framework, then ORM frameworks are usually a bad fit.

相关问题



  1. 使用ORM还是纯SQL?

  2. ORM与手工编码数据访问层

  1. Java EE Architecture - Are DAO's still recommended when using an ORM like JPA 2?
  2. Using an ORM or plain SQL?
  3. ORM vs Handcoded Data Access Layer

这篇关于为什么使用JPA而不是使用JDBC编写SQL查询?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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