2

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?

I am using Netbeans 7.0 btw

Thang Pham
  • 38,125
  • 75
  • 201
  • 285

4 Answers4

2

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>

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.

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>

This way you don't need to repeat the model classes in persistence.xml.

BalusC
  • 1,082,665
  • 372
  • 3,610
  • 3,555
  • Hi BalusC. Thank you very much. I will try it out. One small question, in my development environment, I have to use Eclipse 3.2 for my project, do you know how to create Java EE project in Eclipse 3.2? – Thang Pham Jun 14 '11 at 18:45
  • 1
    *New > Dynamic Web Project*. Ensure that you're using "Eclipse for Java EE" version. Note that *Deployment Assembly* is called *Java EE Module Dependencies* in versions before 3.5. If you can, just upgrade to latest. It's free. – BalusC Jun 14 '11 at 18:49
  • Hi BalusC, thanks to your idea, I successfully able to create a standalone app client using JPA. I now need to add a web module on top of it. I have somes question and I posted them separatedly here, will it be possible if you can take a look at them? http://stackoverflow.com/questions/6387475/java-ee-6-how-to-add-web-module-on-top-of-application-client Thank you so much and have a wonderful day – Thang Pham Jun 17 '11 at 14:45
  • Hi, balusC, I have a question about what you said above.So I have a app client, using the persistence.xml like you describe above. I then create enterprise application (`ear`) that contains a web module (`war`) in it. I then add the app client (`jar`) to the `ear` package. then add app client jar to the web project lib (in netbean, when I add the jar in the web, its name show as dist/javaProject.jar). Then create a new persistence.xml in the web project, with `dist/JavaProject.jar`. I run into problem when i access method in my app client project. Any idea why?(Read below) – Thang Pham Jun 17 '11 at 17:22
  • (Read above first) The method in the app client that deal with JPA is not annotated as `EJB`. Usually, from Managed Bean, for me to access the EJB method, I inject EJB using `@EJB`, but I am not sure how to inject the app client here. Can I inject it using @Resource? – Thang Pham Jun 17 '11 at 17:25
1

Well, JavaEE is a compilation of different frameworks (EJB, JSF, JAXP, JAXB, WS, ...). Not all of them need a full featured application server.

It really depends on your requirements (and your knowledge on the different frameworks), but with the web module request in mind, the following approach might be useful:

  • Model your data with Entity EJB
  • Create your business logic with Session Beans (or POJOs which can be easily migrated to EJB)
  • Use the required additional frameworks (e.g. JAXB)
  • Start with your command line application

If your applications becomes more and more complex or you need additional features (like persistence with JPA, transaction control with JTA or a web framework like JSF) you can consider to use a application server.

Thor
  • 6,607
  • 13
  • 62
  • 96
1

There are a couple 'view' technologies in Java EE 6: web applications and application clients. The web application is the most common view technology used for Java EE 6, but you can create a 'native command-line' client of your EJBs and entity classes.

You can read about application clients in JSR-000316 Java Platform, Enterprise Edition 6 Specification 6.0 Final Release, section 'EE.10 Application Clients'.

There is an article that describes how to use NetBeans to create, deploy and execute a Java EE 6 application that uses an application client.

One of the primary impediments to the adoption of application clients has been burden of deploying them to desktops across a large number of clients. The GlassFish implementation of the Java EE 6 spec includes features that help lower these burdens.

vkraemer
  • 9,864
  • 2
  • 30
  • 44
  • Sadly that the JSR316 is a high level documentation, so it only introduce the idea but not implementation. But thanks anyway. :) – Thang Pham Jun 15 '11 at 14:45
  • @Harry Pham: I added a pointer to an article that may be helpful. – vkraemer Jun 15 '11 at 16:33
  • Thank you very much. I finish walk through the article. One think I want to ask you about is: they expose the EJB module via a `remote` interface, but does not explain why they did that. I have been with Java EE 6 for couples year, and I know if possible use `local` over `remote` interface. What are your thoughts here? – Thang Pham Jun 15 '11 at 16:45
  • @Harry Pham: http://stackoverflow.com/questions/1385717/why-do-we-need-separate-remote-and-local-interfaces-for-ejb-3-0-session-beans – vkraemer Jun 15 '11 at 17:00
0

It is not meant to build native client applications. What you are looking for would be Swing or RCP.

Rocky Pulley
  • 22,531
  • 20
  • 68
  • 106