Webapp cannot find Persistence Unit. No persistence unit named 'X' is available in scope - maven

I am trying to run my webapp on my weblogic 12c server using EJB 3.1, JPA 2.0, M2E-WTP, and JSF 2.1 technologies and continue to get the following error:
javax.ejb.EJBException: EJB
Exception: : java.lang.IllegalArgumentException: No persistence unit named 'X'
is available in scope Webapp. Available persistence units: [] at
weblogic.persistence.ModulePersistenceUnitRegistry
.getPersistenceUnit(ModulePersistenceUnitRegistry.java:130) at
weblogic.persistence.BasePersistenceContextProxyImpl
.<init>(BasePersistenceContextProxyImpl.java:40) at
weblogic.persistence.TransactionalEntityManagerProxyImpl
.<init>(TransactionalEntityManagerProxyImpl.java:31) at
weblogic.persistence.EntityManagerInvocationHandlerFactory.
createTransactionalEntityManagerInvocationHandler
(EntityManagerInvocationHandlerFactory.java:20) at
weblogic.persistence.PersistenceManagerObjectFactory
.createPersistenceContextProxy(PersistenceManagerObjectFactory.java:66) at
weblogic.persistence.PersistenceManagerObjectFactory
.getObjectInstance(PersistenceManagerObjectFactory.java:31) at
javax.naming.spi.NamingManager.getObjectInstance(NamingManager.java:304)
at weblogic.jndi.internal.WLEventContextImpl.lookup(WLEventContextImpl.java:251) at
weblogic.jndi.internal.WLContextImpl.lookup(WLContextImpl.java:406) at
weblogic.j2eeclient.java.ClientReadOnlyContextWrapper.lookup
This is the EJBBean that is triggering the exception
#Stateless
public class QuoteSessionEJB implements QuoteSessionEJBLocal {
/**
* Default constructor.
*/
public QuoteSessionEJB() {
// TODO Auto-generated constructor stub
}
#PersistenceContext(unitName="X")
private EntityManager em;
/**
* Returns a list of Quote objects in the database
* #return List<Customer>
*/
public List<Quote> getQuote() {
Query query = em.createNamedQuery("findQuotes");
return query.getResultList();
}
}
Here is the persistence.xml file which is located under src/main/java --> META-INF which eclipse built.
<?xml version="1.0" encoding="UTF-8"?>
<persistence version="2.0" xmlns="http://java.sun.com/xml/ns/persistence"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/persistence
http://java.sun.com/xml/ns/persistence/persistence_2_0.xsd">
<persistence-unit name="X" transaction-type="JTA">
<provider>org.eclipse.persistence.jpa.PersistenceProvider</provider>
<jta-data-source>localJTA</jta-data-source>
<exclude-unlisted-classes>false</exclude-unlisted-classes>
<properties>
<property name="eclipselink.target-server" value="WebLogic"/>
<property name="eclipselink.logging.level" value="FINEST"/>
</properties>
</persistence-unit>
</persistence>
I have spent hours searching the web for answers with no avail. I don't understand why the persistence unit X in my persistence.xml cannot be located...
I'm hoping someone has ran into this same problem or know hows to fix it.
Any help is greatly appreciated!
Update: Per better_use_mkstemp comment below I moved the META-INF folder containing my persistence.xml from src/main/java to WEB-INF/classes. I have to create the classes folder in eclipse. This worked! Much appreciated!
Following up question: Doing some more research I found that putting the META-INF folder in src/main/resources would also solve the problem, however it did not. I dont know if this is an issue with Maven's M2E-WTP eclipse plugin or I am simply misunderstanding something here. Below is a screen shot of the folder structure.
By browsing the war file the maven install command creates, I can see that the META-INF folder IS correctly placed in the WEB-INF/classes folder. But simply publishing to the server within Eclipse does not work. Unless I manually create the classes folder under WEB-INF and manually drop/move the META-INF folder in there I can't get it to work inside the IDE. Also it doesn't seem to make sense to have a copy of the META-INF folder in two places as a solution.

try to replace in your persistence.xml:
<persistence version="2.0" xmlns="http://java.sun.com/xml/ns/persistence"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/persistence
http://java.sun.com/xml/ns/persistence/persistence_2_0.xsd">
for:
<persistence version="1.0" xmlns="http://java.sun.com/xml/ns/persistence"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/persistence
http://java.sun.com/xml/ns/persistence/persistence_1_0.xsd">

Related

Settng the schema of a Hibernate entity extra to the default schema configuration

I have a Hibernate / Spring 4 application, where the default schema of the entity classes is already configured, e.g. they end up mapped as testschema.table or prodschema.table.
However there is another entity class which needs its own configurable schema, e.g. only this entity must be mapped to testschema2.anothertable or prodschema2.anothertable.
Nice would be something like this:
#Entity
#Table(name="anothertable", schema = "${db.AntherEntitySchema}")
public class AnotherEntity {
// ..
}
where the schema gets injected from the properties file, but such a feature seems only to work with the #Value annotation.
Any idea how to proceed?
We solved it by this applicationContextPersistence.ctx.xml
<bean id="entityManagerFactory"
class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean">
<property name="dataSource" ref="dataSource" />
<property name="persistenceXmlLocation"
value="classpath:META-INF/persistence-${env}.xml" />
<!-- .. -->
</bean>
where Spring can substitute the ${} expression from a property file.
We then have a persistence-dev.xml etc which features a specific orm-dev.xml:
<?xml version="1.0" encoding="UTF-8"?>
<persistence
version="2.0"
xmlns="http://java.sun.com/xml/ns/persistence"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/persistence
http://java.sun.com/xml/ns/persistence/persistence_2_0.xsd">
<persistence-unit name="fooUnit" transaction-type="RESOURCE_LOCAL">
<provider>org.hibernate.jpa.HibernatePersistenceProvider</provider>
<mapping-file>META-INF/orm-dev.xml</mapping-file>
</persistence-unit>
<!-- .. -->
</persistence>
The orm-dev.xml is now responsible for the entity mapping:
<?xml version="1.0" encoding="UTF-8" ?>
<entity-mappings xmlns="http://java.sun.com/xml/ns/persistence/orm"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/persistence/orm
http://java.sun.com/xml/ns/persistence/orm_1_0.xsd"
version="1.0">
<description> XML Mapping file</description>
<package>foo.server.model</package>
<entity class="AnotherEntity">
<table schema="testschema2" name="anothertable" />
<attributes>
<!-- .. -->
</attributes>
</entity>
</entity-mappings>
Finally we removed the mapping annotations from the AnotherEntity POJO, as this one is now mapped via the orm-dev.xml file. The other enitity classes kept their annotations.
Note: We use the Spring Tool Suite flavour of Eclipse. This IDE expects a persistence.xml, so to get rid of an error message we kept a minimal persistence.xml to not have to remember the IDE option which deactivates the validator.

No Spring Bean found in EJB with SpringBeanAutowiringInterceptor

I need some help: I have one EAR-File, containing one WAR-File, one EJB-Jar-File and some "shared" libs:
aopalliance-1.0.jar commons-logging-1.1.1.jar log4j-1.2.16.jar spring-aop-4.0.5.RELEASE.jar spring-beans-4.0.5.RELEASE.jar spring-context-4.0.5.RELEASE.jar spring-context-support-4.0.5.RELEASE.jar spring-core-4.0.5.RELEASE.jar spring-expression-4.0.5.RELEASE.jar
The War File has a Context initializer which find the spring config and loads everything well.
I now want to use another Spring Context for the EJB Jar.
My EJB is defined as
#Stateless(mappedName = "ejb/SpringRocks")
#RemoteHome(com.ibm.websphere.ola.ExecuteHome.class)
#Interceptors(SpringBeanAutowiringInterceptor.class)
public class WolaUseCaseOne {
#Autowired
private DummyService dummyService;
/* ...More stuff here */
Inside the EJB-JAR, there is also a beanRefContext.xml:
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd">
<bean id="myEjb" name="myEjb" class="org.springframework.context.support.ClassPathXmlApplicationContext">
<constructor-arg value="classpath*:META-INF/spring/simpleEjb.xml" />
</bean>
</beans>
The simpleEjb.xml is is also inside the EJB-Jar and is defining a very simple Bean:
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd">
<bean id="myDummyService" class="com.provinzial.beispielanwendung.batch.wola.DummyServiceImpl" />
</beans>
As described, the WEB Part works perfect, but when the EJB is called, the SpringBeanAutowiringInterceptor is called, but seems to do nothing. What do I have to do, to get a Spring Context created?! My hope was that it is initialized when the EJB is created. I created a Subclass of SpringBeanAutowiringInterceptor with some loggers, but the class is only created, no method is called !
What else do I have to do? Or does anybody have a valid EAR File example?
I think the Problem is that inside the EJB Module no context is initialized...
Greets
Timo
I was facing similar issue with my EJB (no WAR). This is what fixed mine,
I was missing the spring-aop jar on my classpath. I see you have it so good there.
In my ejb-jar.xml file, I set the meta-data flag to true so I did not get prompted on deployment to complete.
I set to "false" for one deployment to see what IBM generated for me. In the ejb-jar.xml it added the following (my MDB is named TaskMDB),
<assembly-descriptor>
<interceptor-binding>
<ejb-name>TaskMDB</ejb-name>
<interceptor-class>org.springframework.ejb.interceptor.SpringBeanAutowiringInterceptor</interceptor-class>
</interceptor-binding>
</assembly-descriptor>
<interceptors>
<interceptor>
<interceptor-class>org.springframework.ejb.interceptor.SpringBeanAutowiringInterceptor</interceptor-class>
<post-activate>
<lifecycle-callback-class>org.springframework.ejb.interceptor.SpringBeanAutowiringInterceptor</lifecycle-callback-class>
<lifecycle-callback-method>autowireBean</lifecycle-callback-method>
</post-activate>
<pre-passivate>
<lifecycle-callback-class>org.springframework.ejb.interceptor.SpringBeanAutowiringInterceptor</lifecycle-callback-class>
<lifecycle-callback-method>releaseBean</lifecycle-callback-method>
</pre-passivate>
<post-construct>
<lifecycle-callback-class>org.springframework.ejb.interceptor.SpringBeanAutowiringInterceptor</lifecycle-callback-class>
<lifecycle-callback-method>autowireBean</lifecycle-callback-method>
</post-construct>
<pre-destroy>
<lifecycle-callback-class>org.springframework.ejb.interceptor.SpringBeanAutowiringInterceptor</lifecycle-callback-class>
<lifecycle-callback-method>releaseBean</lifecycle-callback-method>
</pre-destroy>
</interceptor>
</interceptors>
Then I added what IBM generated (the assembly-descriptor and interceptors stanzas) back to my ejb-jar.xml and set the metadata-complete back to true.
Then it worked. Hope this helps.
Here is the full ejb-jar.xml
<?xml version="1.0" encoding="UTF-8"?>
<ejb-jar version="3.1" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/ejb-jar_3_1.xsd">
<display-name>ares-api-uow-ejb</display-name>
<enterprise-beans>
<message-driven id="TaskMDB">
<ejb-name>TaskMDB</ejb-name>
<ejb-class>something.api.uow.ejb.mdb.TaskMDB</ejb-class>
<messaging-type>javax.jms.MessageListener</messaging-type>
<transaction-type>Bean</transaction-type>
</message-driven>
</enterprise-beans>
</ejb-jar>

No setter found for property 'kBaseName' in class 'org.kie.spring.factorybeans.KBaseFactoryBean'

This is an integration issue of Drools KIE and Spring MVC Web V 3.2.3 where google just isn't finding any references to so I'll try my luck here ...
I'm integrating Drools KIE and Spring 3.2.3.RELEASE (MVC Web)- and I'm getting the following error:
No setter found for property 'kBaseName' in class 'org.kie.spring.factorybeans.KBaseFactoryBean'
[config set: maven-spring-drools/web-context application-config.xml
/maven-spring-drools/src/main/resources/spring
The Project is a pure Maven project w/out any outside natures imposed upon it (aka. Drools/Spring).
It's complaining that it can't find the setters for the kBaseName', which is set here:
I'm using a kmodule.xml found in the META-INF dir under the src/main/resources dir.
Can anyone help me discover the disconnect?
Moreover - do I have to do it this way? The project executes the SPring MVC Web App just fine and the Drools KIE test case runs perfectly in the same Maven project. Can't I just integrate them programmatically instead?
Many thanks in advance ... :-)
The offending file: application.xml
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:kie="http://drools.org/schema/kie-spring"
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd http://drools.org/schema/kie-spring http://drools.org/schema/kie-spring.xsd">
<context:component-scan base-package="com.versaggi.springweb"/>
<kie:kmodule id="ksession-rules">
<kie:kbase name="rules" packages="rules">
</kie:kbase>
</kie:kmodule>
<bean id="kiePostProcessor" class="org.kie.spring.KModuleBeanFactoryPostProcessor" />
</beans>

Tomcat 7 - Spring 3.2.1 - OpenJPA No persistent class is specified in eager initialization mode

I'm trying to get a simple web app called Debugger running under Tomcat 7 using Spring 3.2.1 and OpenJPA. I use Eclipse as the IDE and run Tomcat external to the IDE. I'm getting a error when the WAR is being deployed. This is the error message:
org.apache.openjpa.persistence.ArgumentException: No persistent class is specified in eager initialization mode.
Here is the persistence.xml
<?xml version="1.0" encoding="UTF-8"?>
<!--
-->
<persistence xmlns="http://java.sun.com/xml/ns/persistence"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
version="2.0">
<persistence-unit name="applicationDB" transaction-type="RESOURCE_LOCAL">
<provider>org.apache.openjpa.persistence.PersistenceProviderImpl</provider>
<properties>
<property name="openjpa.jdbc.SchemaFactory" value="native(ForeignKeys=true)"/>
<property name="openjpa.InitializeEagerly" value="true"/>
<property name="openjpa.DynamicEnhancementAgent" value="false"/>
</properties>
</persistence-unit>
</persistence>
Is the error caused by not having any classes specified in this file? I'm just trying to get a base application configuration setup so I'm not yet ready to place any classes in the persistence file. Maybe you have to have at least one?
Either list your persistent classes, or remove the openjpa.InitializeEagerly property.

Spring JPA & Hibernate persistence_1_0.xsd not found

So i Have this problem in implementing JPA and Hibernate in Spring WS. I have configured everything correctly and according to tutorial it should work - but it is not.
Problem lies in persistance.xml, here it is how it looks:
<persistence xmlns="http://java.sun.com/xml/ns/persistence"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/persistence
http://java.sun.com/xml/ns/persistence/persistance_1_0.xsd"
version="1.0">
<persistence-unit name="hibernatePersistenceUnit" transaction-type="RESOURCE_LOCAL">
<properties>
<property name="hibernate.hbm2ddl.auto" value="none" />
</properties>
</persistence-unit>
</persistence>
and the exception i am getting:
Caused by: java.io.FileNotFoundException: class path resource [persistence_1_0.xsd] cannot be resolved to URL because it does not exist
at org.springframework.core.io.ClassPathResource.getURL(ClassPathResource.java:177)
at org.springframework.orm.jpa.PersistenceUnitReader.validateResource(PersistenceUnitReader.java:281)
at org.springframework.orm.jpa.PersistenceUnitReader.readPersistenceUnitInfos(PersistenceUnitReader.java:108)
... 57 more
I am struggling with this since a while... Do anyone have an idea what am i missing?
You need to include appropriate jar file containing the persistence_1_0.xsd.
This is explained here.
what is the use of xsi:schemaLocation?
There was a type in the url to the .xsd file. Try using: http://java.sun.com/xml/ns/persistence/persistence_1_0.xsd . A good way to debug these types of issues is to try to hit the url in your browser, a successful request denotes a valid link.
<persistence xmlns="http://java.sun.com/xml/ns/persistence"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/persistence
http://java.sun.com/xml/ns/persistence/persistence_1_0.xsd"
version="1.0">
<persistence-unit name="hibernatePersistenceUnit" transaction-type="RESOURCE_LOCAL">
<properties>
<property name="hibernate.hbm2ddl.auto" value="none" />
</properties>
</persistence-unit>
</persistence>
You make sure your project name does not contain any spaces, nor should the path to the project. This creates an error in Hibernate entity manager.

Resources