Error injecting entity manager with Wicket/JPA - java

I have an app using Wicket for the presentation layer with CDI/Weld, JPA 2.0, EJB 3.1 etc. (Java EE 6) deployed on GlassFish v3.0.1.
When I try to inject an EJB into a wicket page using #EJB I get the following error:
java.lang.IllegalStateException: Unable to retrieve EntityManagerFactory for unitName
When I try to inject using #Inject, I get the following error:
java.lang.IllegalStateException: Unable to convert ejbRef for ejb UserRepository to a business object of type class
I believe the problem is stemming from JPA. I am using the exact same configuration that I used with a JSF application which worked properly, so I am lost as to what the issue could be. The connection pools are set up properly and pinging correctly through GlassFish, I have included wicket-weld on the classpath and I have even tried using the old Java EE 5 wicketstuff project for wicketstuff-javaeeapi with the same results.
Any help would be appreciated.

If you get the exception:
Unable to retrieve EntityManagerFactory for unitName
It might mean it is not detecting your persistence.xml file. Make sure it's in the WEB-INF\classes\META-INF directory.
You can verify that your app has JPA enabled by going to the Admin Console in GlassFish, go to the Applications section and see if it shows something like [ejb, web, weld, jpa] for your app. If it doesn't show jpa then it's not finding your JPA config file.

Could always try to lookup the EJB via its standard "java:global" name. That should at least let you rule out wicket as a possible source of issues and get you a little closer to a working system.

Related

Spring #Transactional not working in ApplicationServer

We have a Spring-Boot application exposing some REST endpoints. We allow for this application to be operated standalone (as executable jar) or as a war to be deployed in a wildfly-11 application-server.
The class defining the REST-endpoints is marked #RestController #Transactional(REQUIRES_NEW) (both on class level, obviously). When running standalone, everything works as expected but when deployed in wildfly, the rollback on exceptions does not work. We established this by sending the exact same REST-message while operating on the exact same database.
We have confirmed via debugging that the final frames of the stacktrace is identical in both cases and especially in both cases we see a transactional-proxy around our REST-controller bean.
One difference would be, that within wildfly the application will use a jndi-datasource, prepared by wildfly while standalone the spring-boot will manage the database-connections.
Any idea what is wrong here?
Edit
I just tried explicitly invoking setRollbackOnly on the JtaTransactionmanager from within my code. The transaction will still commit. This sort of looks like a bug in Spring Boot to me.
Edit 2
Debugging further reveals that the transaction seems to be set to autocommit - every statement is immediately written to the database. This seems to be in violation to the annotation #Transactional and also to the fact that Spring creates a transactional proxy around my bean.
It's not a full answer - just a reasoning. JNDI is usally used at the app server layer whereas JDBC - at the application layer. At the App server layer are used global transaction settins that are overriding app settings. Follow the spring doc to get more
For reasons beyond my understanding the default transactional behaviour when deploying a spring-boot webapp to an application-server is auto-commit.
The solution to this problem is to enrich your application-configuration with the property spring.datasource.tomcat.default-auto-commit=false

Integrating Hibernate to Spring running on embedded jetty

I have just returned to Spring after 5 years of gap and it seems lot is changed. I have a task to create a REST Service using Spring with hibernate as an ORM, So far I am able to run a basic Rest Service using embedded jetty and able to make GET/POST calls, the next is to integrate hibernate into it so that the data fetch/Sent operation actually use MySQL instead of sending hard-coded response(which I have done currently).
The issue is earlier I used to work on Spring MVC using Tomcat where we had web.xml to define the dispatcher servlet and application-context which in turn were used to define hibernate config and other beans declaration, but now having embedded jetty I am not finding a way to integrate hibernate to my REST app, Is the way to add configuration is changed from XML to class based config. I searched over internet but maybe I am out of words or not using correct keywords, in short, Have no luck finding some solution to integrate hibernate to my Spring app which is using embedded jetty.
Could some please breif me about the recent changes or point me to the right tutorial ?
Any help appreciated, thanks in advance !
ps - I have used this example to progress so far https://github.com/pethoalpar/SpringMvcJettyExample
Yes , lot of changes in these 5 years and one of the game-changer is spring-boot
If you want to build a brand new project especially if you want to run the web application on the embedded container such as Jetty , you can check out spring-boot.It already provides integration with Jetty , hibernate and Spring MVC REST service.
The end result is that you just need to change a little bit configuration (most probably the DB connection info) and you can quickly get a production-ready REST service backed by JPA/Hibernate which can just run without any XML configuration anymore.
There are tons of tutorials in Internet talking about how to do it . You should easily find them out such as this using the keywords something likes "springboot webservice hibernate jetty" etc.

Lookup a EJB Bean using JNDI

I trying inject a EJB into my class using a InitialContext (JNDI). For this i use a Netbeans insert code mechanism:
after that Netbeans know witch injection have to use. #EJB annotation or JNDI Lookup. In my Example i have a simple, not managed class and what i want to do is inject a EJB Bean using JNDI. So Netbeans generate code for me as bellow:
problem is that. When Netbeans generate code for me. He change a web.xml file and add there ejb-local-ref node:
and when i trying a turn on my web application. I run glasfish and i always get following error:
Exception while deploying the app [mavenproject1-ear] : Error: Unresolved <ejb-link>: mavenproject1-ejb-1.0-SNAPSHOT#LanguagesFacade
i really dont know what to do. Can someone help with this issue. I will greatful for help.
Just remove the <ejb-local-ref> all together. It's defined for dependency injection and this is not your case here since you use JNDI to lookup the bean and set it in langaugeFacade variable. Just remove it and things will be fine.

How to use Spring Data JPA in a simple Servlet?

I'm very new to Spring, but I am working on a project which is using Spring Data JPA to generate repositories for JPA entities.
I'm currently adding a simple module to be able to show some data on a webpage. I have added a Servlet, but I am having trouble accessing the repositories from there.
I have added a ContextLoaderListener in web.xml, I'm referencing the jpa:repositories and persistence.xml in the applicationContext.xml, but I'm currently stuck with this exception:
No unique bean of type [javax.persistence.EntityManagerFactory] is defined: expected single bean but found 0.
But when I add an EntityManagerFactory in persistence.xml I get the following cryptic message:
java.lang.IllegalAccessError: tried to access field
org.hibernate.engine.spi.CascadeStyle.STYLES from class
org.hibernate.engine.spi.EJB3CascadeStyle
My question is: is what I am trying to do even possible? And if so, how?
Or should I just bite the bullet and use Spring MVC or something else entirely?
Note: this is just for a one-page web site and I'm trying to keep it as simple as possible.
In order to use Spring Data JPA you need to configure the underlying JPA implementation as you would typically do in Spring, see for example, infrastructure.xml and META-INF/persistence.xml in spring-data-jpa-showcase (since Spring 3.1 you can get rid of persistence.xml if you use packagesToScan property of LocalContainerEntityManagerFactoryBean).
Your second problem with IllegalAccessError looks like a classloading problem caused by presence of different versions of Hibernate jars in classpath.

Google Guice and JPA Injection - Strange error

Im currently working on a JPA Jersey Servlet and Im trying to use Guice for dependency injection. The Problem is, that I get the following error:
Exception while loading the app : java.lang.IllegalStateException: ContainerBase.addChild: start: org.apache.catalina.LifecycleException: com.google.inject.CreationException: Guice creation errors:
1) null returned by binding at com.google.inject.persist.jpa.JpaPersistModule.configurePersistence(JpaPersistModule.java:63)
but parameter 1 of com.google.inject.persist.jpa.JpaPersistService.<init>() is not #Nullable
while locating java.util.Properties annotated with #com.google.inject.persist.jpa.Jpa()
for parameter 1 at com.google.inject.persist.jpa.JpaPersistService.<init>(JpaPersistService.java:43)
at com.google.inject.persist.jpa.JpaPersistModule.configurePersistence(JpaPersistModule.java:67)
while locating com.google.inject.persist.jpa.JpaPersistService
for field at com.google.inject.persist.jpa.JpaLocalTxnInterceptor.emProvider(JpaLocalTxnInterceptor.java:33)
at com.google.inject.persist.jpa.JpaPersistModule.configurePersistence(JpaPersistModule.java:76)
2) null returned by binding at com.google.inject.persist.jpa.JpaPersistModule.configurePersistence(JpaPersistModule.java:63)
but parameter 1 of com.google.inject.persist.jpa.JpaPersistService.<init>() is not #Nullable
while locating java.util.Properties annotated with #com.google.inject.persist.jpa.Jpa()
for parameter 1 at com.google.inject.persist.jpa.JpaPersistService.<init>(JpaPersistService.java:43)
at com.google.inject.persist.jpa.JpaPersistModule.configurePersistence(JpaPersistModule.java:67)
while locating com.google.inject.persist.jpa.JpaPersistService
while locating com.google.inject.persist.UnitOfWork
for field at com.google.inject.persist.jpa.JpaLocalTxnInterceptor.unitOfWork(JpaLocalTxnInterceptor.java:36)
at com.google.inject.persist.jpa.JpaPersistModule.configurePersistence(JpaPersistModule.java:76)
2 errors
The code that i generating this error is:
install(new JpaPersistModule("theseen"));
filter("/*").through(PersistFilter.class);
If I use the "standard" way by instantiating a EntitiManager and uncommenting these lines, nothing happens.
EntityManagerFactory emf = Persistence
.createEntityManagerFactory("theseen");
EntityManager em = emf.createEntityManager();
em.getTransaction().begin();
em.merge(s);
em.getTransaction().commit();
works like a charm. So I think the setup if the database connection must be ok.
What could these to errors possibly be? I tested the same App on another test environment and it works! Both machines use Glassfish 3.1, Eclipse Indigo with m2eclipse and m2wtp integration. I use Guice 3.0 with guice-persist 3.0 and guice-servlet 3.0. The machine where the app is working runs Ubuntu 11.04 with OpenJDK, the machine where problems occure uses Windows 7 with JDK1.6v26. I normally use a Datasource provided by Glassfish, but even using a plain persistence.xml is not working.
I am seriously confused...
Any ideas what is wrong with Guice Injection for JPA?
Since nobody answered I went brute force:
Completely reinstall Glassfish and the WebApp worked...
Something must have been corrupted during the long months of testing here on my local machine.

Categories

Resources