使用Hibernate进行对象关系映射 - “Sessionbound”是什么意思?

时间:2021-07-20 13:28:42

I'm currently trying to take my first steps with Hibernate, but unfortunately I don't really get what "sessionbound" means.

我目前正在尝试使用Hibernate的第一步,但不幸的是我并没有真正得到“sessionbound”的意思。

Let's say I've got a transient object. Once it's made persistent, it not only has an ID, but is also "sessionbound". But why is that worth mentioning?

假设我有一个瞬态对象。一旦它变得持久,它不仅具有ID,而且还是“会话绑定”。但为什么值得一提呢?

2 个解决方案

#1


4  

A Session in Hibernate is closely related to a transaction, and also serves as a first level cache. Every time you read or store an object, it is automatically bound to the current session (thus placed in the L1 cache).

Hibernate中的会话与事务密切相关,也可用作第一级缓存。每次读取或存储对象时,它都会自动绑定到当前会话(因此放在L1缓存中)。

Most importantly, as long as an object is bound to the session:

最重要的是,只要对象绑定到会话:

  • You can take advantage of lazy loading of relationships (otherwise you will get the dreadful LazyInitializationException).

    您可以利用延迟加载关系(否则您将获得可怕的LazyInitializationException)。

  • All changes to this object from that moment will be automatically populated to the underlying database. As long as an object is bound to the session, there is no need to persist it manually.

    从那一刻起,对该对象的所有更改都将自动填充到基础数据库。只要对象绑定到会话,就不需要手动持久化。

#2


1  

Also keep in mind that when using a transaction management layer like the one found in spring, your session will typically go away when your outermost transaction is comitted:

还要记住,当使用像春天那样的事务管理层时,你的会话通常会在最外面的事务被调用时消失:

public class MyRep implements Rep {
   @Transactional
   public MyObject findMyObject(..) { ... }
}

So when you activate transaction management in spring, and lets assume you're not already in a transaction, if you do this:

因此,当您在spring中激活事务管理时,如果您执行此操作,我们假设您尚未处理事务:

MyObject o = rep.findMyObject(...);

By the time this method finishes, your object is no longer bound to a session (because spring has closed your session). There's ways to overcome this, but i won't get into that here, just wanted you to be aware.

到此方法完成时,您的对象不再绑定到会话(因为spring已关闭您的会话)。有办法克服这一点,但我不会在这里进入,只是想让你知道。

#1


4  

A Session in Hibernate is closely related to a transaction, and also serves as a first level cache. Every time you read or store an object, it is automatically bound to the current session (thus placed in the L1 cache).

Hibernate中的会话与事务密切相关,也可用作第一级缓存。每次读取或存储对象时,它都会自动绑定到当前会话(因此放在L1缓存中)。

Most importantly, as long as an object is bound to the session:

最重要的是,只要对象绑定到会话:

  • You can take advantage of lazy loading of relationships (otherwise you will get the dreadful LazyInitializationException).

    您可以利用延迟加载关系(否则您将获得可怕的LazyInitializationException)。

  • All changes to this object from that moment will be automatically populated to the underlying database. As long as an object is bound to the session, there is no need to persist it manually.

    从那一刻起,对该对象的所有更改都将自动填充到基础数据库。只要对象绑定到会话,就不需要手动持久化。

#2


1  

Also keep in mind that when using a transaction management layer like the one found in spring, your session will typically go away when your outermost transaction is comitted:

还要记住,当使用像春天那样的事务管理层时,你的会话通常会在最外面的事务被调用时消失:

public class MyRep implements Rep {
   @Transactional
   public MyObject findMyObject(..) { ... }
}

So when you activate transaction management in spring, and lets assume you're not already in a transaction, if you do this:

因此,当您在spring中激活事务管理时,如果您执行此操作,我们假设您尚未处理事务:

MyObject o = rep.findMyObject(...);

By the time this method finishes, your object is no longer bound to a session (because spring has closed your session). There's ways to overcome this, but i won't get into that here, just wanted you to be aware.

到此方法完成时,您的对象不再绑定到会话(因为spring已关闭您的会话)。有办法克服这一点,但我不会在这里进入,只是想让你知道。