Using hibernate in Java EE app - java

I am maintaining following structure for my project
Web - Web Project
Model - EJB Project
Persistence - Java project having data classes and their mapping for Hibernate
Pokuri - EAR Project
As we know we can give jar file to hibernate configuration to load mapping information from jar. As I deploy EAR on to server I just want to build SessionFactory from mapping files in Persistence jar. But I am unable to get the path for that jar. Plz guide me to get the jar path. Or suggest me the best way to build SessionFactory and bind the same to a JNDI name.

Take a look at this thread - it may contain useful information.
Also, make sure your jar is mapped in your application.xml as an ejb module.

Related

unable to find spring bean xml file in another module of application

My application has two separate maven projects.The first project is core project which has dao and bean classes. The second project is web project which is having spring rest classes. This web project has web.xml and spring-servlet.xml files. The core project has spring.xml file which has jdbctemplate and other bean definition codes like and datasource information.
I have written this code in spring-servlet.xml of web project to import the spring.xml file of core project.
<beans:import resource="classpath:/mycarecore/src/main/resources/spring.xml"/>
Now, when I am building the war file, the war builds fine but when I deploy the war on server and starts the server, it gives file not found exception for spring.xml file. I have given the project reference in maven dependency and I can see the jar of core project inside the war generated war. I have done multiple changes in file path, but it didn't help. Because of this, I am also not able to use jdbctemplate in my dao layer.
Please help.
The url for the classpath seems like an absolute url. This will work only if the spring.xml is located at /mycarecore/src/main/resources/spring.xml path on your computer. Otherwise you should simply use: classpath:mycarecore/src/main/resources/spring.xml.
From Spring documentation:
"You can always use fully qualified resource locations instead of relative paths: for example, "file:C:/config/services.xml" or "classpath:/config/services.xml". However, be aware that you are coupling your application’s configuration to specific absolute locations." (http://docs.spring.io/spring/docs/current/spring-framework-reference/html/beans.html)

Share ejb jar file with multiple wars in Wildfly

I'm aware that this is a very simple issue, but as I'm new to Wildfly I haven't had success trying to accomplish it.
In the old days of JBoss 4.2, when I wanted to share an ejb jar file with multiple war files I just deployed the ejb jar file to the application server and configured jndi in my war projects with a file 'jndi.properties' placed in some source directory in the war files, like this:
jndi.properties
---------------
java.naming.factory.initial=org.jnp.interfaces.NamingContextFactory
java.naming.factory.url.pkgs=org.jboss.naming:org.jnp.interfaces
java.naming.provider.url=jnp://localhost:1099
Now, with Wildfly 8.1, this approach is not working anymore. I don't know if the contents of the jndi properties file changed or if I have to do something else. So, I ask: how do I share an ejb jar file with multiple wars in Wildfly 8.1 (I don't want to use ear files for this)?
Thank you in advance.
Marcos
PS.: Cross-posted: https://developer.jboss.org/thread/249133
just deploy the jar file and add adependency to your war file (either in Manifest.MF or in jboss-deployment-structure.xml from your war to your jar. Then you should be able to do JNDI-lookups using java:global/... or using CDI for injecting the beans using #Inject (for this approach you will need to activate CDI using beans.xml)
see also:
Wildfly class loading
Wildfly deployment descriptors
CDI reference
Application deployment

Add shared project jars in websphere shared library

We can add project dependency jars (like spring jars, logging etc) in shared library, can we add shared project jars (like one that communicate with Database) in shared library?
Scenario:
I have these projects
WebApp
WebAppEAR
CommonDB
I am using IBM Websphere server, hibernate, spring mvc
I added all spring, hibernate jars including CommonDB.jar in Websphere shared library. WebApp Application is running successfully with spring controllers but when my application try to hit DB it throws exception.
Caused by: java.lang.IllegalStateException:
Actually WebApp is not loading <bean> DataSource. DataSource class is located in CommonDB project.
When I include CommonDB.jar in Deployment assembly of EAR WebApp runs successfully and fetches data from DB.
I want to exclude CommonDB.jar from Deployment assembly and only want to add it in shared library.
Adding jars in shared library will work. Try figuring out which beans are not initialized and add them in Spring file to initialize. Also check other bean properties for dependency.

Distributing a persistance class that uses Hibernate

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.

persistence.xml not found on glassfish deployment descriptor (IntelliJ)

I have an EJB and a WEB module in my IntelliJ, created 5 entities that I would hope to load into a database using annotations.
I go to project settings, under modules, and select that I want to add JPA facet, I know have a EJB, WEB and JPA facet. I use eclipselink, so I checked that under JPA settings, I then added my persistence.xml file, configured the file.
Problem is that when deploying, it simply will not deploy the persistence.xml file, which means I can not find it under my glassfish 3.0 server when selecting my application under deployment descriptors. I only find my sun-web-ejb.xml under the EJB module. persistence.xml is not under the WEB module either, but I assume it should be under the EJB module anyway.
Is it intended that my persistence.xml should not be loaded on the server so that I can find the xml file under GF admin console (applications -> EJB module of mine -> deployment descriptors) ?
If this is not intended, is there anything in particular I need to do in IntelliJ to make persistence.xml being deployed on server? Maybe its something in particular I need to do with my JPA facet?
Thank you for any feedback on this, I can ofcourse provide more detailed information about my setup if neccessary.
I only find my sun-web-ejb.xml under the EJB module.
That's already weird. If I'm not wrong, this is a proprietary deployment descriptor for the Web module.
persistence.xml is not under the WEB module either, but I assume it should be under the EJB module anyway
It all depends on how you package and deploy your entities, which can be done in several ways (see What You May Need to Know About Persistence Unit Packaging Options).
If your entities are packaged in an EJB-JAR, the persistence.xml must be placed in the META-INF directory.
But you could package them inside the Web module, this would be simpler in your case. See the above link.
Is it intended that my persistence.xml should not be loaded on the server so that I can find the xml file under GF admin console (applications -> EJB module of mine -> deployment descriptors)?
I suggest to check that the physical JAR contains the META-INF/persistence.xml.
If this is not intended, is there anything in particular I need to do in IntelliJ to make persistence.xml being deployed on server? Maybe its something in particular I need to do with my JPA facet?
Can't say. Maybe the following link could help: Enabling JPA Support.

Categories

Resources