Spring 4 + Hibernate 5: No bean named 'transactionManager' is defined - spring

I have a problem with my project when I call a URL.
This is my applicationContext
<context:component-scan base-package="com.prova" />
<mvc:annotation-driven />
<mvc:resources mapping="/resources/**" location="/resources/" />
<bean id="dataSource"
<!-- connection to datasource -->
</bean>
<bean id="sessionFactory"
class="org.springframework.orm.hibernate5.LocalSessionFactoryBean">
<property name="dataSource" ref="dataSource" />
<property name="packagesToScan" value="com.prova.model"></property>
<property name="hibernateProperties">
<props>
<prop key="hibernate.dialect">org.hibernate.dialect.SQLServerDialect</prop>
<prop key="hibernate.show_sql">true</prop>
<prop key="hibernate.id.new_generator_mappings">false</prop>
<prop key="hibernate.cache.use_second_level_cache">false</prop>
<prop key="hibernate.cache.use_query_cache">false</prop>
</props>
</property>
</bean>
<bean id="transactionManager"
class="org.springframework.orm.hibernate5.HibernateTransactionManager"
name ="transactionManager">
<property name="sessionFactory" ref="sessionFactory" />
</bean>
<tx:annotation-driven transaction-manager="transactionManager" />
</beans>
This is my Controller
package com.prova.controller;
<!-- import -->
#Controller
public class EmployeeController {
private static final Logger logger = Logger
.getLogger(EmployeeController.class);
public EmployeeController() {
System.out.println("EmployeeController()");
}
#Autowired
private EmployeeService employeeService;
#RequestMapping(value = "/all")
public ModelAndView listEmployee(ModelAndView model) throws IOException {
List<Employee> listEmployee = employeeService.getAllEmployees();
model.addObject("listEmployee", listEmployee);
model.setViewName("home");
return model;
}
...
...
...
When I call in postman the url http://localhost:8082/prova/all I receive this error:
org.springframework.beans.factory.NoSuchBeanDefinitionException: No bean named 'transactionManager' is defined: No matching PlatformTransactionManager bean found for qualifier 'transactionManager' - neither qualifier match nor bean name match!
Please help me because I try all solutions.
Thanks!

Related

Why is #PrePersist is not being invoked in Hibernate 5.2?

I just recently moved from Hibernate4 to Hibernate5. I have implemented #PrePersist on my entity bean but #PrePersist doesn't seem to be firing up.
I have searched around
https://github.com/hibernate/hibernate-orm/wiki/Migration-Guide---5.2
And on it it states
org.hibernate.SessionFactory now extends javax.persistence.EntityManagerFactory - temporarily it technically extends org.hibernate.jpa.HibernateEntityManagerFactory (which in turn extends javax.persistence.EntityManagerFactory) for backwards compatibility. HibernateEntityManagerFactory is deprecated.
So this means JPA annotation should work right?
My entity class looks like this
#Entity
#Table(name="books")
#Component
public class Book implements Serializable{
private static final long serialVersionUID = -2042607611480064259L;
#Id
#GeneratedValue
private int id;
#NotBlank
private String name;
#NotBlank
#Size(min=2, max=16)
private String ispn;
#DecimalMin(value = "0.1")
private double price;
private Timestamp dateCreated;
private Date datePublished;
#PrePersist
public void beforePersist() {
System.out.println("#PrePersist is called");
this.dateCreated = Timestamp.from(Instant.now());
}
applicationContext.xml (Hibernate configuration)
<!-- Connection Pool -->
<bean id="hikariConfig" class="com.zaxxer.hikari.HikariConfig">
<property name="poolName" value="springHikariCP" />
<property name="connectionTestQuery" value="SELECT 1" />
<property name="dataSourceClassName"
value="com.mysql.jdbc.jdbc2.optional.MysqlDataSource" />
<property name="dataSourceProperties">
<props>
<prop key="url">jdbc:mysql://localhost:3306/sample</prop>
<prop key="user">sample</prop>
<prop key="password">sample</prop>
</props>
</property>
</bean>
<bean id="dataSource" class="com.zaxxer.hikari.HikariDataSource"
destroy-method="close">
<constructor-arg ref="hikariConfig" />
</bean>
<bean id="transactionManager"
class="org.springframework.orm.hibernate5.HibernateTransactionManager">
<property name="sessionFactory" ref="sessionFactory"></property>
</bean>
<!-- Hibernate Settings -->
<bean id="sessionFactory"
class="org.springframework.orm.hibernate5.LocalSessionFactoryBean">
<property name="dataSource" ref="dataSource"></property>
<property name="hibernateProperties">
<props>
<prop key="hibernate.dialect">org.hibernate.dialect.MySQLDialect</prop>
<prop key="hibernate.id.new_generator_mappings">false</prop>
<prop key="hibernate.jdbc.time_zone">UTC</prop>
<prop key="hibernate.connection.useUnicode">true</prop>
<prop key="hibernate.connection.characterEncoding">UTF-8</prop>
<prop key="hibernate.connection.charSet">UTF-8</prop>
<prop key="hibernate.show_sql">true</prop>
<prop key="org.hibernate.jdbc">TRACE</prop>
</props>
</property>
<property name="packagesToScan">
<list>
<value>com.app.books</value>
</list>
</property>
</bean>
Questions
Something wrong with how I implement #Prepersist?
Or perhaps my Hibernate configuration has some error?
Thank you

From TransactionProxyFactoryBean to a Declarative transaction management?

I want to migrate from old style of transaction management with TransactionProxyFactoryBean to a Declarative transaction management recommended by spring.
So that will be possible to avoid exceptions with transactions that appear from time to time.
This is my configuration xml file:
<beans xmlns=...>
<context:annotation-config/>
<context:component-scan base-package="prof" />
<bean id="sessionFactory" class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
<property name="configLocation">
<value>WEB-INF/classes/hibernate.cfg.xml</value>
</property>
</bean>
<import resource="prof-dao-spring.xml" />
<bean id="transactionManager" class="org.springframework.orm.hibernate3.HibernateTransactionManager">
<property name="sessionFactory" ref="sessionFactory"/>
</bean>
<bean id="baseTransactionProxy" class="org.springframework.transaction.interceptor.TransactionProxyFactoryBean" abstract="true">
<property name="transactionManager">
<ref bean="transactionManager"/>
</property>
<property name="transactionAttributes">
<props>
<prop key="save*">PROPAGATION_REQUIRED</prop>
...
<prop key="*">PROPAGATION_REQUIRED,readOnly</prop>
</props>
</property>
</bean>
<bean id="ProfileService" parent="baseTransactionProxy">
<property name="target">
<bean class="tv.clever.hibernate.service.ProfileService"></bean>
</property>
</bean>
</beans>
ProfileService looks like:
#Component
public class ProfileService {
#Autowired
#Qualifier("baseDAO")
protected BaseDAO baseDAO;
private static ProfileService profileService;
public ProfileService() {
setProfileService(this);
}
public void setProfileService(ProfileService ps) {
profileService = ps;
}
public void save(final Collection transientObjects) {
baseDAO.save(transientObjects);
}
...
}
From where do I need to start?
Assuming you want to use annotations slap a #Transactional on your service class, add <tx:annotation-driven /> to your configuration and remove the TransactionalProxyFactoryBean declaration and all beans using that as a parent.
Additional pro-tips:
Use #Service for service classes and #Repository for daos
<context:annotation-config /> is implied by <context:component-scan />
Your service
#Service
#Transactional
public class ProfileService { ... }
Configuration
<beans xmlns=...>
<context:component-scan base-package="prof" />
<import resource="prof-dao-spring.xml" />
<bean id="sessionFactory" class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
<property name="configLocation">
<value>WEB-INF/classes/hibernate.cfg.xml</value>
</property>
</bean>
<bean id="transactionManager" class="org.springframework.orm.hibernate3.HibernateTransactionManager">
<property name="sessionFactory" ref="sessionFactory"/>
</bean>
<tx:annotation-driven />
</beans>
Restart application.

weblogic jndi datasource issue with spring application

I have created a oracle datasource in weblogic with the name jdbc/myDS.
Weblogic created a xml file in mydomain/config/jdbc and the configuration works from the weblogic admin console. Test connection is working. My spring context file details are:
<bean id="dataSource" class="org.springframework.jndi.JndiObjectFactoryBean">
<property name="jndiName" value="jdbc/myDS"/>
</bean>
<bean id="sessionFactory"
class="org.springframework.orm.hibernate3.annotation.AnnotationSessionFactoryBean">
<property name="dataSource" ref="dataSource" />
<property name="hibernateProperties">
<util:map>
<entry key="hibernate.hbm2ddl.auto" value="update" />
<entry key="hibernate.show_sql" value="true" />
</util:map>
</property>
</bean>
<bean id="myDAO" class ="com.example.MyDAOImpl">
<property name="sessionFactory" ref="sessionFactory" />
</bean>
My Java class is:
public class MyDAOImpl implements MyDAO{
private SessionFactory sessionFactory;
public void setSessionFactory(SessionFactory sessionFactory){
this.sessionFactory = sessionFactory;
}
public SessionFactory getSessionFactory() {
return sessionFactory;
}
public void persistPerson(Person person) {
Session session = getSessionFactory().openSession();
try {
session.beginTransaction();
session.save(person);
session.getTransaction().commit();
}catch(HibernateException he) {
he.printStackTrace();
} finally {
session.close();
}
}
}
An error occurred during activation of changes, please see the log for
details.
Message icon - Error weblogic.application.ModuleException:
Message icon - Error While trying to lookup 'jdbc.myDS' didn't find subcontext 'jdbc'. Resolved ''; remaining name 'jdbc/myDS'
The following provides a Spring XML configuration to obtain a datasource by a JNDI name and inject it into Springs AnnotationSessionFactoryBean.
<bean id="dataSource" class="org.springframework.jndi.JndiObjectFactoryBean">
<property name="jndiName" value="jdbc/YourJndi"/>
</bean>
<bean id="sessionFactory"
class="org.springframework.orm.hibernate3.annotation.AnnotationSessionFactoryBean">
<property name="dataSource">
<ref bean="dataSource" />
</property>
<property name="annotatedClasses">
<list>
<value>com.java.model.Employee</value>
</list>
</property>
<property name="hibernateProperties">
<props>
<prop key="hibernate.dialect">${hibernate.dialect}</prop>
<prop key="hibernate.show_sql">${hibernate.show_sql}</prop>
<prop key="hibernate.hbm2ddl.auto">${hibernate.hbm2ddl.auto}</prop>
</props>
</property>
</bean>
This example has been sucessfully tested on Weblogic 10.3.4 with Oracle 11g.

Spring autowire by constructor not working without setter method

Here is my Spring xml config:
<beans profile="test">
<context:annotation-config />
<!-- hsqldbDataSource bean for testing purposes -->
<bean id="hsqldbDataSource" class="com.zaxxer.hikari.HikariDataSource" destroy-method="shutdown">
<constructor-arg>
<bean class="com.zaxxer.hikari.HikariConfig">
<constructor-arg>
<props>
<prop key="dataSource.url">${hsqldb.url}</prop>
<prop key="dataSource.user">${user}</prop>
<prop key="dataSource.password">${password}</prop>
</props>
</constructor-arg>
<property name="dataSourceClassName" value="org.hsqldb.jdbc.JDBCDataSource" />
<property name="connectionTestQuery" value="SELECT 1" />
<property name="maximumPoolSize" value="5" />
<property name="minimumPoolSize" value="1" />
</bean>
</constructor-arg>
</bean>
<!-- execute a script to create tables after creation of bean for in-memory HSQLDB -->
<jdbc:embedded-database id="hsqldbDataSource" type="HSQL">
<jdbc:script location="classpath:schema.sql" />
<jdbc:script location="classpath:test-data.sql"/>
</jdbc:embedded-database>
<bean id="daoManager" class="com.d.DAOManager" autowire="constructor">
<property name="dataSource" ref="hsqldbDataSource"/>
</bean>
</beans>
And my DAOManager class:
public class DAOManager {
private DataSource dataSource;
private Connection connection;
#Autowired
public DAOManager(DataSource dataSource) {
this.dataSource = dataSource;
}
public void setDataSource(DataSource dataSource) {
this.dataSource = dataSource;
}
And here I call get my daoManager bean.
public static DAOManager createHSQLDBDAOManager() {
LOG.info("Setting up datasource to in-memory HSQLDB");
ConfigurableEnvironment env = (ConfigurableEnvironment)applicationContext.getEnvironment();
env.setActiveProfiles("test");
applicationContext.load("classpath:/applicationContext.xml");
applicationContext.refresh();
DAOManager daoManager = applicationContext.getBean("daoManager", DAOManager.class);
return daoManager;
}
Why is it complaining if I remove the setter method? I don't need it. If I remove the #Autowired before the constructor (it also works) it's just useless and not using the by constructor autowire function.
Needed to remove the property value of the daoManager bean
<bean id="daoManager" class="com.d.DAOManager" autowire="constructor"/>

Spring and null pointer exception

I'm trying to do this:
#Controller
public class HomeController {
#Autowired
private TemplatesService templatesService;
public final String TEST = templatesService.getUri();
The getUri:
#SuppressWarnings("unchecked")
public String getUri() {
return (String) sessionFactory.getCurrentSession()
.createQuery("select uri from Templates WHERE state=1")
.uniqueResult();
}
It will work correctly, if I'll declare TEST in any method. But now I'm having this:
org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'homeController' defined in file [C:\Users\anton\springsource\vfabric-tc-server-developer-2.8.2.RELEASE\base-instance\wtpwebapps\blog\WEB-INF\classes\net\babobka\blog\controller\HomeController.class]: Instantiation of bean failed; nested exception is org.springframework.beans.BeanInstantiationException: Could not instantiate bean class [net.babobka.blog.controller.HomeController]: Constructor threw exception; nested exception is java.lang.NullPointerException
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.instantiateBean(AbstractAutowireCapableBeanFactory.java:997)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBeanInstance(AbstractAutowireCapableBeanFactory.java:943)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:485)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:456)
at org.springframework.beans.factory.support.AbstractBeanFactory$1.getObject(AbstractBeanFactory.java:294)
at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:225)
at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:291)
at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:193)
at org.springframework.beans.factory.support.DefaultListableBeanFactory.preInstantiateSingletons(DefaultListableBeanFactory.java:585)
at org.springframework.context.support.AbstractApplicationContext.finishBeanFactoryInitialization(AbstractApplicationContext.java:913)
at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:464)
at org.springframework.web.context.ContextLoader.configureAndRefreshWebApplicationContext(ContextLoader.java:385)
at org.springframework.web.context.ContextLoader.initWebApplicationContext(ContextLoader.java:284)
at org.springframework.web.context.ContextLoaderListener.contextInitialized(ContextLoaderListener.java:111)
My root-context(I don't think it's helpful):
<context:annotation-config />
<context:component-scan base-package="net.babobka.blog" />
<mvc:resources mapping="/resources/**" location="/resources/" />
<cache:annotation-driven />
<bean id="cacheManager" class="org.springframework.cache.support.SimpleCacheManager">
<property name="caches">
<set>
<bean
class="org.springframework.cache.concurrent.ConcurrentMapCacheFactoryBean"
p:name="template" />
<bean
class="org.springframework.cache.concurrent.ConcurrentMapCacheFactoryBean"
p:name="headHunter" />
</set>
</property>
</bean>
<bean id="converter" class="net.babobka.blog.headHunter.Converter" />
<bean id="propertyConfigurer"
class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer"
p:location="/WEB-INF/db/jdbc.properties" />
<bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource"
destroy-method="close" p:driverClassName="${jdbc.driverClassName}"
p:url="${jdbc.databaseurl}" p:username="${jdbc.username}" p:password="${jdbc.password}" />
<bean id="sessionFactory"
class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
<property name="dataSource" ref="dataSource" />
<property name="configLocation">
<value>/WEB-INF/db/hibernate.cfg.xml</value>
</property>
<property name="configurationClass">
<value>org.hibernate.cfg.AnnotationConfiguration</value>
</property>
<property name="hibernateProperties">
<props>
<prop key="hibernate.dialect">${jdbc.dialect}</prop>
<prop key="hibernate.show_sql">${jdbc.show_sql}</prop>
<prop key="hibernate.connection.useUnicode">true</prop>
<prop key="hibernate.connection.characterEncoding">UTF-8</prop>
<prop key="hibernate.connection.charSet">UTF-8</prop>
</props>
</property>
</bean>
<bean id="transactionManager"
class="org.springframework.orm.hibernate3.HibernateTransactionManager">
<property name="sessionFactory" ref="sessionFactory" />
</bean>
<mvc:annotation-driven />
<tx:annotation-driven />
<task:annotation-driven />
<bean id="Backupper" class="net.babobka.blog.backupper.Backupper"></bean>
<bean id="OldDataRemoval" class="net.babobka.blog.termination.OldDataRemoval"></bean>
<bean id="HeadHunterImport" class="net.babobka.blog.headHunter.HeadHunterImport"></bean>
<bean id="urlForwardController"
class="org.springframework.web.servlet.mvc.UrlFilenameViewController" />
<bean id="tilesConfigurer"
class="org.springframework.web.servlet.view.tiles2.TilesConfigurer">
<property name="definitions">
<list>
<value>/WEB-INF/tiles.xml</value>
</list>
</property>
</bean>
<bean id="viewResolver"
class="org.springframework.web.servlet.view.UrlBasedViewResolver">
<property name="viewClass"
value="org.springframework.web.servlet.view.tiles2.TilesView" />
</bean>
</beans>
What's wrong?
It's not working because templatesService is not yet injected when you reference it to set TEST. Fields injection will only work after the instantiation of your Object. What you need to solve this is use #PostConstruct to set test.
#Controller
public class HomeController {
#Autowired
private TemplatesService templatesService;
public String test;
#PostConstruct
public void init() {
this.test = templatesService.getUri();
}
if you still want to use final, you can use injection by constructor and set TEST then.
#Controller
public class HomeController {
private TemplatesService templatesService;
public final String TEST;
#Autowired
public HomeController(TemplatesService templatesService) {
this.templatesService = templatesService;
this.TEST = templatesService.getUri();
}
This is a problem of initialization order. Spring will inject dependencies into #Autowired fields after it has invoked a bean's constructor, but a field initializer is executed during object construction - and therefore before the field has been autowired. The field is therefore still null, which is why attempting to invoke a method throws a NullPointerException.
Alternatives:
pass the TemplatesService by constructor injection, and assign the final field from the constructor rather than the field initializer
continue to inject the TemplatesService by field injection, but move initialization of the bean into a #PostConstruct method. You will have to remove final to pacify the compiler.
inject the TemplatesService by setter injection, and perform the necessary initialization in the setter. You will have to remove final to pacify the compiler.

Resources