@Transactional不使用spring和hibernate(没有活动事务,get无效)

时间:2015-03-07 09:50:16

标签: java spring hibernate transactional

我已经创建了一个spring-mvc应用程序。配置如下所示:

dispatcher-servlet.xml

<beans >
   <mvc:annotation-driven />
   <bean class="org.springframework.web.servlet.mvc.support.ControllerClassNameHandlerMapping"/>

   <context:component-scan base-package="my.server.controller" />

</beans>

的applicationContext.xml

<beans >
     <context:annotation-config />
     <tx:annotation-driven transaction-manager="transactionManager"/>
     <context:component-scan base-package="my.server.dao" />
     <context:component-scan base-package="my.server.service" />


    <bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close">
      <property name="driverClassName" value="org.postgresql.Driver" />
      <property name="url" value="jdbc:postgresql://localhost:5432/jahanserver" />
      <property name="username" value="postgres" />
      <property name="password" value="postgres" />
    </bean>

   <bean id="sessionFactory" class="org.springframework.orm.hibernate4.LocalSessionFactoryBean">
      <property name="dataSource" ref="dataSource" />
      <property name="annotatedClasses">
        <list>
            <value>my.server.entity.TreeItem</value>
            <value>my.server.entity.TreeItemDetail</value>
            <value>my.server.entity.LayerItem</value>
        </list>
      </property>
      <property name="hibernateProperties">
        <props>
           <prop key="hibernate.dialect">org.hibernate.spatial.dialect.postgis.PostgisDialect</prop>
            <prop key="hibernate.current_session_context_class">thread</prop>
            <prop key="hibernate.show_sql">true</prop>
            <prop key="hibernate.hbm2ddl.auto">update</prop>
            <prop key="hibernate.connection.characterEncoding">UTF-8</prop>
            <prop key="hibernate.connection.charSet">UTF-8</prop>
            <prop key="hibernate.default_schema">public</prop>
        </props>
      </property>
   </bean>
   <bean id="transactionManager" class="org.springframework.orm.hibernate4.HibernateTransactionManager">
      <property name="sessionFactory" ref="sessionFactory"/>
   </bean>
</beans>

applicationContext.xmlweb.xml中的以下行加载:

web.xml

<context-param>
    <param-name>contextConfigLocation</param-name>
    <param-value>/WEB-INF/applicationContext.xml, /WEB-INF/security.xml</param-value>
</context-param>

my.server.service.TreeServiceImpl

@Service
public class TreeServiceImpl implements TreeService {

   @Inject
   TreeDao treeDao;

   @Override
   @Transactional
   public TreeItem getTreeItem(Long id) {
       return treeDao.getTreeItem(id);
   }
}

my.server.dao.TreeDaoImpl

@Service
@Singleton
public class TreeDaoImpl extends AbstractDaoImpl implements TreeDao{

    public TreeItem getTreeItem(Long id){
        return (TreeItem)sessionFactory.getCurrentSession().get(TreeItem.class, id);
    }

    ...
}

my.server.controller.TreePermissionController.java

@RestController
@RequestMapping("/treePermission")
public class TreePermissionController implements InitializingBean{

   @Inject 
   TreeService treeService;

   private final PermissionFactory permissionFactory = new DefaultPermissionFactory();

   @Override
   public void afterPropertiesSet() throws Exception {
      Assert.notNull(treeService, "HBUtil cannot be null");
   }

   @RequestMapping(method = RequestMethod.GET, value = "/addPermission")
   public TreeItem addPermission(HttpServletRequest request, 
        @RequestParam Long id ) throws IOException {
       TreeItem item = treeService.getTreeItem(id);

       PrincipalSid sid = new PrincipalSid("dariush");
       Permission permission = permissionFactory.buildFromMask(16);

       try {
           treeService.addPermission(item, sid, permission);
       } catch (DataAccessException existingPermission) {
           existingPermission.printStackTrace();

       }
       return item;
   }

}

在控制器中我称之为:

TreeItem item = treeService.getTreeItem(id);

我面对这个错误:

org.hibernate.HibernateException: get is not valid without active transaction

有人提出了一些有同样问题的问题。我遵循了所有的建议,但对我来说仍然是一个问题。 我该如何解决?请帮忙!

1 个答案:

答案 0 :(得分:1)

使用spring和spring托管事务时,除非使用JTA,否则永远不要使用hibernate.current_session_context_class属性。

实际上,Spring默认会设置自己的CurrentSessionContext实现(SpringSessionContext),但是如果你自己设置它,那就不会意味着它会破坏正确的事务集成。

所以请删除/评论下面的行 applicationContext.xml中的hibernateProperties

<prop key="hibernate.show_sql">true</prop>

希望这可以解决您的错误