"No Session found for current thread" for scheduled task - java

I have a task that write some data to db and it works fine. But when a made it a scheduled task it starts but throws org.hibernate.HibernateException: No Session found for current thread when accessing the database.
This is my bean.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:tx="http://www.springframework.org/schema/tx"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:task="http://www.springframework.org/schema/task"
xmlns:cache="http://www.springframework.org/schema/cache"
xmlns:p="http://www.springframework.org/schema/p"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.1.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.1.xsd
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.1.xsd
http://www.springframework.org/schema/cache http://www.springframework.org/schema/cache/spring-cache.xsd
http://www.springframework.org/schema/task http://www.springframework.org/schema/task/spring-task.xsd">
<bean id="slfInstaller" class="org.springframework.beans.factory.config.MethodInvokingFactoryBean">
<property name="staticMethod" value="org.slf4j.bridge.SLF4JBridgeHandler.install"/>
</bean>
<context:annotation-config/>
<context:component-scan base-package="com.task"/>
<tx:annotation-driven transaction-manager="transactionManager"/>
<aop:aspectj-autoproxy proxy-target-class="true"/>
...
<bean id="dataSource" class="org.apache.tomcat.jdbc.pool.DataSource" destroy-method="close">
<property name="driverClassName" value="com.task.JDBCDriverProxy" />
<property name="url" value="jdbc:task:db" />
<property name="initialSize" value="10" />
<property name="maxActive" value="100" />
<property name="validationQuery" value="select 1" />
<property name="testOnBorrow" value="true" />
<property name="jdbcInterceptors" value="QueryTimeoutInterceptor(queryTimeout=60)" />
</bean>
<bean id="sessionFactory" class="org.springframework.orm.hibernate4.LocalSessionFactoryBean">
<property name="dataSource" ref="dataSource" />
<property name="packagesToScan">
<list>
<value>com.task.model</value>
</list>
</property>
<property name="hibernateProperties">
<props>
<prop key="hibernate.format_sql">true</prop>
<prop key="hibernate.dialect">org.hibernate.dialect.SQLServerDialect</prop>
</props>
</property>
</bean>
</beans>
Spring configuration:
#EnableScheduling
#Configuration
public class SpringConfiguration {
#Inject
private ApplicationContext context;
#PostConstruct
public void registerListeners() {
SessionFactory sessionFactory = context.getBean(SessionFactory.class);
EventListenerRegistry registry = ((SessionFactoryImplementor) sessionFactory).getServiceRegistry().getService(
EventListenerRegistry.class);
registry.getEventListenerGroup(EventType.POST_LOAD).appendListener(new PostLoadEventListener());
}
#Bean
public Validator getValidator() {
ValidatorFactory factory = Validation.buildDefaultValidatorFactory();
return factory.getValidator();
}
}
And the task:
#Service
public class Job {
#Inject
private ConfigurationHolder configurationHolder;
#Scheduled(cron = "0 0 11 * * *")
public void run() {
JobConfiguration configuration = configurationHolder.getConfiguration(JobConfiguration.class);
...
User user = User.getById(userId);
...
}
}
Injecting of configuration works fine. What could be the problem?

Related

Spring jdbc #Transactional annotation, procedure commits the transaction despite throwing appropriate exception

Namastey,
We have a class and its methods as below-
#Service
#Component
public class ReqServiceImpl implements ReqService {
#Autowired
private SubmitDAO submitDAO;
#Override
#Transactional(readOnly = false, rollbackFor = { CustomException.class },propagation=Propagation.REQUIRES_NEW)
public Map initiateSubmission(DetailsDTO vo) CustomException{
//it has two method defined within the same class
if(condition){
Map resultMap = submitRequest1(map,vo.isTestMode());
}else{
Map result = (HashMap) submitRequest2(map,flag,vo.isTestMode());
}
}
#Transactional(readOnly = false, rollbackFor = { CustomException.class }, propagation=Propagation.REQUIRED)
public void submitRequest1(Map map,boolean testMode) CustomException{
submitDAO.simpleTableInsert1(map);
submitDAO.simpleTableInsert2(map);
submitDAO.procedureCall(map);
if(!"success".equalIgnorecase(map.get("output"))){
throw new CustomException("Service Layer | submitRequest1 | Proc Not successs");
}
}
#Transactional(readOnly = false, rollbackFor = { CustomException.class },
propagation=Propagation.REQUIRED)
public void submitRequest2(Map map,boolean testMode) throws CustomException{
submitDAO.simpleTableInsert1(map);
submitDAO.simpleTableInsert2(map);
submitDAO.procedureCall(map);
}
}
Below is our spring-servlet.xml file
In this, we have added our configuration for annotation, proxy and default advice to proxy.
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:mvc="http://www.springframework.org/schema/mvc"
xmlns="http://www.springframework.org/schema/beans"
xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:tx="http://www.springframework.org/schema/tx"
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://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc.xsd
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx.xsd
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd">
<context:annotation-config />
<context:component-scan base-package="com.company,com.company.security.filter,org.springframework.jdbc" />
<mvc:annotation-driven>
<mvc:message-converters register-defaults="true">
<bean class="org.springframework.http.converter.StringHttpMessageConverter">
<constructor-arg index="0" name="defaultCharset"
value="UTF-8" />
</bean>
<bean class="org.springframework.http.converter.json.MappingJackson2HttpMessageConverter" />
</mvc:message-converters>
</mvc:annotation-driven>
<aop:aspectj-autoproxy proxy-target-class="true"/>
<tx:annotation-driven transaction-manager="transactionManager" />
<bean
class="org.springframework.aop.framework.autoproxy.DefaultAdvisorAutoProxyCreator">
<property name="proxyTargetClass" value="true" />
</bean>
<bean id="LogAspect" class="com.company.common.aop.LogAspect">
</bean>
<bean id="PerfAspect" class="com.company.common.aop.PerfAspect">
</bean>
<bean id="SessionAspect" class="com.company.common.aop.SessionAspect">
</bean>
<bean id="mailProperties"
class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
<property name="locations" >
<list>
<value>classpath:db.properties</value>
<value>classpath:dSource.properties</value>
</list>
</property>
<property name="ignoreUnresolvablePlaceholders" value="true" />
</bean>
<bean id="myDataSource" class="org.springframework.jndi.JndiObjectFactoryBean" primary="true">
<property name="jndiName" value="${DEVJNDI}"></property>
</bean>
<bean id="transactionManager"
class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
<property name="dataSource" ref="myDataSource" />
</bean>
<bean id="DataSourceCo" class="org.springframework.jndi.JndiObjectFactoryBean">
<property name="jndiName" value="${JNDINAMECOM}"></property>
</bean>
<bean id="transactionManager1"
class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
<property name="dataSource" ref="DataSourceCo" />
</bean>
<bean id="myDataSourceI" class="org.springframework.jndi.JndiObjectFactoryBean">
<property name="jndiName" value="${JNDINAMEI}"></property>
</bean>
<bean id="transactionManager2"
class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
<property name="dataSource" ref="myDataSourceI" />
</bean>
<!-- Create a proxy to generate session-scope -->
<bean id="userBean"
class="com.company.common.session.UserDetailsSessionBean"
scope="session">
<aop:scoped-proxy />
</bean>
<bean id="multipartResolver"
class="org.springframework.web.multipart.commons.CommonsMultipartResolver"
/>`enter code here`
</beans>
The method 'procedureCall' which is in another class has a procedure call inside.
The procedure does not have any commit inside.
After, the procedure return with any error, we throw an exception, but all in vain. The insert done by procedure still gets committed and transaction does not rollback.
The proc gives output "error" or "succcess", if there was any error.
If we get any error "error", then we throw the customized exception.
It should rollback everything done inside proc, but that does not happen.
Kindly suggest.

CreateQuery is not valid without active transaction Hibernate Spring

Hello i am stuck with the following problem ,When i am using sessionFactory.openSession(); it working properly but Session session = this.sessionFactory.getCurrentSession(); i am getting following error
DAO
public List<Bank> listbank(String bankId) {
Session session = this.sessionFactory.getCurrentSession();
Query query = session.createQuery("from Bank ");
List<Bank>bankList =query.list();
session.close();
return bankList;
}
error
org.hibernate.HibernateException: createQuery is not valid without active transaction
at org.hibernate.context.internal.ThreadLocalSessionContext$TransactionProtectionWrapper.invoke(ThreadLocalSessionContext.java:352)
at com.sun.proxy.$Proxy86.createQuery(Unknown Source)
at
My application is running with spring and hibernate with Jersey
Databaseconfig.xml
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns:mvc="http://www.springframework.org/schema/mvc"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://www.springframework.org/schema/beans"
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.xsd
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd">
<bean
class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
<property name="location">
<value>classpath:properties/database.properties</value>
</property>
</bean>
<tx:annotation-driven transaction-manager="transactionManager"/>
<bean id="dataSource" class="org.apache.commons.dbcp2.BasicDataSource">
<property name="driverClassName" value="${jdbc.driverClassName}" />
<property name="url" value="${jdbc.url}" />
<property name="username" value="${jdbc.username}" />
<property name="password" value="${jdbc.password}" />
</bean>
<bean id="sessionFactory"
class="org.springframework.orm.hibernate4.LocalSessionFactoryBean">
<property name="dataSource" ref="dataSource" />
<property name="packagesToScan" value="com.model" />
<property name="hibernateProperties">
<props>
<prop key="hibernate.format_sql">true</prop>
<prop key="hibernate.use_sql_comments">true</prop>
<prop key="hibernate.current_session_context_class">thread</prop>
<!-- <prop key="hibernate.hbm2ddl.auto">validate</prop> -->
</props>
</property>
</bean>
<bean id="transactionManager" class="org.springframework.orm.hibernate4.HibernateTransactionManager">
<property name="sessionFactory" >
<ref bean="sessionFactory" />
</property>
</bean>
<tx:annotation-driven/>
</beans>
ApplicationContext
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns:mvc="http://www.springframework.org/schema/mvc"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://www.springframework.org/schema/beans"
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc.xsd
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://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop-2.5.xsd"
xmlns:aop="http://www.springframework.org/schema/aop"
>
<!-- for database configuration -->
<import resource="classpath*:/config/db-configuration.xml" />
<!-- for spring-security-with-oauth configuration -->
<import resource="classpath*:/config/spring-security-oauth2.xml" />
<context:component-scan base-package="com" />
<context:annotation-config></context:annotation-config>
</beans>
you missed session.beginTransaction(); in ur code.
place it after Session session = this.sessionFactory.getCurrentSession();

Does spring 3 require aspectJ for #Transactional?

I have a project with Spring 4 and we have a lot of #Transactional classes and methods over there. Everything works perfectly, but I can not to provide transactions driven by Spring 3 until I add AspectJ libs and special XML configs in context file. So I would like to know if Spring 4 implements something like AspectJ natively or am I doing something wrong?
UPDATED
this is xml file for 3d Spring
<?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:tx="http://www.springframework.org/schema/tx"
xmlns:aop="http://www.springframework.org/schema/aop" xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.1.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx-3.1.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop-3.1.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-3.1.xsd">
<context:annotation-config />
<aop:config proxy-target-class="true" />
<tx:annotation-driven transaction-manager="transactionManager" />
<bean id="saveService" class="com.promptlink.stbtp.dao.billing.SaveService" />
<bean id="billingDaoImpl" class="com.promptlink.stbtp.dao.billing.BillingDaoImpl" />
<bean id="dbParams" class="com.promptlink.stbtp.dao.util.SessionManager"
factory-method="getDbParams">
</bean>
<bean id="url" factory-bean="dbParams" factory-method="getBillingUri" />
<bean id="user" factory-bean="dbParams" factory-method="getUser" />
<bean id="pass" factory-bean="dbParams" factory-method="getPassword" />
<bean id="driver" factory-bean="dbParams" factory-method="getDriver" />
<bean id="dialect" factory-bean="dbParams" factory-method="getDialect" />
<bean id="dataSource"
class="org.springframework.jdbc.datasource.DriverManagerDataSource">
<property name="driverClassName" ref="driver" />
<property name="url" ref="url" />
<property name="username" ref="user" />
<property name="password" ref="pass" />
</bean>
<bean id="sessionFactory"
class="org.springframework.orm.hibernate4.LocalSessionFactoryBean">
<property name="dataSource" ref="dataSource" />
<property name="packagesToScan">
<list>
<value>com.promptlink.stbtp.dao.billing</value>
</list>
</property>
<property name="hibernateProperties">
<props>
<prop key="hibernate.dialect">#{dialect}</prop>
</props>
</property>
</bean>
<bean id="transactionManager"
class="org.springframework.orm.hibernate4.HibernateTransactionManager">
<property name="sessionFactory" ref="sessionFactory" />
</bean>
<bean id="persistenceExceptionTranslationPostProcessor"
class="org.springframework.dao.annotation.PersistenceExceptionTranslationPostProcessor" />
<beans profile="webapp">
<bean id="billingDatabaseProcessor"
class="com.promptlink.stbtp.dao.statistics.billingReport.BillingDatabaseProcessorImpl"></bean>
</beans>
</beans>
UPDATED
For Spring 4 I have something like this for Hibernate
import org.hibernate.SessionFactory;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.orm.hibernate4.HibernateTransactionManager;
import org.springframework.transaction.annotation.EnableTransactionManagement;
#Configuration
#EnableTransactionManagement
public class HibernateConfiguration {
#Bean
public HibernateTransactionManager transactionManager() {
return new HibernateTransactionManager(sessionFactory());
}
#Bean
public SessionFactory sessionFactory() {
return SessionManager.getSessionFactory();
}
}
And standard AnnotationConfigApplicationContext initializer

Instantiate service in GUI over spring

my problem is that I get a NullPointerException in my GUI. The object that is null should be intatiated by Spring. Please see my applicationContext.xml:
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:aop="http://www.springframework.org/schema/aop" xmlns:context="http://www.springframework.org/schema/context"
xmlns:security="http://www.springframework.org/schema/security"
xmlns:tx="http://www.springframework.org/schema/tx" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop-3.2.xsd
http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.2.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-3.2.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx-3.2.xsd">
<context:annotation-config />
<context:component-scan base-package="com.Person"
annotation-config="true" />
<tx:annotation-driven transaction-manager="transactionManager"
proxy-target-class="true" />
<bean id="mainGUI" class="com.Person.MainWindow" />
<bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource">
<property name="driverClassName" value="org.hsqldb.jdbcDriver" />
<property name="url" value="jdbc:hsqldb:hsql://localhost/testDB" />
<property name="username" value="sa" />
<property name="password" value="" />
<property name="initialSize" value="5" />
<property name="maxActive" value="10" />
</bean>
<bean id="entityManagerFactory"
class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean">
<property name="dataSource" ref="dataSource" />
<property name="persistenceUnitName" value="jpaData" />
<property name="jpaVendorAdapter">
<bean class="org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter" />
</property>
<property name="jpaProperties">
<props>
<prop key="hibernate.dialect">org.hibernate.dialect.HSQLDialect</prop>
<prop key="hibernate.show_sql">true</prop>
<prop key="hibernate.format_sql">false</prop>
<prop key="hibernate.hbm2ddl.auto">update</prop>
</props>
</property>
</bean>
<bean id="transactionManager" class="org.springframework.orm.jpa.JpaTransactionManager">
<property name="entityManagerFactory" ref="entityManagerFactory" />
</bean>
<bean id="entityManager"
class="org.springframework.orm.jpa.support.SharedEntityManagerBean">
<property name="entityManagerFactory" ref="entityManagerFactory" />
</bean>
<bean
class="org.springframework.orm.jpa.support.PersistenceAnnotationBeanPostProcessor" />
</beans>
I am getting the error here List<Person> test = personService.getAll(), personService is null:
#Component
public class PersonDataPanel extends JPanel {
public PersonTableModel atm;
#Autowired
public PersonServiceImpl personService;
public JPanel placePersonTable() {
log.info("enter placePersonTable method");
JPanel selectionArea = new JPanel(new FlowLayout(FlowLayout.CENTER));
List<Person> test = personService.getAll(); //here I am getting the error
log.info(test.toString());
atm = new PersonTableModel(test);
JTable PersonTable = new JTable(atm);
PersonTable.setAutoResizeMode(JTable.AUTO_RESIZE_LAST_COLUMN );
PersonTable.setFillsViewportHeight(true);
repaint();
return selectionArea;
}
}
Why is this the chase for my desktop application? I thought my annotations are correct. I have used #Autowired, so the service should be instantiated?
I really appreciate your answer!
UPDATE
My PersonServiceImpl class:
#Component
public class PersonServiceImpl {
/**
* PersonDAO Access
*/
#Autowired
private PersonDaoImpl PersonDAO;
public List<Person> getAll() {
log.info("reveiving all data in the Person Service");
return PersonDAO.getAll();
}
}

Spring/JPA data persisting in unit test but not in application

I'm unable to persist data in my Spring/JPA/Tomcat application by calling my userService but when I call it from my unit test the data gets written to the database. Nor is there any exception thrown when calling the service from my controller.
Controller class:
#Controller
#RequestMapping("/")
public class AccessManagementController {
#Autowired
private UserService userService;
#Autowired
private ApplicationProperties applicationProperties;
#RequestMapping(method = RequestMethod.GET, value = "/register")
:
:
:
userService.createNewUser(username, password);
model.addAttribute("loginMessage", "Registration successful; you can now login");
return "/access";
}
}
Working unit test:
#RunWith(SpringJUnit4ClassRunner.class)
#ContextConfiguration(locations = {
"classpath:/spring/applicationContext.xml",
"classpath:/spring/securityContext.xml",
"classpath:/spring/jpaContext.xml"
})
public class UserServiceTest {
#Autowired
private UserService userService;
#Test
public void userServiceSaveUserTest() {
String testUsername = (new Date()).toString();
userService.createNewUser(testUsername, "password");
User findUser = userService.findByUsername(testUsername);
Assert.assertNotNull(findUser);
Assert.assertEquals(findUser.getUsername(), testUsername);
}
}
applicationContext.xml:
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:mvc="http://www.springframework.org/schema/mvc"
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/mvc
http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd">
<context:component-scan base-package="com.bytediary"/>
<bean id="applicationProperties" class="com.bytediary.util.ApplicationProperties">
<property name="location" value="classpath:application.properties"/>
</bean>
<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix" value="/jsp/" />
<property name="suffix" value=".jsp" />
</bean>
<bean class="org.springframework.orm.hibernate4.HibernateExceptionTranslator"/>
<mvc:annotation-driven/>
<mvc:resources mapping="/resources/**" location="/resources/"/>
</beans>
jpaContext.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:jpa="http://www.springframework.org/schema/data/jpa"
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.xsd
http://www.springframework.org/schema/data/jpa
http://www.springframework.org/schema/data/jpa/spring-jpa.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx-3.0.xsd">
<bean id="dataSource" class="org.springframework.jdbc.datasource.SimpleDriverDataSource">
<property name="driverClass" value="${jdbc.driver}"/>
<property name="url" value="${jdbc.url}"/>
<property name="username" value="${jdbc.username}"/>
<property name="password" value="${jdbc.password}"/>
</bean>
<context:annotation-config />
<context:component-scan base-package="com.bytediary.entity" />
<jpa:repositories base-package="com.bytediary.repository"/>
<bean id="transactionManager" class="org.springframework.orm.jpa.JpaTransactionManager">
<property name="entityManagerFactory" ref="entityManagerFactory" />
</bean>
<bean id="entityManagerFactory" class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean">
<property name="persistenceUnitName" value="default"/>
<property name="dataSource" ref="dataSource" />
<property name="packagesToScan" value="com.bytediary.entity" />
<property name="persistenceXmlLocation" value="classpath:/jpa/persistence.xml" />
<property name="jpaVendorAdapter">
<bean class="org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter">
<property name="showSql" value="true"/>
<property name="database" value="MYSQL" />
<property name="generateDdl" value="true" />
</bean>
</property>
</bean>
<bean class="org.springframework.orm.jpa.support.PersistenceAnnotationBeanPostProcessor"/>
<tx:annotation-driven proxy-target-class="true" transaction-manager="transactionManager" />
</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="default" transaction-type="RESOURCE_LOCAL">
<class>com.bytediary.entity.User</class>
</persistence-unit>
</persistence>
1 . You do not need
<bean class="org.springframework.orm.jpa.support.PersistenceAnnotationBeanPostProcessor"/>
Explanation here:
http://static.springsource.org/spring/docs/2.5.6/api/org/springframework/orm/jpa/support/PersistenceAnnotationBeanPostProcessor.html
Note: A default PersistenceAnnotationBeanPostProcessor will be
registered by the "context:annotation-config" and
"context:component-scan" XML tags. Remove or turn off the default
annotation configuration there if you intend to specify a custom
PersistenceAnnotationBeanPostProcessor bean definition.
2 . Try adding #Transactional to UserService.createNewUser().

Categories

Resources