DAO,Spring和Hibernate [英] DAO, Spring and Hibernate

查看:91
本文介绍了DAO,Spring和Hibernate的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



现在当我们使用Spring DAO for ORM模板时,当我们使用@Transactional属性
时,我们没有当方法被外部调用时,而不是在方法内部时,控制事务和/或会话。

延迟加载节省资源 - 减少对db的查询,减少内存所有收集到的应用程序内存中。



因此,如果lazy = false,那么一切都会被提取,所有关联的集合都不是有效的,在一个链接集中。

现在,我在DAO类中有一个方法,它应该返回一个User对象。
它具有表示数据库链接表的集合。
我需要通过id获得一个对象,然后查询它的集合。



当我试图访问Hibernate时,发生未能懒惰地初始化集合异常这个DAO方法返回的链接集合。



请解释一下,这里有什么解决方法?

更新:好的,让我问你这个问题。 DAO是一个抽象层,所以getUserById(Integer id)方法应该返回一个Object。

如果在某些情况下我需要这些User对象的链接集合,并且在其他情况下我需要这些集合。

只有两种方法:
1)lazy loading = false
2)创建不同的方法:getUserByIdWithTheseCollections(),getUserByIdWithOtherCollections()和inside那些方法使用你的方法?



我的意思是只有2种方法,没有更好的办法吗?



更新2 :请说明一下,有什么会给我明确使用SESSIONFACTORY?
它在实践中看起来如何?我们创建一个DAO对象实例
,然后使用会话工厂注入它,这意味着对DAO的两个随后的
方法调用将在同一个事务中运行?
在我看来,无论如何,DAO是从使用它的类中分离的!



逻辑和事务封装在DAO中,对吗? / p>

解决方案

您可以在事务中获取链接集合以在您仍处于事务中时加载它。

  User user = sessionFactory.getCurrentSession()。get(User.class,userId); 
user.getLinkedCollection()。size();
返回用户;

正如BalusC指出的那样,您可以使用 Hibernate.initialize() / code>而不是 size()



然后当你返回这样一个实体时,lazy字段已经被初始化了。



回复您的PS - 是否使用服务级别(而不是DAO)级别的交易是可行的?它似乎是,因为在单独的事务中进行每个DAO调用似乎是一种浪费(并且可能不正确)。

Correct me if anything is wrong.

Now when we use Spring DAO for ORM templates, when we use @Transactional attribute, we do not have control over the transaction and/or session when the method is called externally, not within the method.

Lazy loading saves resources - less queries to the db, less memory to keep all the collections fetched in the app memory.

So, if lazy=false, then everything is fetched, all associated collections, that is not effectively, if there are 10,000 records in a linked set.

Now, I have a method in a DAO class that is supposed to return me a User object. It has collections that represent linked tables of the database. I need to get a object by id and then query its collections.

Hibernate "failed to lazily initialize a collection" exception occurs when I try to access the linked collection that this DAO method returns.

Explain please, what is a workaround here?

Update: All right, let me ask you this. DAO is an abstract layer, so a method "getUserById(Integer id)" is supposed to return an Object.

What if in some cases I need these linked collections of the User object and in other situation I need those collections.

Are there only two ways: 1) lazy loading = false 2) create different methods: getUserByIdWithTheseCollections(), getUserByIdWithOtherCollections() and inside those methods use your approach?

I mean are there only 2 ways and nothing better?

Update 2: Explain please, what would give me the explicit use of SESSIONFACTORY? How does it look in practice? We create an instance of DAO object, then inject it with session factory and this would mean that two consequent method calls to DAO will run within the same transaction? It seems to me that anyway, DAO is detached from the classes that make use of it!

The logic and transactions are encapsulated within DAO, right?

解决方案

You can get the linked collection in transaction to load it while you're still within the transaction:

User user = sessionFactory.getCurrentSession().get(User.class, userId);
user.getLinkedCollection().size();
return user;

As BalusC has pointed out, you can use Hibernate.initialize() instead of size(). That's a lot cleaner.

Then when you return such an entity, the lazy field is already initialized.

Replying to your PS - is using transactions on service level (rather than DAO) level feasible? It seems to be, as doing each DAO call in separate transaction seems to be a waste (and may be incorrect).

这篇关于DAO,Spring和Hibernate的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆