简体   繁体   English

创建一对多关系的hql查询

[英]creating hql query for one-to-many relationship

I have two class Domain and UserProfile . 我有两个类Domain和UserProfile。 The relation between them is one to many. 它们之间的关系是一对多的。 It is defined in UserProfile Class.I have firstName of user and domain object I have to write hql query to retrive the UserProfile object. 它在UserProfile类中定义。我具有用户名和域对象的firstName,我必须编写hql查询来检索UserProfile对象。 I have created like this "from UserProfile as profile where profile.domain='"+domain+"' and profile.firstName ='"+firstName+"' ". 我已经创建了这样的“从UserProfile作为配置文件,其中profile.domain ='“ + domain +”'和profile.firstName ='“ + firstName +”'“。 But it is not working. 但这是行不通的。

can anyone tell me how should be the query. 谁能告诉我该查询应该如何。

@Entity
@Table(name="TBL_STD_DOMAIN")
public class Domain implements Serializable {

  @Id
  @GeneratedValue
  @Column(name ="FLD_DOMAIN_ID")
  private Long domainId;

  @Column(name = "FLD_DOMAIN_NAME")
  private String domainName;
}


@Entity
@Table(name="TBL_STD_USERPROFILE")
public class UserProfile {

  @Id
  @GeneratedValue
  @Column(name = "FLD_USER_ID")
  private Long userId;

  @Column(name = "FLD_First_Name")
  private FirstName firstName;

  @ManyToOne
  private Domain domain;
}

You should never use String concatenation to pass parameters to queries. 您永远不要使用String串联将参数传递给查询。 Use query parameters instead (named, preferrably): 改用查询参数(最好命名):

String hql = 
    "select u from UserProfile u"
    + " where u.firstName = :theFirstName"
    + " and u.domain = :theDomain";

return session.createQuery(hql)
              .setParameter("theFirstName", firstName)
              .setParameter("theDomain", domain)
              .list();

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM