Is it possible to have Web & JPA & EJB in 1 project in Eclipse? - eclipse

I just finished a tutorial which was done in NetBeans. The tutorial created only 1 project which was a Web Application Project under Java Web category. The tutorial has 1 entity using JPA annotations, 1 Stateless Session EJB using JPA annotations, 1 Servlet calling the EJB, and 1 JSP calling the Servlet. So basically this 1 project has all 3 items: Web & JPA & EJB.
How can I have such a project in Eclipse?
When I work on Eclipse I get confused about how many projects I would need for a web application which uses JPA and EJB.
Should I create 3 projects 1 each for Web & JPA & EJB? OR
Should I create 2 projects 1 each for Web & EJB and include JPA in both?

Just create the "Dynamic Web Project" right away with a minimum version of 3.0.
Since Java EE 6 ("Web Profile") you indeed don't necessarily need to create a separate project for EJBs. You can then use a subset of the EJB API in the WAR, also known as "EJB Lite". You can then easily create a single no-interface EJB class with just a state annotation (Stateless, #Stateful or #Singleton) and you're already there.
You don't need the EJB facet for a "Dynamic Web Project". EJB Lite is basically already covered by the "Dynamic Web Project". There's not much IDE magic (wizards, code generators, etc) needed for EJBs anyway. You only need to make sure that the version is set to a minimum of 3.0 (from Servlet 3.0; part of Java EE 6), and that you set the target runtime to a real Java EE application server (even if it's only "Web Profile"), such as WildFly, TomEE, GlassFish, Liberty, etc and thus not a barebones servlet container like Tomcat or Jetty. This way the EJB annotations will be readily available in the project.
You don't even necessarily need to enable the JPA facet. You'll only miss the JPA-related wizards and code-generators under the project options. But you can just write all JPA-related code all by yourself and still get it to deploy. After all, an IDE is just like notepad, but then with millions of features trying to make you more comfortable while writing code. The project facets basically enable/disable the available project options/wizards/code-generators.

Related

Maven archetype for java ee 6 and eclipse

I need to create a java ee 6 project which contains:
JSF
EJB
JPA
The Web-things should be compiled to a .war file, the ejb to the ejb-jar and overall into an ear file. The application will be deployed to a Glassfish v3.
I was looking for a maven archetype which I can use and integrate into Eclipse. However I haven't found one. Can you help me?
I consider Andy Gibson's Knappsack Archetype as a good starting point for what you are looking for. It gives you certain levels of working projects from a very basic setup to one filled with examples.
Alternatively, Adam Bien's Weblog is always a good place for finding simple solutions as for example a minimal project setup.
The Java EE MVC Security Archetype is a quickstart for websites that want user self registration and security configuration.
You can try it out on Openshift, though sometimes you have to wait a couple of minutes for openshift to load and start the application.
The archetype includes the following technologies:
Java EE MVC web application for Wildly 9 environment
JSF 2.2 and Bootstrap
JPA 2.1
H2DB (H2 Development Database)
JUnit/Arquillian/Drone/Graphene for testing
Java EE SecuritySupported by JBoss/Wildfly Database Module
Please note: This post is self-promotional.

Eclipse EAR project structure in eclipse

For the past years I have been working with Java EE 6 and simpler application structures, where only one ejb and one war project were present.
Now we have a more complex project, where I need to have additional modules:
- JPA package
- client interfaces for an automated tester application
So I have started creating the following projects in eclipse:
Client project
JPA project referencing the client project
EJB project referencing both client and JPA projects
WAR project, that should use services from the EJB project and pojo classes from JPA project
EAR project holding all of this
Packing the project together, I am facing different problems:
"A cycle was detected in the build path of project..." because multiple projects are referencing the client project, and it is packaged to the EAR
Error in annotation processing: {0}. java.lang.NoClassDefFoundError
I was trying to solve these problems. I have come up with solutions, but neither of them works.
Ex 1.: I have simplified the packing to only the JPA + Client + EAR, but this also gives me the cycle detected error. When I turn the error off, I get the 2nd error.
Ex 2.: Tried to simplify build path dependencies, but no luck.
I have also been thinking of merging the EJB + JPA projects, maybe that will work.
But my question is: using Eclipse for development, what is the desired (project and dependency) structure for an EAR app containing client interfaces, ejbs, jpa entities and a standalone tester app using only the client interfaces?
Thanks for your help!
First of all, for your components you won't need an EAR file, a WAR file is sufficient for Java EE 6 applications (see Java EE 6 tutorial on that topic).
Second, my recommendation is not using the project layout created by the eclipse wizard, but use a maven-based archetype (e.g. Knappsack Archetypes), which will give you an easier and standardized layout.

Is there any way to have a fully portable Java EE 6 deployment?

Is it possible to make a Java EE 6 application deployable on any Java EE 6 Container (like JBoss or GlassFish, etc) without using their modules / libraries?
If for example I want Hibernate or Weld then add these in my Maven pom.
In other words, is there any "vanilla" container or can JBoss or GlassFish be made "vanilla"?
I´m sorry... but to be honest I don´t understand your question.
Java EE applications are in most cases deployable an all containers - as long as they are not using packages or configurations which are specific to the chosen container.
Even if you add libraries in your POM and the applications are packaged as WAR or EAR this should work.
Weld is not needed because the API is part of Java EE 6. If you want to use JPA you also don´t need hibernate.

Access EJB from JAX-RS

While Developing a Java EE Web app, I would like to know simple ways to access an stateless EJB object through Rest.
This project needs no Dynamic Web from Java, as Client side is fully javascript deployed, so communication is done only with Ajax calls.
As I've read one way would be to create 2 EJB one for functionality other to represent Web Service.
Is there a way to avoid the Web Service part? (avoid completely the WAR) Maybe with DI?
Java EE 6, EJB 3.1, Eclipse desired. I also believe the right application server would be Glassfish instead of Jboss, due to it's compatibility with EJB 3.0
No interesting in using JAX-WS
With EJB 3.1 you can actually publish a session bean as a JAX-RS web service, if you package your (properly annotated) session bean inside a WAR.
So, the simplest solution I can think of would be to create a WAR (a dynamic web app if you're using Eclipse) and create a JAX-RS annotated stateless session bean inside the web app. You don't need any EJB projects at all.
Not really. In Java EE 6 you can directly publish a Session Bean as a JAX-WS web service, but not as a JAX-RS web service. You're pretty much stuck with creating a WAR that hosts that JAX-RS services that front the EJBs themselves.

How to deploy EJB on server?

I am learning EJB3 from last few days. I have many questions regarding EJB, application servers and deployment of EJB.
To start with, I have created one simple helloworld stateless session bean but I don't know how to deploy it on server. It has single bean class, bean interface and one servlet client. I have used eclipse to develop this project.
None of the books that I read gives step by step details about how to put EJB on server and how to access those beans.
I have JBoss 6 server and I also have Java EE budle downloaded from sun website. Does this Java EE bundle contains Glassfish server? or do I need to download it separately?
Can anyone please give me step by step details of how to put my bean and its client on server (JBoss or Java EE)?
And why do we need to include bean interface class in EJB client code? I mean either we need to keep client and bean in same package or if we keep them in seperate packages we need to import bean interfaces in client code. Am I right?
With Java EE 6, you can package your Servlet and your EJB in a WAR (either package your EJB in a JAR and put it in WEB-INF/lib or simply put all classes in WEB-INF/classes). And to deploy this WAR, copy it to:
$GLASSFISH_HOME/domains/<domain1>/autodeploy for GlassFish v3*
$JBOSS_HOME/server/default/deploy for JBoss 6
With Java EE 5, you'll have to package your code in a EAR.
And if you want to deploy your application from Eclipse (using the Eclipse WTP), you'll have to install the appropriate server adapter. For Eclipse Galileo and GlassFish (there is currently no adapter for JBoss 6 AFAIK), right-click the server view, select New > Server, click on Download additional server adapters and select the GlassFish adapter. Finish to define your new GlassFish v3 Java EE 6 server and deploy your application on it (right-click on your application then Run As > Run on Server). For Eclipse Helios and GlassFish, you can follow the link given by #VonC (manual install) or check this answer (install via the Update Site).
You need to add GlassFish to your Eclipse installation (see GlassFish plugin for Eclipse).
The full process is described here (with the latest Eclipse Helios 3.6M6)
You should export as EJB into your jboss<version>\server/default/deploy folder and then add the build path for it on the servlet's web project. You can "Run on Server" and choose an application server just like you would in any project, no need to export the WAR although if you do that, you're gonna have to re-export your WAR every time you modify your code
AFAIK there's no Eclipse plugin for JBoss 6 but Eclipse provides one for 5.1