等同于ActiveRecord的has_many的Hibernate:through [英] Hibernate equivalent of ActiveRecord's has_many :through

查看:56
本文介绍了等同于ActiveRecord的has_many的Hibernate: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屋!

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