Chirag Kamat
Chirag Kamat

Reputation: 109

How to write HQL Query for multiple entities

I am trying to write a HQL query for getting an id of a record. but the query is giving me this error

org.hibernate.QueryException: could not resolve property: CompanyMaster of: EntityPack.UserTypeMenuBarComp [select userTypeMenuBar.userMenuBar.menuBarId FROM EntityPack.UserTypeMenuBarComp as UserTypeMenuBarComp where UserTypeMenuBarComp.companyGroupMaster.companyGroupId=4 and UserTypeMenuBarComp.CompanyMaster.companyMasterId=2 and UserTypeMenuBarComp.userTypeMenuBar.userType.userTypeId=2]

here is my query :

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 + ""

Here are the entity classes (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;

and (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;

Upvotes: 1

Views: 961

Answers (2)

Dev
Dev

Reputation: 2325

in addition to @Popandopolos answer I think use of join in your query will help you.

please visit hibernate documentation on Associations and joins for more information

if you are using hibernate Query then I think your Query look like below

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 + ""

Upvotes: 1

Popandopolos
Popandopolos

Reputation: 630

According to HQL reference Java classes and properties are case sensitive

With the exception of names of Java classes and properties, queries are case-insensitive. So SeLeCT is the same as sELEct is the same as SELECT, but org.hibernate.eg.FOO is not org.hibernate.eg.Foo, and foo.barSet is not foo.BARSET.

so you nedd to fix your query here: UserTypeMenuBarComp.companyMaster.companyMasterId - companyMaster should be started with small letter c

Upvotes: 0

Related Questions