等同于ActiveRecord的has_many的Hibernate:through [英] Hibernate equivalent of ActiveRecord's has_many :through
问题描述
在Rails中,可以声明一个传递关系,如下所示:
In Rails it is possible to declare a transitive relation as follows:
class Author < ActiveRecord::Base
has_many :authorships
has_many :books, :through => :authorships
end
是否可以在Hibernate中执行类似的操作?当我打电话给author.getBooks()
时,我希望Hibernate知道与具有本书著作权的作者一起加入.
Is it possible to do something similar in Hibernate? When I call author.getBooks()
I want Hibernate to know to join authors with authorships with books.
推荐答案
我不相信Hibernate的映射中可以完成任何操作,但是您可以在Author
类中简单地添加一个getBooks()
方法,在authorships
属性上调用正确的方法:
I don't believe there is anything in Hibernate's mappings that can accomplish this, but you could simply add a getBooks()
method to your Author
class that calls the correct method on the authorships
property:
public class Author {
public Collection<Book> getBooks() {
if (this.authorships != null) {
return this.authorships.getBooks();
}
return null;
}
}
如果您可以自己在类中进行设置,我不确定为什么ORM需要知道A与C之间的传递关系.
I'm not sure why the ORM would need to know about a transitive relationship between A and C between B, if you can just set that up in the class on it's own.
这篇关于等同于ActiveRecord的has_many的Hibernate:through的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!