如何为多个实体编写HQL查询

时间:2015-04-17 11:33:55

标签: hibernate orm hql hibernate-mapping

我正在尝试编写一个HQL查询来获取记录的id。但查询给了我这个错误

  

org.hibernate.QueryException:无法解析属性:CompanyMaster:EntityPack.UserTypeMenuBarComp [select userTypeMenuBar.userMenuBar.menuBarId FROM EntityPack.UserTypeMenuBarComp as UserTypeMenuBarComp,其中UserTypeMenuBarComp.companyGroupMaster.companyGroupId = 4,UserTypeMenuBarComp.CompanyMaster.companyMasterId = 2并且UserTypeMenuBarComp.userTypeMenuBar.userType.userTypeId = 2]

这是我的查询:

select userTypeMenuBar.userMenuBar.menuBarId FROM UserTypeMenuBarComp as UserTypeMenuBarComp where UserTypeMenuBarComp.companyGroupMaster.companyGroupId=" + 
    UsercompGid(LoginImpl.masterid) +
     " and UserTypeMenuBarComp.CompanyMaster.companyMasterId=" + 
    Usercompid(LoginImpl.masterid) +
     " and UserTypeMenuBarComp.userTypeMenuBar.userType.userTypeId=" + 
    LoginImpl.usertypid + ""

以下是实体类(UserTypeMenuCompany):

@Entity
public class UserTypeMenuCompany implements Serializable {
    private static final long serialVersionUID = 1L;
    @Id
    @GeneratedValue(strategy = GenerationType.AUTO)
    @Basic(optional = false)
    @Column(name = "User_Type_Menu_Company_Id")
    private Integer userTypeMenuCompanyId;
    @Basic(optional = false)
    @Column(name = "Status")
    private String status;
    @Column(name = "Markers")
    private String markers;
    @ManyToOne(optional = false)
    private UserTypeMenu userTypeMenu;
    @ManyToOne(optional = false)
    private CompanyMaster companyMaster;
    @JoinColumn(name = "Company_Group_Id", referencedColumnName = "Company_Group_Id")
    @ManyToOne(optional = false)
    private CompanyGroupMaster companyGroupMaster;

(UserTypeMenuBar)

public class UserTypeMenuBar implements Serializable {
    private static final long serialVersionUID = 1L;
    @Id
    @GeneratedValue(strategy = GenerationType.AUTO)
    @Basic(optional = false)
    @Column(name = "User_Type_Menu_Bar_Id")
    private Integer userTypeMenuBarId;
    @Basic(optional = false)
    @Column(name = "Status")
    private String status;
    private String markers;
    //@JoinColumn(name = "User_Type_Id", referencedColumnName = "User_Type_Id")
    @ManyToOne(optional = false)
    private UserType userType;
   // @JoinColumn(name = "Menu_Bar_Id", referencedColumnName = "Menu_Bar_Id")
    @ManyToOne(optional = false)
    private UserMenuBar userMenuBar;

(UserMenuBar)

public class UserMenuBar implements Serializable  {
    @Id
    @GeneratedValue(strategy = GenerationType.AUTO)
    @Basic(optional = false)
    @Column(name = "Menu_Bar_Id")
    private Integer menuBarId;
    @Basic(optional = false)
    @Column(name = "Menu_Bar_Desc")
    private String menuBarDesc;
    @Basic(optional = false)
    @Column(name = "Status")
    private String status;

    @Basic(optional = false)
    @Column(name = "Markers")
    private String markers;

和(UserType)

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;

2 个答案:

答案 0 :(得分:1)

除了@Popandopolos回答我认为在您的查询中使用join会对您有所帮助。

请访问Associations and joins上的hibernate文档以获取更多信息

如果您正在使用hibernate Query,那么我认为您的查询如下所示

select userTypeMenuBar.userMenuBar.menuBarId FROM UserTypeMenuBarComp as UserTypeMenuBarComp 
inner join UserTypeMenuBarComp.companyGroupMaster 
inner join UserTypeMenuBarComp.CompanyMaster
inner join UserTypeMenuBarComp.userTypeMenuBar.userType
where UserTypeMenuBarComp.companyGroupMaster.companyGroupId=" + 
    UsercompGid(LoginImpl.masterid) +
     " and UserTypeMenuBarComp.CompanyMaster.companyMasterId=" + 
    Usercompid(LoginImpl.masterid) +
     " and UserTypeMenuBarComp.userTypeMenuBar.userType.userTypeId=" + 
    LoginImpl.usertypid + ""

答案 1 :(得分:0)

根据HQL reference Java类和属性区分大小写

  

除了Java类和属性的名称之外,查询不区分大小写。所以SeLeCT与sELEct和SELECT相同,但org.hibernate.eg.FOO不是org.hibernate.eg.Foo,而foo.barSet不是foo.BARSET。

所以你需要在这里修复你的查询: UserTypeMenuBarComp.companyMaster.companyMasterId - companyMaster应以小写字母 c

开头