lazy type尝试获取结果集时出现异常

时间:2016-10-07 20:08:50

标签: java spring hibernate

我试图通过使用域DAO中的getOrders(String domain)函数来获取订单 这是代码:

Set<Order> orders;
orders = domainService.getOrders('google.com');

但我一直收到此错误

  

未能懒惰地初始化角色集合:com.latinon.reportator.model.Domain.ordersDomain,无法初始化代理 - 无会话

哪一个是设置惰性类型而不会收到错误的正确方法

的POJO

@Entity
@Table(name="Domain")
public class Domain {

    private Set<Order> ordersDomain = new HashSet<Order>(0);
    private Set<User> userDomain = new HashSet<User>(0);

    @ManyToMany(fetch=FetchType.LAZY)
    @JoinTable(name="Order_Domain", joinColumns={
            @JoinColumn(name="Order_Domain_Domain_id")
    },inverseJoinColumns={
            @JoinColumn(name="Order_Domain_Order_id", nullable=false,updatable=false)
    })
    public Set<Order> getOrdersDomain() {
        return ordersDomain;
    }
    public void setOrdersDomain(Set<Order> ordersDomain) {
        this.ordersDomain = ordersDomain;
    }
}

顺序

@Entity
@Table(name="Order")
public class Order {

    private Set<Domain> orderDomains = new HashSet<Domain>(0); 

    @ManyToMany(fetch=FetchType.LAZY)
    @JoinTable(name="Order_Domain", joinColumns={
            @JoinColumn(name="Order_Domain_Order_id",nullable=false,updatable=false)
    }, inverseJoinColumns={
            @JoinColumn(name="Order_Domain_Domain_id",nullable=false,updatable=false)
    })
    public Set<Domain> getOrderDomains() {
        return orderDomains;
    }
    public Set<Domain> getOrderDomains() {
        return orderDomains;
    }
}

的DAO

public interface DomainDAO {
    public void saveDomain(Domain domain);
    public void updateDomain(Domain domain);
    public Domain getDomainById(Integer id);
    public Domain getDomain(String domain);
    public void deleteDomain(Integer id);
    public Set<Order> getOrders(String domain);
}

顺序

public interface OrderDAO {
    public void saveOrder(Order order);
    public void updateOrder(Order order);
    public Order getOrderById(Integer id);
    public void deleteOrder(Integer id);
    public Order getOrderByAdUnit(String adUnitId);
}

servicios

public interface DomainService {
    public void saveDomain(Domain domain);
    public void updateDomain(Domain domain);
    public Domain getDomainById(Integer id);
    public void deleteDomain(Integer id);
    public Domain getDomain(String domain);
    public Set<Order> getOrders(String domain);
} 

域名服务实施

@Service
public class DomainServiceImpl implements DomainService {
    private DomainDAO domainDao;
    public DomainDAO getDomainDao() {
        return domainDao;
    }
    public void setDomainDao(DomainDAO domainDao) {
        this.domainDao = domainDao;
    }

    public DomainServiceImpl() {
        // TODO Auto-generated constructor stub
    }
    @Transactional
    public Set<Order> getOrders(String domain) {
        Domain domainAux = this.domainDao.getDomain(domain);
        return domainAux.getOrdersDomain();
    }
}

顺序

public interface OrderService {
    public void saveOrder(Order order);
    public void updateOrder(Order order);
    public Order getOrderById(Integer id);
    public void deleteOrder(Integer id);
    public Order getOrderByAdUnit(String adUnitId);
}

订购服务实施

@Service
public class OrderServiceImpl implements OrderService {

    private OrderDAO orderDAO;

    public OrderDAO getOrderDAO() {
        return orderDAO;
    }
    public void setOrderDAO(OrderDAO orderDAO) {
        this.orderDAO = orderDAO;
    }

    public OrderServiceImpl() {
        // TODO Auto-generated constructor stub
    }
    @Transactional
    public Order getOrderByAdUnit(String adUnitId) {
        return this.orderDAO.getOrderByAdUnit(adUnitId);
    }
}

1 个答案:

答案 0 :(得分:1)

在Hibernate中,如果某些内容被延迟加载,则应在会话可用时加载它。会话关闭后,无法加载(延迟初始化异常)

在DomainServiceImpl.getDomain方法中,如下所示:

Hibernate.initialize(domainAux.getOrdersDomain())

如果您的DAO是您的会话边界,请在相应的DAO方法中执行上述操作。