Причина: org.hibernate.HibernateException: createCriteria не является
действует без активной транзакции на
org.hibernate.context.internal.ThreadLocalSessionContext $ TransactionProtectionWrapper.invoke (ThreadLocalSessionContext.java:352)
на com.sun.proxy. $ Proxy63.createCriteria (неизвестный источник)
Конфигурация Hibernate
package com.cm;
import java.util.Properties;
import javax.sql.DataSource;
import org.hibernate.SessionFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.PropertySource;
import org.springframework.core.env.Environment;
import org.springframework.jdbc.datasource.DriverManagerDataSource;
import org.springframework.orm.hibernate4.HibernateTemplate;
import org.springframework.orm.hibernate4.HibernateTransactionManager;
import org.springframework.orm.hibernate4.LocalSessionFactoryBean;
import org.springframework.transaction.annotation.EnableTransactionManagement;
@Configuration
@EnableTransactionManagement
@PropertySource("classpath:config.properties")
@ComponentScan(basePackages = "com.cm")
public class HibernateConfiguration {
@Value("${db.username}")
private String userName;
@Value("${db.password}")
private String password;
@Value("${db.jdbcurl}")
private String jdbcurl;
/*@Value("${hibernate.dialect}")
private String dialect;*/
/*@Value("${hibernate.show_sql}")
private String show_sql;
@Value("${hibernate.format_sql}")
private String format_sql;
*/
@Bean
public LocalSessionFactoryBean sessionFactory() {
LocalSessionFactoryBean sessionFactory = new LocalSessionFactoryBean();
sessionFactory.setDataSource(dataSource());
sessionFactory.setPackagesToScan(new String[] { "com.cm.models" });
sessionFactory.setHibernateProperties(hibernateProperties());
return sessionFactory;
}
@Bean
public DataSource dataSource() {
DriverManagerDataSource dataSource = new DriverManagerDataSource();
dataSource.setDriverClassName("com.mysql.cj.jdbc.Driver");
dataSource.setUrl(jdbcurl);
dataSource.setUsername(userName);
dataSource.setPassword(password);
return dataSource;
}
private Properties hibernateProperties() {
Properties properties = new Properties();
properties.put("hibernate.dialect", "org.hibernate.dialect.MySQLDialect");
properties.put("hibernate.show_sql", true);
properties.put("hibernate.format_sql", true);
return properties;
}
@Bean
@Autowired
public HibernateTemplate getHibernateTemplate(SessionFactory sessionFactory)
{
HibernateTemplate hibernateTemplate = new HibernateTemplate(sessionFactory);
return hibernateTemplate;
}
@Bean
@Autowired
public HibernateTransactionManager transactionManager(SessionFactory s) {
HibernateTransactionManager txManager = new HibernateTransactionManager();
txManager.setSessionFactory(s);
return txManager;
}
}
@Service
public class TransactionService {
private TransactionDao transactionDao;
private TransactionDetailsDao transactionDetailDao;
@Autowired
public TransactionService(TransactionDao transactionDao, TransactionDetailsDao transactionDetailDao){
this.transactionDao = transactionDao;
this.transactionDetailDao = transactionDetailDao;
}
transactionDao.selectFromTransaction(bookRoomsRequest.getConfirmationNo(), bookRoomsRequest.getBookingStatus());
}
**DAO**
Если я пишу @Transectional, тогда я получаю проблему создания бина
при запуске приложения я также применил на сервисном уровне, но то же самое
выпуск.
@Repository
public class TransactionDaoImpl extends AbstractDao implements TransactionDao {
@SuppressWarnings("unchecked")
@Override
public boolean selectFromTransaction(String orderId, String type) throws SQLException {
/*
* String SQL_QUERY = "select * from transaction t where t.type = '" + type +
* "' and t.order_id='" + orderId +"';";
*/
List<Transaction> list = new ArrayList<>();
boolean response = false;
try {
Criteria criteria = getSession().createCriteria(Transaction.class);
Criterion Type = (Restrictions.eq("type", type));
Criterion Order_id = (Restrictions.eq("order_id", orderId));
criteria.add(Restrictions.and(Type, Order_id));
list = criteria.list();
if (!CollectionUtils.isEmpty(list)) {
response = true;
}
} catch (Exception e) {
e.printStackTrace();
throw e;
}
return response;
}
}
если я удаляю ThreadLocalSessionContext
из hibernate configuration
, тогда я получаю ошибку ниже.
org.hibernate.HibernateException: не удалось получить
синхронизированный транзакцией сеанс для текущего потока в
org.springframework.orm.hibernate4.SpringSessionContext.currentSession (SpringSessionContext.java:134)
Я правильно управляю транзакцией и перепробовал все возможные решения ( Spring Hibernate - Не удалось получить сеанс с синхронизацией транзакций для текущего потока ), но все же не повезло, пожалуйста, помогите найти точное решение для этой проблемы.
**On Putting @Transectional on on selectFromTransaction method i am getting below erro Stacktrace at application statup.**
Caused by: org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'retryServiceImpl' defined in file [/home/saurabhsharma/Desktop/fresh_proxycm/hotels-proxy-cm/.metadata/.plugins/org.eclipse.wst.server.core/tmp0/wtpwebapps/cm-proxy-api/WEB-INF/classes/com/paytm/cm/services/impl/RetryServiceImpl.class]: Unsatisfied dependency expressed through constructor parameter 1: No qualifying bean of type [com.paytm.cm.dao.impl.TransactionDaoImpl] found for dependency [com.paytm.cm.dao.impl.TransactionDaoImpl]: expected at least 1 bean which qualifies as autowire candidate for this dependency. Dependency annotations: {}; nested exception is org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type [com.paytm.cm.dao.impl.TransactionDaoImpl] found for dependency [com.paytm.cm.dao.impl.TransactionDaoImpl]: expected at least 1 bean which qualifies as autowire candidate for this dependency. Dependency annotations: {}
at org.springframework.beans.factory.support.ConstructorResolver.createArgumentArray(ConstructorResolver.java:749) ~[spring-beans-4.3.1.RELEASE.jar:4.3.1.RELEASE]
at org.springframework.beans.factory.support.ConstructorResolver.autowireConstructor(ConstructorResolver.java:189) ~[spring-beans-4.3.1.RELEASE.jar:4.3.1.RELEASE]
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.autowireConstructor(AbstractAutowireCapableBeanFactory.java:1143) ~[spring-beans-4.3.1.RELEASE.jar:4.3.1.RELEASE]
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBeanInstance(AbstractAutowireCapableBeanFactory.java:1046) ~[spring-beans-4.3.1.RELEASE.jar:4.3.1.RELEASE]
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:510) ~[spring-beans-4.3.1.RELEASE.jar:4.3.1.RELEASE]
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:482) ~[spring-beans-4.3.1.RELEASE.jar:4.3.1.RELEASE]
at org.springframework.beans.factory.support.AbstractBeanFactory$1.getObject(AbstractBeanFactory.java:306) ~[spring-beans-4.3.1.RELEASE.jar:4.3.1.RELEASE]
at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:230) ~[spring-beans-4.3.1.RELEASE.jar:4.3.1.RELEASE]
at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:302) ~[spring-beans-4.3.1.RELEASE.jar:4.3.1.RELEASE]
at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:197) ~[spring-beans-4.3.1.RELEASE.jar:4.3.1.RELEASE]
at org.springframework.beans.factory.config.DependencyDescriptor.resolveCandidate(DependencyDescriptor.java:187) ~[spring-beans-4.3.1.RELEASE.jar:4.3.1.RELEASE]
at org.springframework.beans.factory.support.DefaultListableBeanFactory.findAutowireCandidates(DefaultListableBeanFactory.java:1213) ~[spring-beans-4.3.1.RELEASE.jar:4.3.1.RELEASE]
at org.springframework.beans.factory.support.DefaultListableBeanFactory.doResolveDependency(DefaultListableBeanFactory.java:1053) ~[spring-beans-4.3.1.RELEASE.jar:4.3.1.RELEASE]
at org.springframework.beans.factory.support.DefaultListableBeanFactory.resolveDependency(DefaultListableBeanFactory.java:1018) ~[spring-beans-4.3.1.RELEASE.jar:4.3.1.RELEASE]
at org.springframework.beans.factory.support.ConstructorResolver.resolveAutowiredArgument(ConstructorResolver.java:834) ~[spring-beans-4.3.1.RELEASE.jar:4.3.1.RELEASE]
at org.springframework.beans.factory.support.ConstructorResolver.createArgumentArray(ConstructorResolver.java:741) ~[spring-beans-4.3.1.RELEASE.jar:4.3.1.RELEASE]
... 24 common frames omitted
Caused by: org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type [com.paytm.cm.dao.impl.TransactionDaoImpl] found for dependency [com.paytm.cm.dao.impl.TransactionDaoImpl]: expected at least 1 bean which qualifies as autowire candidate for this dependency. Dependency annotations: {}
at org.springframework.beans.factory.support.DefaultListableBeanFactory.raiseNoSuchBeanDefinitionException(DefaultListableBeanFactory.java:1403) ~[spring-beans-4.3.1.RELEASE.jar:4.3.1.RELEASE]
at org.springframework.beans.factory.support.DefaultListableBeanFactory.doResolveDependency(DefaultListableBeanFactory.java:1056) ~[spring-beans-4.3.1.RELEASE.jar:4.3.1.RELEASE]
at org.springframework.beans.factory.support.DefaultListableBeanFactory.resolveDependency(DefaultListableBeanFactory.java:1018) ~[spring-beans-4.3.1.RELEASE.jar:4.3.1.RELEASE]
at org.springframework.beans.factory.support.ConstructorResolver.resolveAutowiredArgument(ConstructorResolver.java:834) ~[spring-beans-4.3.1.RELEASE.jar:4.3.1.RELEASE]
at org.springframework.beans.factory.support.ConstructorResolver.createArgumentArray(ConstructorResolver.java:741) ~[spring-beans-4.3.1.RELEASE.jar:4.3.1.RELEASE]
... 39 common frames omitted
Nov 16, 2018 11:40:53 AM org.apache.c