Note that there are some explanatory texts on larger screens.

plurals
  1. PO@Transactional+spring+hibernate doesn't work
    text
    copied!<p>My context:</p> <pre><code>`&lt;?xml version="1.0" encoding="UTF-8"?&gt; &lt;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:p="http://www.springframework.org/schema/p" xmlns:tx="http://www.springframework.org/schema/tx" xmlns:jdbc="http://www.springframework.org/schema/jdbc" xsi:schemaLocation=" http://www.springframework.org/schema/jdbc http://www.springframework.org/schema/jdbc/spring-jdbc-3.1.xsd 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/context http://www.springframework.org/schema/context/spring-context-3.1.xsd"&gt; &lt;context:annotation-config /&gt; &lt;context:component-scan base-package="com.globerry.project.domain" /&gt; &lt;context:component-scan base-package="com.globerry.project.dao" /&gt; &lt;context:component-scan base-package="com.globerry.project.service" /&gt; &lt;!-- Файл с настройками ресурсов для работы с данными (Data Access Resources) --&gt; &lt;tx:annotation-driven transaction-manager="transactionManager" /&gt; &lt;!-- Менеджер транзакций --&gt; &lt;!-- Настройки бина dataSource будем хранить в отдельном файле --&gt; &lt;bean id="propertyConfigurer" class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer" p:location="classpath:/META-INF/jdbc.properties" /&gt; &lt;!-- Непосредственно бин dataSource --&gt; &lt;bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource" p:driverClassName="com.mysql.jdbc.Driver" p:url="${jdbc.databaseurl}" p:username="${jdbc.username}" p:password="${jdbc.password}" /&gt; &lt;!-- Настройки фабрики сессий Хибернейта --&gt; &lt;bean id="sessionFactory" class="org.springframework.orm.hibernate3.annotation.AnnotationSessionFactoryBean" p:packagesToScan="com.globerry.project.Dao"&gt; &lt;property name="dataSource" ref="dataSource" /&gt; &lt;property name="configLocation"&gt; &lt;value&gt;classpath:hibernate.cfg.xml&lt;/value&gt; &lt;/property&gt; &lt;property name="configurationClass"&gt; &lt;value&gt;org.hibernate.cfg.AnnotationConfiguration&lt;/value&gt; &lt;/property&gt; &lt;property name="hibernateProperties"&gt; &lt;props&gt; &lt;prop key="hibernate.show_sql"&gt;true&lt;/prop&gt; &lt;prop key="hibernate.current_session_context_class"&gt;thread&lt;/prop&gt; &lt;prop key="hibernate.transaction.factory_class"&gt;org.hibernate.transaction.JDBCTransactionFactory&lt;/prop&gt; &lt;prop key="hibernate.dialect"&gt;org.hibernate.dialect.MySQLInnoDBDialect&lt;/prop&gt; &lt;prop key="hibernate.connection.charSet"&gt;UTF-8&lt;/prop&gt; &lt;/props&gt; &lt;/property&gt; &lt;/bean&gt; &lt;bean id="transactionManager" class="org.springframework.orm.hibernate3.HibernateTransactionManager"&gt; &lt;property name="sessionFactory" ref="sessionFactory" /&gt; &lt;/bean&gt; &lt;/beans&gt;` ---------- </code></pre> <p>My DaoClass</p> <pre><code>` @Repository public class CityDao implements ICityDao { @Autowired SessionFactory sessionFactory; @Override @Transactional(readOnly=true) public City getCityById(int id) { City city = (City) sessionFactory.getCurrentSession().get(City.class, id); Hibernate.initialize(city.getEvents()); return city; } } ` ---------- </code></pre> <p>I Have problem with My Dao Method.</p> <pre><code>getCityById(int id). </code></pre> <p>Hibernate throws Exception</p> <pre><code>org.hibernate.HibernateException: get is not valid without active transaction at org.hibernate.context.ThreadLocalSessionContext$TransactionProtectionWrapper.invoke(ThreadLocalSessionContext.java:341) at $Proxy36.createQuery(Unknown Source) at com.globerry.project.dao.CityDao.getCityById(CityDao.java:290) at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57) at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) at java.lang.reflect.Method.invoke(Method.java:601) at org.springframework.aop.support.AopUtils.invokeJoinpointUsingReflection(AopUtils.java:318) at org.springframework.aop.framework.ReflectiveMethodInvocation.invokeJoinpoint(ReflectiveMethodInvocation.java:183) at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:150) at org.springframework.transaction.interceptor.TransactionInterceptor.invoke(TransactionInterceptor.java:110) at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:172) at org.springframework.aop.framework.JdkDynamicAopProxy.invoke(JdkDynamicAopProxy.java:202) at $Proxy34.getCityById(Unknown Source) at com.globerry.project.dao.CityDaoTest.LazyTest(CityDaoTest.java:274) at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57) at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) at java.lang.reflect.Method.invoke(Method.java:601) at org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:45) at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:15) at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:42) at org.junit.internal.runners.statements.InvokeMethod.evaluate(InvokeMethod.java:20) at org.junit.internal.runners.statements.RunBefores.evaluate(RunBefores.java:28) </code></pre> <p>com.globerry.project.dao.CityDaoTest.LazyTest() marked as Transactional. CityDao autowired. I saw a lot of examples, import all nessasary libraries, but it doesn't works.</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload