用于数据库应用程序的 Java JDBC 与 JPA
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/6784504/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Java JDBC vs JPA for database application
提问by Zach
I'd like to preface that I'm somewhat of a novice looking for advice as I'm trying to build good habits.
我想先说明一下,我是个新手,在努力养成良好习惯时寻求建议。
The application I'm developing right now is a heavily integrated database application. As I develop and explore and implement the requirements for each of my entities, I'm finding that my classes are just exploding with code to run queries in different ways on each of the entities.
我现在正在开发的应用程序是一个高度集成的数据库应用程序。当我开发、探索和实现每个实体的要求时,我发现我的类只是用代码爆炸,以不同的方式对每个实体运行查询。
While it might not be a bad thing right now, in terms of maintenance, I foresee my application being a nightmare to debug and update.
虽然现在这可能不是一件坏事,但在维护方面,我预见我的应用程序将成为调试和更新的噩梦。
Do any JDBC experts out there have any suggestions for design patters that would help slim down the boiler-plate type code for handling all of these queries? Or should I stray from that completely and use JPA?
是否有任何 JDBC 专家对设计模式有任何建议,以帮助减少处理所有这些查询的样板类型代码?或者我应该完全偏离它并使用JPA?
I've tried to implement JPA in the past, but have had trouble with complex entity relationships. Should I just read a JPA book and go from there?
我过去曾尝试实现 JPA,但在处理复杂的实体关系时遇到了麻烦。我应该读一本 JPA 书然后从那里开始吗?
回答by Tahir Akhtar
JPA can be a good long-term solution. But if you prefer to stay closer to plain SQL, you can consider other options like Spring Framework's JDBC support.
JPA 可以是一个很好的长期解决方案。但是,如果您更喜欢接近纯 SQL,则可以考虑其他选项,例如Spring Framework 的 JDBC 支持。
Note that, you don't need to use other spring framework components link DI, MVC etc to be able to use Spring JDBC. It is quiet easy to use without other parts spring framework. When using spring jdbc, you don'tneed to do following tasks in your code:
请注意,您无需使用其他 spring 框架组件链接 DI、MVC 等即可使用 Spring JDBC。无需其他零件弹簧框架,安静易用。当使用了Spring JDBC,你不需要做下面的代码中的任务:
- Open the connection.
- Prepare and execute the statement.
- Set up the loop to iterate through the results (if any).
- Process any exception.
- Handle transactions.
- Close the connection, statement and resultset.
- 打开连接。
- 准备并执行语句。
- 设置循环以迭代结果(如果有)。
- 处理任何异常。
- 处理交易。
- 关闭连接、语句和结果集。
What you need to do is:
你需要做的是:
- Define connection parameters. (once)
- Specify the SQL statement. (for each query)
- Declare parameters and provide parameter values (when using prepared statements)
- Do the work for each iteration. (spring do the resultset traversal, you only need to provide logic for acting on a single row)
- 定义连接参数。(一次)
- 指定 SQL 语句。(对于每个查询)
- 声明参数并提供参数值(使用准备好的语句时)
- 为每次迭代做工作。(spring做resultset遍历,只需要提供作用于单行的逻辑)
Another benefit of spring-jdbc is that it replaces JDBC checked exceptions with unchecked exceptions.
spring-jdbc 的另一个好处是它用未检查的异常替换了 JDBC 检查的异常。
回答by Marvo
Whatever solution you DO use, be it straight JDBC or JPA, be sure to break your code up into pieces that can be easily swapped out when the time comes to change technologies.
无论您使用什么解决方案,无论是直接的 JDBC 还是 JPA,请务必将您的代码分解为可以在需要更改技术时轻松交换的部分。
The downside with JDBC is that you may end up with implementation specific code (Oracle, MS, MySQL, etc.) This can be a real pain to migrate away from if you decide to change things down the road.
JDBC 的缺点是您最终可能会得到特定于实现的代码(Oracle、MS、MySQL 等)。如果您决定在未来进行更改,迁移出去可能会非常痛苦。
I ended up studying up on Hibernate, and the book Harnessing Hibernategot me quite a ways into doing that kind of development (and also brought Spring and Maven along for the ride, in ways that slowly built on top of one another.)
我最终学习了 Hibernate,这本书Harnessing Hibernate 为我提供了进行这种开发的相当多的方法(并且还带来了 Spring 和 Maven,以慢慢建立在彼此之上的方式。)
What you should end up with, regardless of approach, are:
无论采用何种方法,您最终应该得到的是:
DAO objects -- these Data Access Objects will do your CRUD operations (create, update, and delete) and should be database agnostic.
Model objects -- these should represent your data, and will probably look a lot like Java representations of a single row in a database table. DAO classes will return these, or lists of these.
DAO 对象——这些数据访问对象将执行您的 CRUD 操作(创建、更新和删除),并且应该与数据库无关。
模型对象——它们应该代表您的数据,并且可能看起来很像数据库表中单行的 Java 表示。DAO 类将返回这些或这些列表。
Harnessing Hibernate describes a pattern in later chapters (after it has thrown Spring at you) where you'll use essentially two layers of DAO classes. The highest level DAO class will instantiate (or allow to be injected) an implementation specific DAO class.
Harnessing Hibernate 在后面的章节中描述了一种模式(在它向您抛出 Spring 之后),您将在其中使用基本上两层 DAO 类。最高级别的 DAO 类将实例化(或允许注入)特定于实现的 DAO 类。
So, let's pretend you have an EMPLOYEE database table. So you create a model object called Employee that holds all the data a row in the EMPLOYEE table holds. Now you create a DAO class called EmployeeDAO that implements the following:
因此,让我们假设您有一个 EMPLOYEE 数据库表。因此,您创建了一个名为 Employee 的模型对象,该对象包含 EMPLOYEE 表中一行所包含的所有数据。现在您创建一个名为 EmployeeDAO 的 DAO 类,它实现以下内容:
EmployeeDAO.createEmployee(Employee emp)
EmployeeDAO.updateEmployee(Employee emp)
EmployeeDAO.deleteEmployee(Employee emp)
Your initial thinking would be to put your JDBC calls there. But don't do it. Instead, you now write another DAO for Employee, and this one will implement all your JDBC calls. (Assuming you go JDBC):
您最初的想法是将您的 JDBC 调用放在那里。但不要这样做。相反,您现在为 Employee 编写另一个 DAO,这个 DAO 将实现您的所有 JDBC 调用。(假设你去 JDBC):
EmployeeJdbcDAO.create(Employee emp)
EmployeeJdbcDAO.update(Employee emp)
EmployeeJdbcDAO.delete(Employee emp)
Now the methods in EmployeeDAO? They simply instantiate EmployeeJdbcDAO, and call the appropriate method. When it comes time down the road to switch to Oracle with Hibernate, you create a new DAO class called something like EmployeeOrHibDAO, write Hibernate and Oracle specific code there, and then instead of calling EmployeeJdbcDAO in EmployeeDAO, you instantiate EmployeeOrHibDAO instead. (And with Spring, you don't even change the code. You just change your Spring DI configuration.)
现在 EmployeeDAO 中的方法?他们只是实例化 EmployeeJdbcDAO,并调用适当的方法。当需要使用 Hibernate 切换到 Oracle 时,您可以创建一个名为 EmployeeOrHibDAO 之类的新 DAO 类,在那里编写 Hibernate 和 Oracle 特定代码,然后不是在 EmployeeDAO 中调用 EmployeeJdbcDAO,而是实例化 EmployeeOrHibDAO。(使用 Spring,您甚至不需要更改代码。您只需更改 Spring DI 配置。)
回答by Michael J. Lee
Not a realanswer but, i do think it's important to throw in a couple more options in the mix that may help you find a good middle ground. I suggest this because a JPA implementation on a existing database with lots of complexity and queries can be a little troublesome for a someone without a fair share of battle scars. Consider the following but do the research and build some tracer bullet apps;
不是一个真正的答案,但是,我确实认为在混合中加入更多选项很重要,这可能会帮助您找到一个好的中间立场。我建议这样做是因为在具有大量复杂性和查询的现有数据库上实现 JPA 对没有公平份额的战斗伤痕的人来说可能有点麻烦。考虑以下但做研究并构建一些示踪子弹应用程序;
- Spring JDBCTemplate and DAO PatternLove this for solutions where JPA\Hibernate just don't make sense.
- MyBatisAgain, another nice middle ground with a little more control over the SQL
- Spring JDBCTemplate 和 DAO 模式喜欢这个解决方案,其中 JPA\Hibernate 没有意义。
- MyBatis再次,另一个很好的中间立场,对 SQL 有更多的控制
回答by evildethow
Don't discard an active record patternas an option. That doesn't have to be an EJB entity approach. The pattern is worth mentioning IMHO
不要丢弃活动记录模式作为选项。这不一定是 EJB 实体方法。该模式值得一提恕我直言
回答by Alan B. Dee
If your looking to build good habits I would read Code Complete 2by Steve McConnell.
如果您希望养成良好的习惯,我会阅读Steve McConnell 的Code Complete 2。