I got an exception:
org.hibernate.HibernateException: getNamedQuery is not valid without active transaction org.hibernate.context.ThreadLocalSessionContext$TransactionProtectionWrapper.invoke(ThreadLocalSessionContext.java:340)
$Proxy10.getNamedQuery(Unknown Source)
Here is my configuration:
...
<context:annotation-driven/>
<beans:bean id="transactionManager"
class="org.springframework.orm.hibernate3.HibernateTransactionManager">
<beans:property name="sessionFactory" ref="sessionFactory" />
</beans:bean>
<tx:annotation-driven transaction-manager="transactionManager"/>
...
Also, I added context:annotation-driven since the tr:annotation-driven is not working, does <tx:annotation-driven/> use the transactionManager which obtain its own session from Hibernate?
I used my derived sessionFactory using Hibernate3 inside the annotated transaction, so how do I configure the Spring to do so?
The transaction manager has a dependency on session factory which it's using to manage transactions.
By adding <tx:annotation-driven /> you tell Spring how transactions are demarcated. In this case you can use annotations.
See the docs page how to use XML Schema-based configuration:
<?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 definitions here -->
</beans>
Related
I'm working on a spring and JPA project. I had configured my JPA Persistence Unit in the Persistence.xml and here's my spring configuration file.
My application works fine, but I didn't understand how does spring framework detects the Persistence Unit defined in my Persistence.xml file and injects it without being defined in my spring bean configuration file .
Can anybody answer me please ?
<?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:context="http://www.springframework.org/schema/context"
xmlns:tx="http://www.springframework.org/schema/tx"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.0.xsd">
<context:annotation-config/>
<context:component-scan base-package="ma.professionalpartners.fireAppBusiness.dao"/>
<bean id="entityManagerFactory" class="org.springframework.orm.jpa.LocalEntityManagerFactoryBean">
<property name="persistenceUnitName" value="fireApp-Domain" />
</bean>
<bean id="jpaTransactionManager" class="org.springframework.orm.jpa.JpaTransactionManager">
<property name="entityManagerFactory" ref="entityManagerFactory" />
</bean>
<tx:annotation-driven transaction-manager="jpaTransactionManager" />
</beans>
You have provided the name for the persistence unit, when configuring the entityManagerFactory bean:
<property name="persistenceUnitName" value="fireApp-Domain" />
The persistence.xml file MUST be on certain paths, so that Spring simply searched in those locations. After finding the file, it parses the XML content, and if there is a single PersistenceUnit, that is made the default one. Of course, if you specify a name (as you did), then it looks exactly for that PersistenceUnit.
I am having a bean defined as below
<bean id="batchManagementService" class="com.amdocs.dc.sprint.batch.service.BatchManagementServiceImpl" autowire-candidate="false">
<property name="repository" ref="batchPersistenceRepository" />
<property name="timeService" ref="batchTimeService" />
<property name="validator" ref="batchValidator" />
</bean>
I am refering this bean in two places in different modules.
However it is getting initialised with two differnt values.
with the JDK proxy which performs correct DB transactions
with the implementor class directly "BatchManagementServiceImpl" as mentioned above.
IN the second case the Entity manager is returning null causing all DB transactions to fail.
The implementor class contains transactional methods.
Any help on this will be appreciated..
Not quite clear from the description for me, but it sounds like your applicationContext is misconfigured within the modules.
Maybe you try an approach like:
MODULE_A:
-applicationContextA.xml
MODULE_B (which should use MODULE_A's beans )
-applicationContextB.xml
MODULE_C (which should use MODULE_B and MODULE_A )
-applicationContextC.xml
The WRONG approach is:
applicationContextA.xml:
<beans xmlns="http://www.springframework.org/schema/beans">
...
<bean id="batchManagementService"...>
applicationContextB.xml:
<beans xmlns="http://www.springframework.org/schema/beans">
<import resource="applicationContextA.xml"/>
<bean id=anotherBean...
applicationContextC.xml:
<beans xmlns="http://www.springframework.org/schema/beans">
<import resource="applicationContextB.xml"/>
<bean id=anotherBean...
In this case, when applicationContextC is created, batchManagementService will be instantiated twice.
Try this one:
applicationContextA.xml:
<beans xmlns="http://www.springframework.org/schema/beans">
...
<bean id="batchManagementService"...>
applicationContextB.xml:
<beans xmlns="http://www.springframework.org/schema/beans">
<!--No import here! -->
<!--<import resource="applicationContextA.xml"/>-->
<bean id=anotherBean...
applicationContextC.xml:
<beans xmlns="http://www.springframework.org/schema/beans">
<!-- No import here -->
<!--<import resource="applicationContextB.xml"/>-->
<bean id=anotherBean...
applicationContextFull.xml
<beans xmlns="http://www.springframework.org/schema/beans">
<import resource="applicationContextA.xml"/>
<import resource="applicationContextB.xml"/>
<import resource="applicationContextC.xml"/>
</beans>
, and try to boot application context using applicationContextFull.xml.
Hope this helps.
I'm struggling to understand how to properly configure Spring 3.2 and Hibernate 4.2. After working through several issues over the last few days, I ran into this exception while trying to execute a query in my DAO:
java.lang.UnsupportedOperationException: The application must supply JDBC connections
org.hibernate.service.jdbc.connections.internal.UserSuppliedConnectionProviderImpl.getConnection(UserSuppliedConnectionProviderImpl.java:62)
org.hibernate.internal.AbstractSessionImpl$NonContextualJdbcConnectionAccess.obtainConnection(AbstractSessionImpl.java:292)
org.hibernate.engine.jdbc.internal.LogicalConnectionImpl.obtainConnection(LogicalConnectionImpl.java:214)
org.hibernate.engine.jdbc.internal.LogicalConnectionImpl.getConnection(LogicalConnectionImpl.java:157)
org.hibernate.engine.transaction.internal.jdbc.JdbcTransaction.doBegin(JdbcTransaction.java:67)
org.hibernate.engine.transaction.spi.AbstractTransactionImpl.begin(AbstractTransactionImpl.java:160)
org.hibernate.internal.SessionImpl.beginTransaction(SessionImpl.java:1426)
org.hibernate.ejb.TransactionImpl.begin(TransactionImpl.java:59)
com.bsj.demo.rest.dao.DemoUserDaoImpl.getAllDemoUsers(DemoUserDaoImpl.java:41)
Here are my Spring and Hibernate files:
context.xml
<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:p="http://www.springframework.org/schema/p" xmlns:tx="http://www.springframework.org/schema/tx"
xmlns:context="http://www.springframework.org/schema/context"
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-3.0.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx-3.0.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-3.0.xsd">
<context:property-placeholder location="classpath:spring/data-access.properties"/>
<bean id="dataSource" class="org.springframework.jndi.JndiObjectFactoryBean">
<property name="jndiName" value="java:comp/env/jdbc/restdemo"/>
</bean>
<bean id="entityManagerFactory" class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean">
<property name="jpaVendorAdapter">
<bean class="org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter"/>
</property>
<property name="persistenceUnitManager">
<bean class="org.springframework.orm.jpa.persistenceunit.DefaultPersistenceUnitManager">
<property name="defaultDataSource" ref="dataSource" />
</bean>
</property>
<property name="dataSource" ref="dataSource" />
</bean>
<context:component-scan base-package="com.bsj.demo.rest.*">
<context:include-filter type="annotation" expression="org.springframework.stereotype.Repository" />
</context:component-scan>
<bean class="org.springframework.orm.jpa.JpaTransactionManager" id="transactionManager">
<property name="entityManagerFactory" ref="entityManagerFactory" />
</bean>
<context:spring-configured />
<context:annotation-config />
<bean class="com.bsj.demo.rest.spring.config.AppConfig"/>
</beans>
persistence.xml
<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_2_0.xsd"
version="2.0">
<persistence-unit name="RESTDemoJPA" transaction-type="RESOURCE_LOCAL">
<provider>org.hibernate.ejb.HibernatePersistence</provider>
<properties>
<property name="hibernate.dialect" value="org.hibernate.dialect.PostgreSQLDialect"/>
</properties>
</persistence-unit>
What am I missing? My understanding is that with Spring defining the datasource and persistence unit, there should be very little configuration needed in the persistence.xml. Am I completely wrong here?
Server is Tomcat 7, dependencies managed by Maven. I understand this is a commonly asked question on SO, but after trying several solutions I have not found a way to resolve this problem.
Have a look over here. Might be it can resolve your issue.
https://github.com/abdulwaheed18/Spring-HIbernate-Integration/blob/master/src/beans.xml
Alright, I figured out the problem and it wasn't really related to configuration. The problem was how I was instantiating the EntityManager in my code.
In the configuration, I needed to add the following to the entityManager bean definition:
<property name="persistenceUnitName" value="RESTDemoJPA" />
The bigger problem was that I was creating the EntityManager in each DAO by getting an instance from the EntityManagerFactory. Instead, I should have been allowing Spring to inject it via the PersistenceContext annotation as follows:
#PersistenceContext(unitName="RESTDemoJPA")
private EntityManager em;
After doing that, I removed all calls to em.getTransaction(), annotated the class as #Transactional and the exceptions went away. After that, I could query the database.
TL;DR: Configuration was correct minus defining the Persistence Unit name in the entityManager definition. Changed EntityManager instantiation from manual to Spring injection.
I integrated Spring Batch Admin into my app, which uses Spring 3.2.
Now I try to annotate a method with #Scheduled and activate this with <task:annotation-driven/>.
When I launch the webapp I get this exception:
Caused by: java.lang.IllegalStateException: #Scheduled method 'removeInactiveExecutions'
found on bean target class 'SimpleJobService', but not found in any interface(s) for bean
JDK proxy. Either pull the method up to an interface or switch to subclass (CGLIB) proxies
by setting proxy-target-class/proxyTargetClass attribute to 'true'
The SimpleJobService of Spring Batch Admin uses this annotation on a method.
In Spring 3.2. it seems, that there is no need to put cglib into the classpath and spring-asm is obsolete, too. I excluded the spring-asm dependency from spring-batch-integration.
Where can I set proxy-target-class=true (I already tried it on <tx:annotation-config> and <aop:config>?
How can I use #Scheduled in my application?
Add execution-context.xml in META-INF\spring\batch\override, set proxy of SimpleJobServiceFactoryBean to target class
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:p="http://www.springframework.org/schema/p" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:aop="http://www.springframework.org/schema/aop"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd">
<!-- Original jobRepository missing read ${batch.isolationlevel} -->
<bean id="jobRepository"
class="org.springframework.batch.core.repository.support.JobRepositoryFactoryBean"
p:dataSource-ref="dataSource" p:transactionManager-ref="transactionManager" p:isolationLevelForCreate = "${batch.isolationlevel}"/>
<!-- Original jobService conflicted with #EnableScheduling -->
<bean id="jobService"
class="org.springframework.batch.admin.service.SimpleJobServiceFactoryBean">
<aop:scoped-proxy proxy-target-class="true" />
<property name="jobRepository" ref="jobRepository" />
<property name="jobLauncher" ref="jobLauncher" />
<property name="jobLocator" ref="jobRegistry" />
<property name="dataSource" ref="dataSource" />
<property name="jobExplorer" ref="jobExplorer" />
<property name="transactionManager" ref="transactionManager" />
</bean>
</beans>
<bean name="readerService" class="com.mayank.example1.ReaderService"/>
<property name="reader" ref="fileReader" />
</bean>
<bean name="fileReader" class="com.mayank.example1.FileReader">
<constructor-arg value="resources/myfile.txt" />
</bean>
Reder service take reader as argument in its constructor
Reader is Interface.
FileReader is class that implement Reader
In spring It is not taking property reader and throwing exception:
Exception in thread "main" org.springframework.beans.factory.xml.XmlBeanDefinitionStoreException: Line 15 in XML document from class path resource [reader-beans.xml] is invalid; nested exception is org.xml.sax.SAXParseException: cvc-complex-type.2.4.a: Invalid content was found starting with element 'property'. One of '{"http://www.springframework.org/schema/beans":import, "http://www.springframework.org/schema/beans":alias, "http://www.springframework.org/schema/beans":bean, WC[##other:"http://www.springframework.org/schema/beans"]}' is expected
It looks like you are closing the bean tag too early (note the /> at the end, shouldn't this be just >?):
<bean name="readerService" class="com.mayank.example1.ReaderService"/>
<property name="reader" ref="fileReader" />
</bean>
Make sure you have the required xml namespaces bean and context provided at the top of your configuration file. My example uses version 3.1 of Spring you may need to adjust for the version of Spring you are using.
Also notice the adjustment to the readerService bean tag which was being closed too early.
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
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-3.1.xsd">
<bean name="readerService" class="com.mayank.example1.ReaderService">
<property name="reader" ref="fileReader" />
</bean>
<bean name="fileReader" class="com.mayank.example1.FileReader">
<constructor-arg value="resources/myfile.txt" />
</bean>
</beans>