In a project (war), I am using an EntityManager defined using a persistence unit. The definition of this persistence unit is made in a library that is packed as jar and installed on in Glassfish's libs. This lib is not included in the war.
Since I am using the persistence-unit defined in the library's persistence.xml, I have to copy it into my project.
I prefer not having to do this.
Is there a way, such that I don't need to copy this persistence.xml and just reference it somehow in the library or anything like that?
Related
Let's say I want to create a library which I will use in future projects but I also want to include EJBs in that library referencing other EJBs etc.. That library would also contain simple java classes. What is the best way to do that? How do I define the dependencies in this case? I thought I would define them with annotations. If the user of the library wants to configure other dependencies he will be able to do so by overriding them inside the ejb-jar.xml of his project. Has anyone done something like this in the past? How would you go about it when developing in Eclipse?
Basically my problem is that as far as I can tell if I simply create an ejb-jar which I am going to include in all my projects the ejb container is going to instantiate my e.g. MDBs at deployment time even if I don't need all of the MDBs that are contained inside my library but only some of them.
Is a solution to not define MDBs as EJBs with annotations or inside the ejb-jar.xml but only their dependencies?
What about the session beans? Will they be automatically instantiated even if I don't use them inside a project?
EAR files. Although not very common, you could include your library and its dependencies in an EAR file and distribute that.
What I'd like to do is to distribute the library in its own Jar file along with a documentation of dependencies (e.g.: a Maven' POM file or ivy's xml file). Either way, you'll need a dependency manager.
There is also an option to build a fat JAR file in which all the dependencies are exploded. I don't really like that. If I have to include dependencies, I'd go with EAR files.
In a .ear file, my EJBs are copied in several jar and war for some technical constraints. And only one of these jar contains the persistence.xml file where I have configured several persistence units.
myear.ear
|---- myjar1.jar
|-------- META-INF/persistence.xml
|---- myjar2.jar
|---- mywar.war
The problem is Jboss cannot find the persistence configuration for the classes within the jar "myjar2" and the war :
Could not get class configuration for ....EjbA.class due to the following errors: Can't find a deployment unit named xxxxx at
subdeployment "jar2.jar" of deployment "myear.ear"
To resolve that, I tried to create a new jar containing only the persistence.xml file but it doesn't work.
Any idea of how I could share my persistence.xml file to every jar without reorganising all my ear archive?
As per the JPA spec, it should be possible for you to define a persistence unit at the EAR level that is visible to all the submodules that you define in the same .ear:
8.2.2 Persistence Unit Scope
...
A persistence unit that is defined at the level of the EAR is
generally visible to all components in the application. However, if a
persistence unit of the same name is defined by an EJB-JAR, WAR, or
application jar file within the EAR, the persistence unit of that name
defined at EAR level will not be visible to the components defined by
that EJB-JAR, WAR, or application jar file unless the persistence unit
reference uses the persistence unit name # syntax to specify a path
name to disambiguate the reference.
However, in section 8:
NOTE: Java Persistence 1.0 supported use of a jar file in the root of
the EAR as the root of a persistence unit. This use is no longer
supported. Portable applications should use the EAR library directory
for this case instead. See [9].
So I would try to place the jar in the lib folder. If you need that module to be a EJB one, it must be in the root of the ear, so you can create a separate jar with the persistence.xml file.
So I have a standalone application and a web application to do, but the persistance classes that I've created in the standalone gonna be used in the web application too.
Since my standalone project have the hibernate.cfg.xml and the hbm.xml for each persistance class, how can I export this project like in a JAR and just import in the web application project and use the classes easily?
Use maven to create a jar containing persistence classes and *.hbm.xml files. In your webapp you can use this jar as a dependency and use the hbm files to configure your session factory bean.
I need to use some 3rd party jar in my project. The project is a Spring project and the jar is also using Spring.
Is there a way by which I can include the 3rd party jar in my project? I am finding it difficult to find each and every dependency of the 3rd part jar and inject it.
Shouldn't be a problem. Application contexts can be loaded independent of one another in the same JVM, generally. But if you're loading your bean definitions from a resource file in the classpath (e.g. using ClasspathXmlApplicationContext), make sure the location and name of your file does not conflict with the third-party JAR. For example, if they're both located at "/applicationContext.xml" in different JARs in the classpath, you will have a problem. Make yours unique.
I'm not clear exactly what you mean by "include".
If the 3rd party jar is defined as a dependency in your war project, it will automatically be bundled into the WEB-INF/lib folder of the war when it is packaged by the war plugin. Any class in the jar would then be on the classpath and therefore available to be referenced in your Spring configuration. Do you have a more specific requirement than this?
Also note that if the 3rd-party jar is a properly-defined Maven project, it's dependencies will be defined in its pom. Those transitive dependencies are also bundled into the war (unless you have them defined with a non-default scope in which case they might not be).
Any of the jars you find on the Maven central repository should be defined with all their transitive dependencies. If you're having trouble resolving them, please update your answer so that the relevant Maven credentials can be located.
Update based on your comment. Once the jar is on the war's classpath, you can reference any Spring configuration files it declares by importing them into your war's application context. You just specify the import in the form: "jar:file://jarName!/path/to/config.xml"
I would like to use the spring framework within an EJB3 project. In detail I would like to use the JDBC template class which should be instantinated from a given data source. When I put the spring.jar to my Jboss lib directoy everything is working fine. But when I put the JAR inside my EAR only there seems to be external dependencies from JDbcTemplate to other libraries. EARs/EJBs classloader try to instantinate the JdbcTemplate and shows me that he can not load the class because of external dependencies. It does not show me which additional JARs I have to put in.
Question: Does some body know which addtional JARs I have to include or even how I can search for depending JARs with external tool. I remember there is a tool which can do this, but I do not know its name anymore. I think something like jarjar etc.
Could anyone help please? Thank you.
This smells like an EAR config problem not an Spring problem. Are you sure that the jar is in the EJB's classpath? You might want to check the MANIFEST.MF file of the EJB's jar to verify this.