如何在HQL中编写连接查询?

时间:2015-04-14 07:03:05

标签: java hibernate orm hql

我正在尝试运行一个HQL查询,它给出了错误说:

org.hibernate.QueryException: could not resolve property:
UserType of: EntityPack.UserTypeMenu 
[from EntityPack.UserTypeMenu as utm ,EntityPack.UserType as ut 
 where utm.UserType.userTypeId=ut.userTypeId and ut.userTypeDesc like ' %ad%' ]

这是我写查询的函数:



    public ObservableList PostTableusertypemenu(String search, int q) {
            ObservableList data;
            data = FXCollections.observableArrayList();
            List ll=null;
                 ll = pop.UrviewTable("from UserTypeMenu as utm ,UserType as ut "+
                                      "where utm.UserType.userTypeId=ut.userTypeId"+
                                    " and ut.userTypeDesc like ' %"+ search +"%' ");
            Iterator ite = ll.iterator();
            while (ite.hasNext()) {
                UserTypeMenu obj = (UserTypeMenu) ite.next();
                data.add(obj);
            }  
            return data;
        }

我的UserType实体类:


    @Entity
    @Table(name = "user_type")
    public class UserType {
            @Id
        @GeneratedValue(strategy = GenerationType.AUTO)
        @Basic(optional = false)
        @Column(name = "User_Type_Id")
        private Integer userTypeId;
        @Basic(optional = false)
        @Column(name = "User_Type")
        private String userType;
        @Basic(optional = false)
        @Column(name = "User_Type_Desc")
        private String userTypeDesc;
        @Basic(optional = false)
        @Column(name = "Status")
        private String status;
          @Basic(optional = false)
        @Column(name = "Markers")
        private String markers;
    }

UserTypeMenu实体类:



    @Entity
    @Table(name = "user_type_menu")
    @NamedQueries({
        @NamedQuery(name = "UserTypeMenu.findAll", query = "SELECT u FROM UserTypeMenu u")})
    public class UserTypeMenu implements Serializable {
        private static final long serialVersionUID = 1L;
        @Id
        @GeneratedValue(strategy = GenerationType.AUTO)
        @Basic(optional = false)
        @Column(name = "User_Type_Menu_Id")
        private Integer userTypeMenuId;
        @Basic(optional = false)
        @Column(name = "Status")
        private String status;
        @Basic(optional = false)
        @Column(name = "Markers")
        private String markers;
        @ManyToOne(optional = false)
        private UserType userType;
        @ManyToOne(optional = false)
        private UserMenuMaster userMenuMaster;
        @ManyToOne(optional = false)
        private UserMenuBar userMenuBar;
    }

我想要的是根据Usertype description从UsertypeMenu获取数据。

请帮帮我..  谢谢。 :)

1 个答案:

答案 0 :(得分:1)

您不像在SQL中那样在HQL中编写联接,而是使用.表示法来浏览对象图。试试这个

"from UserTypeMenu as utm where utm.userType.userTypeDesc like ' %"+ search +"%' "

实际上,您可以直接在关联上使用联接。这是相同的查询,但使用连接语法

"from UserTypeMenu as utm join utm.userType ut where ut.userTypeDesc like ' %"+ search +"%' "

此处使用联接的好处是,如果关系不是强制性的,您可以指定left join,并且由于隐式使用inner join而您不希望丢失任何结果当您使用.