pesistence.xml of other project - java

I have an API for my database model and JPA controllers and I add the jar of that AProject to my BProject. When I declare my API in BProject.
And the error is No Persistence provider for EntityManager named "MyPersistenceUnitName" in my "BProject".
Is it possible or should I create what i'm calling in my AProject?

Yes you can reuse the code you had in AProject.jar. According to persistence chapter of Java EE 6 Tutorial:
The JAR file or directory whose META-INF directory contains
persistence.xml is called the root of the persistence unit. The scope
of the persistence unit is determined by the persistence unit’s root.
Each persistence unit must be identified with a name that is unique to
the persistence unit’s scope.
Persistent units can be packaged as part of a WAR or EJB JAR file or
can be packaged as a JAR file that can then be included in an WAR or
EAR file.
If you package the persistent unit as a set of classes in an EJB JAR
file, persistence.xml should be put in the EJB JAR’s META-INF
directory.
If you package the persistence unit as a set of classes in a WAR file,
persistence.xml should be located in the WAR file’s
WEB-INF/classes/META-INF directory.
If you package the persistence unit in a JAR file that will be
included in a WAR or EAR file, the JAR file should be located in
either
The WEB-INF/lib directory of a WAR
The EAR file’s library directory

Related

Class Loading issue in Wildfly 16

During my migration from weblogic to wildfly 16, I need to implement the concept of a shared library but all I am is getting class loading issues. I am new to the concept of modules.
I created a folder for the library with jars in modules and created a module.xml. I used jboss-deployment-structure.xml to map the dependency to the shared library. The EAR gets the external jar but I am getting a class not found for the jar inside the EAR. It seems the jar inside the EAR is being excluded when I add the jboss-deployment-structure.xml to the EAR. Without the
jboss-deployment-structure.xml I am able to deploy and login to my application but cant implement the functionalities of the shared jar.
The above image is the jboss-deployment-structure.xml I used.
The above image is the module.xml I used.
This is the server log I got on deployment with reference to the module created, It shows that the file "FileItem" is not found in the location I created but the jar with that class is already present in the EAR. Why is it not being read ?
According to the error in the screenshot, JBoss is not able to resolve your dependency com.lib. The module you created i.e. com.lib should be present in one of the following locations :
$JBOSS_HOME/modules/
$JBOSS_HOME/modules/system/layers/base
e.g. For location according to 1, The directory structure will be like :
$JBOSS_HOME/modules/com/lib/main
You will have following files at this location
module.xml
ebsUtility.jar
SessionHandler.jar

How to load spring beans for multiple jar file in a war based application

I have a war based spring web application project which internally has multiple jar files. I am using maven setup to build jars and war file. Each jar file has a set of beans that needs to be loaded and i am not able to do so.
In each of the jar file i have defined a beans.xml file . But the beans are not getting loaded automatically. I have tried loading the beans.xml file from:
a) src/main/resources
b) src/main/resources/META-INF
c) src/main/resources/META-INF/spring
It doesnt work.
My Question: How to prepare the application context for such scenarios? War based app with multiple jars.
If your are packaging your application as a webapp one, then you can simply add a file named yourservletname-servlet.xml and include all resources from your jar files using the <import /> element.
Spring, behind the scenes, will scan the file mentioned above by default including all beans declared in the files imported.
Here is how your servletname-servlet.xml should look like (xml namespace and schemas declaration are ommited for brevity sake):
<beans>
<import resource="classpath:/META-INF/beans.xml"/>
</beans>
I suggest the use of the META-INF as your context config files location.
This will scan all bean declaration files named beans.xml under META-INF folder under the root of your classpath, which assumes that those files must be under src/main/resources/META-INF/ in your project structure when using Maven as your build tool (so they can get copied directely under jar_root_path/META-INF/).
Otherwise, if you are not using the default -servlet.xml file, you can specify a custom application context descriptor using the contextConfigLocation as follows:
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>application-context.xml</param-value>
</context-param>
Try simple
import resource="classpath*:/META-INF/beans.xml"/
Where each jar contains beans.xml file under META-INF folder.It will scan each jar and load beans.xml file and creates beans based on these XMLs files.
You mention beans.xml, is this a CDI project or a standard Spring project ?
Using maven, everything under src/main/resources gets packaged at the top level of your JAR. So if you had a file in src/main/resources/META-INF/beans.xml, then you should load it using "/META-INF/beans.xml" or define it in your spring context as "classpath:/META-INF/beans.xml".

How to inject EJB in a jar file to a severlet in another war file?

I have a project which at the end will be packaging to an ear file, this ear file contains two eclipse project, one for EJB and it will be packaging to a jar file and another project is a web project, which will be packaging to a war file.
a serverlet is be dev in the war file and all EJB in the jar file.
I know we can use JNDI to ref EJB, but how can I use dependence inject to inject the EJB to my war file?
It should be sufficient to just annotate it with #EJB in your servlet class:
public class MyServlet
extends HttpServlet {
#EJB
private ApplicationLogicEJB logicEJB;
// ...
}
If your .ear file has an application.xml, that file must contain a <module> element for the EJB .jar file as well as a <module> for the .war file.

Persistence.xml file usage without container [packaging]

I am trying to use jpa/hibernate framework with a simple java console application without a container. According to jpa documentation the persistence.xml file should be placed under the folder META-INF on the src folder.
The issue came when trying to package the application as a simple jar file then the persitence.xml file will be within the jar file generated (since it should be placed on the src folder).
In this situation the persistence.xml file is not easy accessible to modify the application configuration like the DB URL, time out,the hibernate logs...
I tried to put the META-INF/persistence.xml outside the src folder and added it to the CLASSPATH but an error saying Could not find any META-INF/persistence.xml file in the classpath is always thrown.
Is there any way to keep the persistence.xml editable and accessible once the application is packaged and deployed on production like any other classic configuration file (eg .properties files).
Thank you
I think you don't need to make persistence.xml editable since you can provide additional properties when calling Persistence.createEntityManagerFactory() (or using similar Hibernate-specific mechanisms).
So, you can use persistence.xml for static configuration only, and configure dynamic properties using your own configuration mechanism.

persistence.xml for ejb deployed in glassfish

folks,
i am trying to deploy a ear with a ejb in the ear root. the ejb has a persistence.xml file where the tag has to be a jar file in the Glassfish's domain lib. (this i have put it there because the jar is used across ears)
so what exactly should be the content of the <jar-file>*****/myjar.jar</jar-file> tag, for the jar in the glassfish's domain lib directory.
thanks fr yure time,
rajan.
The path should be either an absolute path to a JAR (not recommended) or the simple file name for a JAR inside the EAR deployment file. So, assuming you have an EAR with the jars "ejbs.jar", "jpa.jar" and "myjar.jar", the <jar-file> tag in jpa.jar for myjar.jar should be:
<jar-file>myjar.jar</jar-file>

Categories