Hibernate关联缓存
问题一
根据Grails手册
除了使用 Hibernate 的二级缓存来缓存实例的能力之外,您还可以缓存对象的集合(关联)。例如:
class Person {
static hasMany = [addresses: Address]
static mapping = {
cache true
addresses cache: true
}
}
如果我们缓存一个人与其地址之间的关联,那么缓存反向关系是否有意义,例如,
class Address {
static belongsTo = [person: Person]
static mapping = {
cache true
person cache: true // is this necessary?
}
}
显然,只有当我们从地址导航时(在我们的应用程序代码中),缓存反向关系才有意义。到 Person,但假设关系是双向导航的,是否也需要双向缓存?
问题二
上一个问题是关于 1:N 关系上下文中的缓存。如果两者之间的关系是 1:1,那么大概可以/应该指定相同的缓存行为?例如:
class Person {
static hasOne = [address: Address]
static mapping = {
cache true
address cache: true
}
}
class Address {
static belongsTo = [person: Person]
static mapping = {
cache true
person cache: true
}
}
问题 III
如果我们在两个对象之间有 N:N 关系,并且我们在两个方向上导航该关系,则以下是缓存关联的正确方法:
class Person {
static hasMany = [personAddress: PersonAddress]
static mapping = {
cache true
personAddress cache: true
}
}
class PersonAddress {
static belongsTo = [person: Person, address: Address]
static mapping = {
cache true
person cache: true
address cache: true
}
}
class Address {
static hasMany = [personAddress: PersonAddress]
static mapping = {
cache true
personAddress cache: true
}
}
Question I
According to the Grails manual
As well as the ability to use Hibernate's second level cache to cache instances you can also cache collections (associations) of objects. For example:
class Person {
static hasMany = [addresses: Address]
static mapping = {
cache true
addresses cache: true
}
}
If we cache the association between a person and their addresses, then does it make sense to also cache the inverse relationship, e.g.
class Address {
static belongsTo = [person: Person]
static mapping = {
cache true
person cache: true // is this necessary?
}
}
Obviously it would only make sense to cache the inverse relationship if (in our application code) we navigate from Address to Person, but assuming the relationship is navigated in both directions, does it also need to be cached in both directions?
Question II
The previous question is about caching in the context of a 1:N relationship. If the relationship between the two is 1:1 instead, presumably the same caching behaviors could/shouid be specified? For example:
class Person {
static hasOne = [address: Address]
static mapping = {
cache true
address cache: true
}
}
class Address {
static belongsTo = [person: Person]
static mapping = {
cache true
person cache: true
}
}
Question III
If we have a N:N relationship between two objects, and we navigate the relationship in both directions, is the following the correct way to cache the associations:
class Person {
static hasMany = [personAddress: PersonAddress]
static mapping = {
cache true
personAddress cache: true
}
}
class PersonAddress {
static belongsTo = [person: Person, address: Address]
static mapping = {
cache true
person cache: true
address cache: true
}
}
class Address {
static hasMany = [personAddress: PersonAddress]
static mapping = {
cache true
personAddress cache: true
}
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
无法缓存
XxxtoOne
关联。但是,在大多数情况下,XxxToOne 关联是使用实体本身映射的表中的外键来映射的。因此,关联已经作为实体本身的一部分进行缓存。它仅在使用联接表的多对一的情况下有用(非常罕见),或者在一对一关联的反面的情况下(其中外键位于另一个表中)。我不知道在这种情况下 Hibernate 是否使用关联实体的缓存,但它可以。
XxxtoOne
associations can't be cached. However, in most of the cases, a XxxToOne association is mapped using a foreign key that is in the table mapped by the entity itself. So, the association is already cached as part of the entity itself.It could only be useful in the case of a ManyToOne using a join table (very rare), or in case of the inverse side of a OneToOne association (where the foreign key is in the other table). I don't know if Hibernate uses the cache of the associated entity in this case, but it could.