为什么使用JPA而不是直接在Java文件上写SQL查询(即直接向JDBC)?[英] Why use JPA instead of directly writing SQL query on Java File (i.e. directly to JDBC)?

本文是小编为大家收集整理的关于为什么使用JPA而不是直接在Java文件上写SQL查询(即直接向JDBC)?的处理/解决方法,可以参考本文帮助大家快速定位并解决问题,中文翻译不准确的可切换到English标签页查看源文。

问题描述

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

我没有使用 ORM(对象关系映射)的经验.

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

对我来说,我觉得像下面这样写 DAO 类就可以了.

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 表.

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

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

如果您需要更多说明,请告诉我,我是这个主题的新手,想听听一些意见.

推荐答案

为什么使用 JPA 而不是直接使用在 Java 文件上编写 SQL 查询(即直接连接到 JDBC)?

某些项目要求工程师更多地关注对象模型,而不是用于访问数据存储的实际 SQL 查询.这个问题其实可以理解为

<块引用>

为什么要使用 ORM 框架?

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

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

也就是说,ORM 框架在某些地方可能无法直接解决问题,尤其是当数据模型已经建立时,或者在处理将数据库表映射到 Java 类的遗留系统时琐碎的练习.而且在某些情况下,如果需要彻底调整 ORM 框架生成的 SQL,那么 ORM 框架通常不适合.

相关问题

  1. JavaEE 架构 - 在使用 JPA 2 之类的 ORM 时,是否仍然推荐使用 DAO?
  2. 使用 ORM 还是普通 SQL?
  3. ORM 与手动编码数据访问层

本文地址:https://www.itbaoku.cn/post/597443.html

问题描述

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

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

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.

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);
     }

}

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

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

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.

推荐答案

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

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

Why should one use an ORM framework ?

which can have different answers in different contexts.

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.

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.

Related Questions

  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