multiple EJB jar file inside one EAR sharing the same persistence.xml - ejb-3.0

I am using JBoss 4.2.3 application server.
I have an EAR that currently contains 2 EJB (EJB 3) jars.
In the main EJB jar I have persistence.xml file with the data source configurations.
From the secondary EJB jar file I can successfully call the the EntityManager and read\write entities that are defined in the main EJB jar that contains the persistence.xml
If I try to put an entity in the secondary EJB jar module and then persist it via the EntityManager that uses the persistence unit from the main EJB I get an exception:
Caused by: org.hibernate.hql.ast.QuerySyntaxException: [ENTITY-CLASS-NAME] is not mapped
The solution I found is to create another persistence.xml in the secondary EJB module and change the persistence unit name.
I am not sure whether what I tried to do is correct? Can you please contribute whether I can have a way to share the same persistence.xml withing multiple EJB modules in the EAR (in a way I can have entities in both EJB modules)?

Put the persistence.xml and entities into a JAR in the top-level lib/ directory of the EAR. This should allow the persistence units to be used by all modules in the EAR.

Related

ClassNotFoundException for OracleConnection$CommitOption when using EntityManager

I'm trying to learn the JPA and Oracle DB. I made a maven ear project with an ejb, shared and a web module. I added a glassfish-resources.xml that contains a jdbc connection pool and a jdbc resource to an Oracle database. I can make it work using the DataSource class and JDBC, but when I add an EntityManager to the EJB class (without using it yet), and add a persistence.xml I get the exception java.lang.ClassNotFoundException: oracle.jdbc.OracleConnection$CommitOption. This class is part of the ojdbc-6.jar downloaded from the oracle site and is in the lib folder of my ear. The jar is also added to the ear's Class-Path in its manifest. I've no idea how to make this work.

Singleton EJB not found

I've an EAR with an EJB and WAR project in it. I use Netbeans and the Maven building tool. In my EJB there is one EJB, a singleton class: http://pastebin.com/f8jXw0TK
Here is a screenshot of my project structure in Netbeans: https://gyazo.com/09e075972279f637de2d4d98fe9a1ac7
When I deploy the EAR or the EJB-JAR in Glassfish (localhost). I get the follow error message:
Error occurred during deployment: Exception while deploying the app [arp-auth-business-1.0] : Invalid ejb jar [arp-auth-business-1.0]: it contains zero ejb. Note: 1. A valid ejb jar requires at least one session, entity (1.x/2.x style), or message-driven bean. 2. EJB3+ entity beans (#Entity) are POJOs and please package them as library jar. 3. If the jar file contains valid EJBs which are annotated with EJB component level annotations (#Stateless, #Stateful, #MessageDriven, #Singleton), please check server.log to see whether the annotations were processed properly.. Please see server.log for more details.
I've added a simple stateless bean to the EJB JAR, and then it deployed. But then I saw just the simple stateless bean I created, but not my singleton class.
What could be wrong? The server.log shows the same error I get in Glassfish but with a exception stack from the loader. No error from the singleton class or something like that...
Still trying but can't figure it out. Maybe it's maven? But the jar and war file are in the ear file. And in the jar file there is my singleton class.
Kind regards

How do you use #Inject with an ejb-jar with (one or more) WAR files?

Working with GlassFish, trying to be tidy, I would like to put all of my business logic into a single EJB JAR. I then have 2 WAR files.
app-frontend-war
app-backend-war
app-logic-ejb
Each of the WAR files need to use the EJBs that are within the app-logic-ejb JAR. This EJB JAR holds the main persistence unit. But I am finding that #Inject of any app-logic-ejb EJB's from any Java within the WAR files are not working.
Also, I am trying to avoid using an EAR.
With the help of the Java EE 7 tutorial I have finally worked it out.
Even though the beans in app-logic-ejb are on the same GlassFish they need to be annotated as remote beans. The WAR files then use #EJB injection (instead of #Inject) into the correct interface within the WAR file.
#EJB
TestBeanInterface t;
To share the interface between the EJB JAR and the War files a Java Library is needed.
So to make it work it turns out I need:
app-library
app-logic-ejb
app-frontend-war
app-backend-war

Maven EAR project won't register Faces Converter

My Maven EAR project has two WAR module and one EJB module. There is a FacesConverter class in EJB module and when I try to use it from one of the WAR module it throws an exception. I register this converter with annotation #FacesConverter("org.util.ObjectConverter") use it in a JSF page
javax.servlet.ServletException: Expression Error: Named Object: org.util.ObjectConverter not found.
When ObjectConverter is in WAR module it works fine, but it won't load from the EJB module.
What am I missing here?
Cheers
Why do you put it in EJB module? Front-end (read: JSF) artifacts should go in WAR module. EJB module should only contain business services which are supposed to be reuseable to front-ends other than JSF, such as JSP/Servlet, Struts2, SpringMVC, JAX-RS, etc. The EJB module should absolutely have no single line of javax.faces.* import/dependency in the code.
JSF doesn't look for converters (let alone any other JSF related artifacts such as validators, managed beans and Facelets files) in EJB module, but only in WAR module. Just keep them in the WAR module. Whatever code you think need to share between the WAR and EJB modules should be refactored into a separate Java project which end up as a common JAR file in EAR module (note that this should in turn also contain no JSF-specific artifacts.

Spring 3 in jar configuration (auto configuration)

I'm looking for some kind of "best practice" informations about Spring jar configuration. I have a web project (war) and I need connect some jar libraries - my jars which contains additional functions. These jars contains Spring services. But when I connect jar, the service class did not work, because Spring don't know about that. So I need to tell Spring about this by “package auto scan” configuration inside my jar.
The final solution must be war project (main functions) and some additional jars which contains other functions. When I add jar into war project, I don't want to change configuration in applicationContext.xml (in war). I want minimal dependency to war project. I was thinking, when if I place applicationContext.xml to META-INF folder in jar it will be auto loaded by Spring, but it is not.
Do you know how can i solve this? May be some kind of “after startup dynamic configuration” :-). thanx
If you are trying to load annotated beans from the jars into your war's Spring context, you can set up a component scan in the war's context xml file to scan the packages in the jars.
If you are trying to load beans defined in XML files from the jars, you can include them using something like this in your war's Spring context xml file:
<import resource="classpath:path/to/config/in/jar/beans-from-jar.xml"/>
You shouldn't need to have your jar know anything about your war this way. You just scan the annotated beans and/or import the config from the jar.

Resources