简体   繁体   中英

LazyInitializationException Spring and Hibernate

I am getting this exception nested exception is org.hibernate.LazyInitializationException: failed to lazily initialize a collection of role: com.ibm.ro.model.Location.subLocations, could not initialize proxy - no Session.

I do get that upon accessing the collection, the transaction has already been closed that's why the code is throwing this exception. Here is my sample code

@Entity
@Table(name="location")
public class Location extends BaseEntity {

    private static final long serialVersionUID = 1L;

    private String name;
    private List<Location> subLocations;
    private Location location;

    @Column(name="name")
    public String getName() {
        return name;
    }
    public void setName(String name) {
        this.name = name;
    }

    @OneToMany(fetch = FetchType.LAZY, mappedBy = "location")
    public List<Location> getSubLocations() {
        return subLocations;
    }
    public void setSubLocations(List<Location> subLocations) {
        this.subLocations = subLocations;
    }

    @ManyToOne(fetch = FetchType.LAZY)
    public Location getLocation() {
        return location;
    }
    public void setLocation(Location location) {
        this.location = location;
    }

    @Override
    public String toString() {
        return "Location [name=" + name + ", subLocations=" + subLocations
            + "]";
    }

}

Here is my DAO:

@Repository("locationDao")
public class LocationDao implements ILocationDao{

    @Autowired
    private SessionFactory sessionFactory;

    @Override
    public List<Location> getAll() {
        Session sess = getSession();
        return sess.createCriteria(Location.class).setResultTransformer(CriteriaSpecification.DISTINCT_ROOT_ENTITY).list();
    }
}

Then here is my service :

@Service("locationService")
@Transactional
public class LocationService implements ILocationService{

    @Autowired
    ILocationDao dao;

    @Override
    public List<Location> getAll() {
        return dao.getAll();
    }
}

Here is the controller where the exception is being thrown:

@Controller
public class BaseController {

    @Autowired
    ILocationService service;

    private static final String VIEW_INDEX = "index";
    private final static org.slf4j.Logger logger =    LoggerFactory.getLogger(BaseController.class);

    @RequestMapping(value = "/", method = RequestMethod.GET)
    public String location(ModelMap model) {        
    logger.debug(service.getAll().toString());
        return VIEW_INDEX;
    }
}

What can I do to fix the problem without using OpenSessionInViewFilter?

You can iterate your Location inside your service (where you still have your transaction) and call Hibernate.initialize on the elements, the force initialization of a persistent collection.

    @Override
    public List<Location> getAll() {
        List<Location> locations = dao.getAll();
        for (Location location : locations ) { 
          Hibernate.intialize(location.getSubLocations())
        }
        return locations;
    }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM