使用连接和类似运算符的Hibernate查询

时间:2013-12-19 13:35:48

标签: java hibernate

我有两个模型类:

第1类看起来像这样:

@Entity
@Table(name = "cdm_location_charge_class", catalog = "emscribedx")
public class Cdm_location_charge_class {
    private static Logger LOG = Logger.getLogger(Cdm_location_charge_class.class);
    private int indx;
    private String location;
    private String charge_Class;
    private List <Cdm_trans> cdm_Trans;
    @Id
    @GeneratedValue(strategy = GenerationType.AUTO)
    @Column(name = "indx")
    public int getIndx() {
        return indx;
    }
    public void setIndx(int indx) {
        this.indx = indx;
    }   
    @Column(name = "location")
    public String getLocation() {
        return location;
    }
    public void setLocation(String location) {
        this.location = location;
    }
    @Column(name = "charge_Class")
    public String getCharge_Class() {
        return charge_Class;
    }
    public void setCharge_Class(String charge_Class) {
        this.charge_Class = charge_Class;
    }
    @OneToMany (mappedBy = "cdm_location_charge_class", targetEntity = Cdm_trans.class, cascade = CascadeType.ALL, fetch = FetchType.EAGER)
    public List<Cdm_trans> getCdm_Trans() {
        return cdm_Trans;
    }
    public void setCdm_Trans(List<Cdm_trans> cdm_Trans) {
        this.cdm_Trans = cdm_Trans;
    }

第2课看起来像这样:

@Entity
@Table(name = "cdm_Trans", catalog = "emscribedx")
public class Cdm_trans {
private static Logger LOG = Logger.getLogger(Cdm_trans.class);

private int indx;
private String charge_Class;
private String charge_Code;
private String charge_Description;
private String charge_Eligibility;
private String exp_Date;
private BigDecimal rate_Charge;
private String cpt_Code;
private int rev_Code;
private String charge_Type;
private Cdm_location_charge_class cdm_location_charge_class;

@Id
@GeneratedValue(strategy = GenerationType.AUTO)
@Column(name = "indx")
public int getIndx() {
    return indx;
}

public void setIndx(int indx) {
    this.indx = indx;
}

@Column(name = "charge_Class")
public String getCharge_Class() {
    return charge_Class;
}

public void setCharge_Class(String charge_Class) {
    this.charge_Class = charge_Class;
}

@Column(name = "charge_Code")
public String getCharge_Code() {
    return charge_Code;
}

public void setCharge_Code(String charge_Code) {
    this.charge_Code = charge_Code;
}

@Column(name = "charge_Description")
public String getCharge_Description() {
    return charge_Description;
}

public void setCharge_Description(String charge_Description) {
    this.charge_Description = charge_Description;
}

@Column(name = "charge_Eligibility")
public String getCharge_Eligibility() {
    return charge_Eligibility;
}

public void setCharge_Eligibility(String charge_Eligibility) {
    this.charge_Eligibility = charge_Eligibility;
}

@Column(name = "exp_Date")
public String getExp_Date() {
    return exp_Date;
}

public void setExp_Date(String exp_Date) {
    this.exp_Date = exp_Date;
}

@Column(name = "rate_Charge")
public BigDecimal getRate_Charge() {
    return rate_Charge;
}

public void setRate_Charge(BigDecimal rate_Charge) {
    this.rate_Charge = rate_Charge;
}

@Column(name = "cpt_Code")
public String getCpt_Code() {
    return cpt_Code;
}

public void setCpt_Code(String cpt_Code) {
    this.cpt_Code = cpt_Code;
}

@Column(name = "rev_Code")
public int getRev_Code() {
    return rev_Code;
}

public void setRev_Code(int rev_Code) {
    this.rev_Code = rev_Code;
}

@Column(name = "charge_Type")
public String getCharge_Type() {
    return charge_Type;
}

public void setCharge_Type(String charge_Type) {
    this.charge_Type = charge_Type;
}

@ManyToOne
@JoinColumn(name = "charge_Class", insertable = false, updatable = false)
public Cdm_location_charge_class getCdm_location_charge_class() {
    return cdm_location_charge_class;
}

public void setCdm_location_charge_class(
        Cdm_location_charge_class cdm_location_charge_class) {
    this.cdm_location_charge_class = cdm_location_charge_class;
}
}

这两个类对应于两个基础表:

Cdm_trans和Cdm_location_charge_class。

我想创建一个Hibernate服务,它会给我与以下sql查询相同的结果:

select cdm_trans.* from Cdm_trans, Cdm_location_charge_class where 
cdm_location_charge_class.charge_Class = cdm_trans.charge_Class and 
cdm_location_charge_class.location = <Some location passed to the method> and
Cdm_trans.charge_Description like '%<Some search term passed into the method>%;

我的偏好是使用Hibernate标准API来做到这一点。但如果这是唯一的方法,我将使用HQL。有人能告诉我怎么做吗?

我已经尝试过这个HQL查询:

Query query = session.createQuery("FROM cdm_location_charge_class as cl  INNER JOIN   cl.cdm_Trans where cl.location = :location and  cdm_Trans.charge_Description like :search");

但我收到一个错误:   cdm_location_charge_class未映射

3 个答案:

答案 0 :(得分:2)

如果您想使用Criteria,只需添加一个限制。

 List cats = session.createCriteria(Cat.class)
     .createCriteria("kittens")
         .add( Restrictions.like("name", "Iz%") )
     .list();

HQL示例:

Query query = session.createQuery("FROM Cdm_location_charge_class cl " + 
   "INNER JOIN cl.cdm_Trans trans " +
   "where cl.location = :location and trans.charge_Description like :search")
   .setParameter("location", locationValue)
   .setParameter("search", "%" + searchValue + "%");

答案 1 :(得分:0)

您能更具体地了解这个问题,还是只是要求一个非常具体的教程?

无论如何为什么HQL是第二选择。恕我直言,Criteria Helper只应在构造动态查询时使用(例如,根据用户输入添加条件)。如果您有静态查询,我会建议HQL。它也应该更快。另请参阅NamedQuery。

答案 2 :(得分:0)

以下是Hibernate中Criteria查询的文档: http://docs.jboss.org/hibernate/orm/3.3/reference/en-US/html/querycriteria.html

具体来说,您需要查看Restrictions类(http://docs.jboss.org/hibernate/orm/3.3/api/org/hibernate/criterion/Restrictions.html)和静态方法(如()和ilike()),后者处理不区分大小写的比较。我也倾向于HQL用于静态查询,但我看到有时可以使静态元素可配置,特别是对于调试。因此,我可以将动态查询方法与存储在配置文件中的查询一起使用,因此即使应用程序通常运行相同的查询,我也可以轻松地进行测试以进行测试。

尊敬你的,

凯文