保持Hibernate会话通过注释失效?(Keeping Hibernate Session from Expiring via Annotations? — failed to lazily initialize a collection of role)
当试图访问一个包/集合的返回对象的成员时,我有一个hibernate DAO抛出一个“无法懒惰地初始化一个角色集合”异常。
我理解抛出异常的问题的范围。 Hibernate返回我的对象,并且对于任何集合,返回代理对象。 在我的调用者中,当我去访问这些代理对象时, 因为hibernate会话已经过期 ,所以抛出了这个异常。
我想知道的是,如何避免会话使用注释过期? 可能吗?
例如,如果我的调用方法是:
@RequestMapping("refresh.url") public ModelAndView refresh(HttpServletRequest request, HttpServletResponse response, int id) throws Exception { TestObject myObj = testObjDao.get(id); // throws the exception myObj.getCollection();
我将如何使用注释来防止此异常? 我知道一个解决方案是通过一个以伪代码形式显示的回调来扩展hibernate会话:
@RequestMapping("refresh.url") public ModelAndView refresh(HttpServletRequest request, HttpServletResponse response, int id) throws Exception { session = get hibernate session... session.doInSession(new SessionCallback() { TestObject myObj = testObjDao.get(id); // no longer throws the exception myObj.getCollection(); });
但是这在我需要访问集合的所有函数中似乎都是重复的。 是不是有一种方法可以在那里简单地拍@Transactional注释并完成它? 如下所示:
@RequestMapping("refresh.url") @Transactional // this doesn't extend the session to this method? public ModelAndView refresh(HttpServletRequest request, HttpServletResponse response, int id) throws Exception { TestObject myObj = testObjDao.get(id); // throws the exception myObj.getCollection();
感谢您的帮助,向我解释这一点。
I have a hibernate DAO that is throwing a "failed to lazily initialize a collection of role" Exception when trying to access a member of the returned object that is a bag/Collection.
I understand the scope of the problem throwing the exception. Hibernate returns my object, and for any Collections, returns proxy objects. In my caller, when I go to access those proxy objects, because the hibernate session has expired, this exception is thrown.
What I want to know is, how can I keep the session from expiring using annotations? Is it possible?
For instance if my calling method is:
@RequestMapping("refresh.url") public ModelAndView refresh(HttpServletRequest request, HttpServletResponse response, int id) throws Exception { TestObject myObj = testObjDao.get(id); // throws the exception myObj.getCollection();
How would I prevent this exception using annotations? I know one solution would be to extend the hibernate session via a callback which in pseudocode might look something like:
@RequestMapping("refresh.url") public ModelAndView refresh(HttpServletRequest request, HttpServletResponse response, int id) throws Exception { session = get hibernate session... session.doInSession(new SessionCallback() { TestObject myObj = testObjDao.get(id); // no longer throws the exception myObj.getCollection(); });
but this seems rather repetitive to have in all of my functions that need to access collections. isn't there a way to simply slap an @Transactional annotation on there and be done with it? as in:
@RequestMapping("refresh.url") @Transactional // this doesn't extend the session to this method? public ModelAndView refresh(HttpServletRequest request, HttpServletResponse response, int id) throws Exception { TestObject myObj = testObjDao.get(id); // throws the exception myObj.getCollection();
thanks for your help in explaining this to me.
原文:https://stackoverflow.com/questions/6316124
最满意答案
DTO的规范定义是没有任何行为的对象的数据形状。
ViewModels是视图的模型。 ViewModel通常是来自一个或多个对象(或DTO)的完整或部分数据,以及特定于视图行为的任何附加成员(可由视图执行的方法,指示切换视图元素等的属性等)。 您可以将viewmodel视为视图加行为的所有数据。 ViewModel可能会也可能不会将一对一映射到业务对象或DTO。
顺便说一句,如果某个viewmodel需要一个持久对象的数据子集,NHibernate的预测就会派上用场。
The canonical definition of a DTO is the data shape of an object without any behavior.
ViewModels are the model of the view. ViewModels typically are full or partial data from one or more objects (or DTOs) plus any additional members specific to the view's behavior (methods that can be executed by the view, properties to indicate how toggle view elements etc...). You can look at the viewmodel as all the data for a view plus behaviors. ViewModels may or may not map one to one to business objects or DTOs.
By the way, NHibernate projections come in handy if a certain viewmodel needs a subset of the data from a persisted object.