Hibernate Select查询返回null外部对象

时间:2015-03-12 00:15:10

标签: mysql hibernate jpa-2.1

我定义了以下实体: 餐厅:

@Entity
@Table(name = "restaurantes")
public class Restaurante implements Serializable {

    private int id;
    private Set<Menu> menus = new HashSet<>(0);

    public Restaurante() {
    }

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

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

    @JsonIgnore
    @OneToMany(mappedBy = "restaurante")
    public Set<Menu> getMenus() {
        return menus;
    }

    public void setMenus(Set<Menu> menus) {
        this.menus = menus;
    }
}

菜单:

@Entity
@Table(name = "menus")
public class Menu implements Serializable {
    private int id;
    private Restaurante restaurante;

    public Menu() {
    }

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

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

    @JsonIgnore
    @ManyToOne(fetch = FetchType.EAGER)
    @JoinColumn(name = "restaurante_id", nullable = false)
    public Restaurante getRestaurante() {
        return restaurante;
    }

    public void setRestaurante(Restaurante Restaurante) {
        this.restaurante = restaurante;
    }
}

当我执行以下查询时:

public Menu getMenuById(Integer id) throws HibernateException, IndexOutOfBoundsException {
    session = HibernateSessionService.getSessionFactory().openSession();
    criteria = session.createCriteria(Menu.class)
            .add(Restrictions.eq("id", id));
    return (Menu) criteria.list().get(0);
}

酒店餐厅为空。 数据库中加载了一个带有Id I&m; m使用的餐馆,并且有几个菜单也加载了该外键。

有人可以给我建议或帮助我吗?

提前致谢!

0 个答案:

没有答案