找到包含Hibernate Bar的所有Foo

时间:2015-06-25 08:02:22

标签: java hibernate

我有一个班级Foo和一个班级Bar,其中FooBar有多对多的关系。

Foo看起来像这样:

@Entity
@Table(name = "Foo", catalog = "xxx")
public class Foo implements java.io.Serializable{
    private int id;
    private String name;
    private Set<Bar> allBars;

    public Bar(){
        allBars = new HashSet<Bar>(0);
    }

    @Id
    @GeneratedValue(strategy = IDENTITY)
    @Column(name = "Foo_id", unique = true, nullable = false)
    public int getId(){
        return this.id;
    }

    public void setId(int id){
        this.id = id;
    }

    @Column(name = "Name", nullable = false)
    public String getName(){
        return this.name;
    }

    public void setName(String n) {
        this.name = n;
    }

    @ManyToMany(fetch = FetchType.LAZY, cascade = CascadeType.ALL)
    @JoinTable (name = "Foo_Bars", catalog = "xxx", joinColumns = {
            @JoinColumn(name = "Foo_id", nullable = false, updatable = false) },
            inverseJoinColumns = { @JoinColumn(name = "Bar_id", nullable = false, updatable = false) })
    public Set<Bar> getAllBars() {
        return this.allBars;
    }

    public void setAllBars(Set<Bar> allBars) {
        this.allBars = allBars;
    }
}

Bar看起来像这样

@Entity
@Table (name = "Bar", catalog = "xxx")
public class Bar implements java.io.Serializable{
    private int    id;
    private String name;

    public Bar(){}

    @Id
    @GeneratedValue(strategy = IDENTITY)
    @Column(name = "Bar_id", unique = true, nullable = false)
    public int getId() {
        return this.id;
    }

    public void setId(int id) {
        this.id = id;
    }

    @Column(name="Name",  nullable = false)
    public String getName(){
        return this.name;
    }

    public void setName(String nm){
        this.name = nm;
    }
}

问题

我想在我的public List<Foo> retrieveFooForBarId(int bar_id)课程中设置一个名为FooDaoImpl的方法,以便我可以检索包含某个Foo的所有Bar

实施例

我有2个Foo,第一个包含2 BarBar包含id=1id=2。第二个Foo包含2个Bar,其中一个id=1,另一个id=3。这两个Foo都包含Barid=1

SQL中,查询将是

SELECT * FROM Foo
LEFT JOIN Foo_Bars ON Foo.Foo_id = Foo_Bars.Foo_id
WHERE Foo_Bars.Bar_id = 1;

我如何使用Hibernate实现这一目标?

2 个答案:

答案 0 :(得分:0)

如果您只需要Foo,则可以使用HQL

from Foo foo
left join foo.allBars bar
where bar.id = 1

但是,生成的SQL将类似于

SELECT Foo.* FROM Foo
LEFT JOIN Foo_Bars ON Foo.Foo_id = Foo_Bars.Foo_id
WHERE Foo_Bars.Bar_id = 1;

这意味着,foo.allBars将不会被检索,因此会被懒惰地取出。这可能与您的要求略有不同

答案 1 :(得分:-1)

如果您使用的是hql,则可以使用等效查询

你有

public List<Foo> getFoo(long id) {
       Session session =   HibernateUtil.getSessionFactory().getCurrentSession();
          List<Foo> feeds = null;
        try {
                Transaction tx=session.beginTransaction();
                feeds = (List<Foo>) session.createQuery(
                   "from Foo as foo " 
                   +"left join foo.bar as bar "
                  +"where bar.id=:id")
                  .setParameter("id", id)
                   .list();
           }Catch(HibernateException e){
             System.out.println("Exception "+e);
             if(tx!=null){
                   tx.rollback();
            }   
         }