Java EE 6框架仅用于Web应用程序还是我也可以将它用于客户端应用程序 [英] Does Java EE 6 framework only for Web Application Or can I use it for Client Application as well
问题描述
我总是将Jave EE 6框架用于Web应用程序。所以我不确定我要做的是正确的。我需要创建一个具有数据库访问权限的本机客户端命令行应用程序。如此简单的带有JDBC的Java Project就可以完成这项工作。但是db访问的要求包括连接池,并发句柄和事务......现在,项目的需求最终构建了一个Web界面,但首先它将首先构建一个命令行应用程序。这就是我在思考框架的时候。我喜欢Java EE 6.那么java EE 6在这里是正确的选择吗?我可以使用java EE 6开发本机客户端应用程序,然后再添加一个Web模块吗?
I always use Jave EE 6 framework for web application. So I am not sure what I about to do is correct. I need to create an native client command-line application that has database access. So simple java Project with JDBC would do that job. But the requirements for db access include connection pool, concurrency handle, and transaction ..., Now the requirement of the projects does eventually build a web interface, but first it will build a command line application first. And this is when i am thinking about framework. I love Java EE 6. So does java EE 6 the right choice here? Can I use java EE 6 to develop native client application, and later add a web module to it?
我正在使用Netbeans 7.0顺便说一句
I am using Netbeans 7.0 btw
推荐答案
你可以完美地使用JPA在独立客户端应用程序中,以 main()
类作为入口点。只需将JPA JAR添加到buildpath / classpath并配置 persistence.xml
即可使用 RESOURCE_LOCAL
事务类型。您可以在 EclipseLink Wiki - 运行JPA Outside Container 中找到启动示例。以下是相关摘录:
You can perfectly use JPA in a standalone client application with a main()
class as entry point. Just add the JPA JAR(s) to the buildpath/classpath and configure the persistence.xml
to use a RESOURCE_LOCAL
transaction type. You can find kickoff examples in EclipseLink Wiki - Running JPA Outside Container. Here's an extract of relevance:
<persistence-unit name="LocalPersistenceUnit" transaction-type="RESOURCE_LOCAL">
<provider>org.eclipse.persistence.jpa.PersistenceProvider</provider>
<exclude-unlisted-classes>false</exclude-unlisted-classes>
<properties>
<property name="javax.persistence.jdbc.driver" value="oracle.jdbc.OracleDriver"/>
<property name="javax.persistence.jdbc.url" value="jdbc:oracle:thin:@localhost:1521:orcl"/>
<property name="javax.persistence.jdbc.user" value="scott"/>
<property name="javax.persistence.jdbc.password" value="tiger"/>
</properties>
</persistence-unit>
通过添加客户端项目,您可以在Web项目中重用客户端项目和JPA模型以及最终的DAO作为Web项目的一个模块。例如,在Eclipse上,您只需通过 Java构建路径>项目>添加将客户端项目添加到Web项目的构建路径,并配置部署程序集以允许它在 / WEB-INF / lib
中结束为JAR。
You can reuse the client project with JPA models and eventual DAOs in the web project by adding the client project as a module of the web project. On Eclipse for example, you just have to add the client project to the buildpath of the web project by Java Build Path > Projects > Add and configure the Deployment Assembly to let it end up as JAR in /WEB-INF/lib
.
最后,在您的Web项目中,您可以拥有另一个 persistence.xml
它基本上指向客户端项目的JAR文件并覆盖事务类型。
Finally, in your web project you can have another persistence.xml
which basically points the JAR file of the client project and overriddes the transaction type.
<persistence-unit name="WebPersistenceUnit" transaction-type="JTA">
<jta-data-source>jdbc/DataSourceName</jta-data-source>
<jar-file>lib/JavaProject.jar</jar-file>
</persistence-unit>
这样您就不需要在持久性中重复模型类。 xml
。
这篇关于Java EE 6框架仅用于Web应用程序还是我也可以将它用于客户端应用程序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!