JPA 在哪些场景中变得有趣/有用?
我正在开发一个 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
这可能是主观的,但就我个人而言,我发现 JDBC 通常更冗长,更难维护,因此更复杂。使用像 JPA 这样的 ORM,您不必编写所有 CRUD 查询,不必处理查询结果到对象的映射,不必自己处理低级内容,等等。
这完全取决于你对“更好”的定义。就您而言,您可能更喜欢 SP,因为您的开发工作流程(我不在容器中运行代码来设置持久性部分)并且因为您对 PL/SQL 感到满意。但是,就我个人而言,我并不认为 SP “更好”:
还请阅读 谁需要存储过程?(以及其他资源)以获取更多意见。
当您想加快开发速度时,请专注于实现业务代码而不是管道。当然,在适当的时候。
您是否发现了特定的性能问题?或者您只是假设会有问题。根据我的经验,检索多于所需的列在大多数情况下都不是问题。如果它成为一个问题,就有解决方案。
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.
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":
Read also Who Needs Stored Procedures, Anyways? (amongst other resource) for more opinions.
When you want to speed-up development, to focus on implementing business code instead of plumbing. And of course, when appropriate.
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.
您可以使用 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