JPA 在哪些场景中变得有趣/有用?

发布于 2024-09-25 11:25:55 字数 374 浏览 1 评论 0原文

我正在开发一个 Java EE 应用程序(JSF + Richfaces + Oracle 10g),并且我想使用 JPA。

但最终,我没有看到使用它的任何优点,因为它会使我的代码变得复杂。

我发现调用我的程序(我的 orale DB 中的存储过程)比使用 JPA 更好(例如,因为我可以更改这些程序中的某些行,而无需每次我有一些时重新编译我的“WAR”项目)错误+我可以使用PL/SQL,这对我有很大帮助)

所以,我想问你们,什么时候使用JPA?

不是进行自己的查询(您可以为您选择的、要选择的列,而不是所有列选择正确的顺序:因为 ORM 以及您的实体属性映射到表的列这一事实,这迫使您选择实体中存在的所有属性,....)

这是我使用的方法(存储函数)

I'm developing a Java EE application (JSF + Richfaces + Oracle 10g), and i wanted to use JPA.

But in the end, i didn't see any advantages of using it, because it's going to complexify my code.

I found that calling my procedures (stored procedures in my orale DB) is better than using JPA (because i can, for example, change some lines in those procedures without the need to re'compile my "WAR" project every time i have some error + i can use PL/SQL which helps me a lot)

So, i wanted to ask you people, when to use JPA ?

Isn't making your own queries (you can choose the right ordering for you selects, the columns that you want to select, and not all the columns: because of ORM and the fact that your entities attributes are mapped to the columns of your table, and that oblige you to select all the attributes present in your entity,....)

Is it my method that i used (stored function)

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

情丝乱 2024-10-02 11:25:55

但最终,我没有看到使用它的任何优势,因为它会使我的代码变得复杂。

这可能是主观的,但就我个人而言,我发现 JDBC 通常更冗长,更难维护,因此更复杂。使用像 JPA 这样的 ORM,您不必编写所有 CRUD 查询,不必处理查询结果到对象的映射,不必自己处理低级内容,等等。

我发现调用我的程序(我的 orale DB 中的存储过程)比使用 JPA 更好(例如,因为我可以更改这些程序中的某些行,而无需每次都重新编译我的“WAR”项目我有一些错误 + 我可以使用 PL/SQL 这对我有很大帮助)

这完全取决于对“更好”的定义。就您而言,您可能更喜欢 SP,因为您的开发工作流程(不在容器中运行代码来设置持久性部分)并且因为您对 PL/SQL 感到满意。但是,就我个人而言,我并不认为 SP “更好”:

  • 我并不真正喜欢为所有内容手动编写 SQL 查询,
  • 我发现 SP 更难测试、调试和维护
  • 我发现 SP 不利于代码重用
  • 我发现SP 将您锁定(我认为这是一个缺点)
  • 我倾向于发现围绕 SP 构建的系统更难以扩展

还请阅读 谁需要存储过程?(以及其他资源)以获取更多意见。

所以,我想问一下大家,什么时候使用JPA?

当您想加快开发速度时,请专注于实现业务代码而不是管道。当然,在适当的时候。

不是自己进行查询(...)

您是否发现了特定的性能问题?或者您只是假设会有问题。根据我的经验,检索多于所需的列在大多数情况下都不是问题。如果它成为一个问题,就有解决方案。

But in the end, i didn't see any advantages of using it, because it's going to complexify my code.

This might be subjective but, personally, I find JDBC typically more verbose, harder to maintain and thus somehow more complex. With an ORM like JPA, you don't have to write all the CRUD queries, you don't have to handle the mapping of query results to objects, you don't have to handle the low level stuff yourself, etc.

I found that calling my procedures (stored procedures in my orale DB) is better than using JPA (because i can, for example, change some lines in those procedures without the need to re'compile my "WAR" project every time i have some error + I can use PL/SQL which helps me a lot)

This is totally dependent on your definition of "better". In your case, you might prefer SP because of your development workflow (I don't run my code in container to setup the persistence part) and because you are comfortable with PL/SQL. But again, personally, I don't find SP "better":

  • I don't really enjoy hand writing SQL queries for everything
  • I find that SP are harder to test, debug, maintain
  • I find that SP are bad for code reuse
  • I find that SP lock you in (I consider this as a disadvantage)
  • I tend to find systems build around SP harder to scale

Read also Who Needs Stored Procedures, Anyways? (amongst other resource) for more opinions.

So, I wanted to ask you people, when to use JPA ?

When you want to speed-up development, to focus on implementing business code instead of plumbing. And of course, when appropriate.

Isn't making your own queries (...)

Did you identify a particular performance problem? Or are you just assuming there will be a problem. To my experience, retrieving more columns than required is most of time not an issue. And if it becomes an issue, there are solutions.

风向决定发型 2024-10-02 11:25:55

您可以使用 JPA 定义在查询中检索哪些字段。
为什么使用 JPA 会增加代码的复杂性?你没有举出例子。之前您必须执行混乱的 JDBC,现在您只需调用“em.persist(obj)”...这确实更复杂。

您应该考虑对象之间的关系(如果有的话),这将决定您是否需要 ORM

You can define what fields are retrieved in a query, using JPA.
Why is the complexity of your code going up with use of JPA ? You state no example. Before you'd have to do messy JDBC, and now you just call "em.persist(obj)" ... is that really more complex.

You ought to be thinking in terms of what relations there are between objects (if any) and that be the determiner on whether you need an ORM

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文