使用Projecions从子表中获取特定的列 [英] Using Projecions to fetch a particular column from child table

查看:114
本文介绍了使用Projecions从子表中获取特定的列的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有两张表
$ b $ pre $ code贷款(id,金额,持续时间)

LoanStatus(id ,status,loan_id)//只是一个例子,但它在这个表中有很多字段

贷款.java

  public class Loan {

private int id;
private int amount;
private int duration;
私人LoanStatus loanStatus;

// getter and setters

}

LoanStatus.java

  public class LoanStatus {//只是一个例子,但它有很多字段,而不是实际给出的

private int id;
私人字符串状态;
私人贷款贷款;

// getter and setters

}

现在我只想得到金额持续时间 loanStatus.status 使用投影。我已经使用 createAlias()来成功获取特定列,但是将其设置为setter时会出现问题。

  Criteria criteria = getSession()。createCriteria(Loan.class,loan); 
criteria.createAlias(loan.loanStatus,loanStatus)
.setProjection(Projections.projectionList()。add(Projections.property(id),id)
.add(Projections.property(amount),amount)
.add(Projections.property(duration),duration)
.add(Projections.property(loanStatus。状态),loanStatus))
.add(Restrictions.eq(id,id))
.setResultTransformer(Transformers.aliasToBean(Loan.class));
return criteria.list();

我有一个错误,如下所示。

 在为set [com.site.dto.Loan.loanStatus(expected type = com.site.dto.LoanStatus)]调用setter时发生IllegalArgumentException; target = [com.site.dto.Loan@4083974a],property value = [Pending] 

我收到了预期的列值Pending,但问题在于将其设置为setter时。我看到很多预测问题,但其中大多数是基于使用投影的限制,但不是使用投影来获取孩子的特定列。

>

编写您自己的定制变压器。以下实现可能正是您需要的功能



https:/ /github.com/samiandoni/AliasToBeanNestedResultTransformer 。使用示例写在他们的文档中

  class Person {
private Long id;
私人字符串名称;
私家车车;
// getters and setters
}

class Car {
private Long id;
私有字符串颜色;
//获得者和设置者
}

列表< Person> getPeople(){
ProjectionList预测= Projections.projectionList()
.add(Projections.id()。as(id))
.add(Projections.property(name ).as(name))
.add(Projections.property(c.id)。as(car.id))
.add(Projections.property(c。色 ),为( car.color));

Criteria criteria = getCurrentSession()。createCriteria(Person.class)
.createAlias(car,c)
.setProjection(预测)
。 setResultTransformer(新的AliasToBeanNestedResultTransformer(Person.class));

return(List< Person>)criteria.list();
}

//每个人的车将被填充


I have two tables

Loan (id, amount, duration)

LoanStatus(id, status, loan_id)   // just an example, but it has lot more fields in this table

Loan.java

public class Loan{

    private int id;
    private int amount;
    private int duration;
    private LoanStatus loanStatus;

    // getter and setters

}

LoanStatus.java

public class LoanStatus{   // just an example, but it has many fields than what actually given

    private int id;
    private String status;
    private Loan loan;

    //getter and setters

}

Now I would like to get only amount , duration , loanStatus.status using Projections. I've used createAlias() to successfully fetch that particular column, but the problem occurs when setting it to a setter.

Criteria criteria = getSession().createCriteria(Loan.class,"loan");
    criteria.createAlias("loan.loanStatus", "loanStatus")
            .setProjection(Projections.projectionList().add(Projections.property("id"),"id")
            .add(Projections.property("amount"),"amount")
            .add(Projections.property("duration"),"duration")
            .add(Projections.property("loanStatus.status"), "loanStatus"))
            .add(Restrictions.eq("id", id))
            .setResultTransformer(Transformers.aliasToBean(Loan.class));
    return criteria.list();

I've an error which as follows.

IllegalArgumentException occurred while calling setter for property [com.site.dto.Loan.loanStatus (expected type = com.site.dto.LoanStatus)]; target = [com.site.dto.Loan@4083974a], property value = [Pending]

So I'm getting my expected column value "Pending", but the problem is when setting it to a setter. I've seen many question for projections, but most of them was based on Restrictions using Projections but not fetching a child's particular column using projections.

解决方案

Write your own custom transformer. The following implementation might be exactly what you need

https://github.com/samiandoni/AliasToBeanNestedResultTransformer . The usage example as written in their docs

class Person {
  private Long id;
  private String name;
  private Car car;
  // getters and setters
}

class Car {
  private Long id;
  private String color;
  // getters and setters
}

List<Person> getPeople() {
  ProjectionList projections = Projections.projectionList()
    .add(Projections.id().as("id"))
    .add(Projections.property("name").as("name"))
    .add(Projections.property("c.id").as("car.id"))
    .add(Projections.property("c.color").as("car.color"));

  Criteria criteria = getCurrentSession().createCriteria(Person.class)
    .createAlias("car", "c")
    .setProjection(projections)
    .setResultTransformer(new AliasToBeanNestedResultTransformer(Person.class));

  return (List<Person>) criteria.list();
}

// each car of Person will be populated

这篇关于使用Projecions从子表中获取特定的列的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆