我正在Spring 3.0.5和Hibernate 3.6.2中开发应用程序,目前我正在JSON控制器中工作,但是我有此异常,我不明白为什么会发生。我之前在SO和Google中进行了检查,但是这个问题很奇怪。所以这是我的代码:

控制者

@RequestMapping(value = "/props", method = RequestMethod.GET)
public @ResponseBody
List<Property> getJsonProps(String id) {
    if(id==null)return null;
    Device dev = deviceService.getDispositivo(Long.parseLong(id));
    List<Property> props = deviceService.listProperties(dev, 10);
    return props;
}


设备服务

@Service("manageDevices")
@Transactional(readOnly=true,propagation=Propagation.REQUIRED)
public class ManageDevicesImpl implements ManageDevices {

private Logger log = LoggerFactory.getLogger(getClass());

@Autowired
private DevicesDAO devicesDAO;


    public List<Property> listProperties(Device dev, Integer qty) {
    List<Property> props =  devicesDAO.pickProperties(dev, qty);
    return props;
    }
}




@Repository("devicesDAO")
public class DevicesDAOImpl implements DevicesDAO {
private Logger log = LoggerFactory.getLogger(getClass());

@Autowired
private SessionFactory sessionFactory;

public List<Property> pickProperties(Device dev, Integer qty) {
    if(qty >= 0){
        log.debug("Open? "+ sessionFactory.getCurrentSession().isOpen());
        log.debug("Tx Active? " + sessionFactory.getCurrentSession().getTransaction().isActive());
        List<Property> props = dev.getProperties();
        if(props != null){
            if(props.size() >= qty)
                return props.subList(0, qty-1);
            else
                return props;
        }
    }
    return null;
}


}

在我尝试加载属性(getProperties)的那一行中,在pickProperties函数(DAO层)中发生了异常。在日志中,有一个打开的会话和事务。提前致谢。

最佳答案

您能否发布确切的例外情况?

您正在进行dev.getProperties()行中的事务,但是不在装入dev的事务中。您可能需要重新连接它,或者安排仍在装入它的事务中时调用dev.getProperties(),或者将事务边界上移以便两个调用都在同一事务中,或者更改Hibernate配置,以便properties不是延迟加载的,或者更改加载dev的代码,以便在HQL中获取properties

哪种选择适合您,取决于您的情况,但我将从最后一个开始。

07-26 07:17