如何在java中使用hql查询获取包含List的对象列表?

时间:2016-12-11 16:45:23

标签: java hibernate hql

我遇到了问题。我有两个表t1和t2。并且它们与外键连接(更确切地说,t1具有字段id,而t2表具有id_t1_fkey字段)。 t1中的一个记录可以在t2中具有一个或多个记录。所以我想使用dto构造函数将所有内容放在对象列表中。结果我希望得到这样的结果: 列表中的对象编号为i: Object_i:field1_t1,field2_t2,fieldn_t1,List [field1_t2]                 Ivanov Ivan [order1,order2,order3]

现在hql只返回这样的对象:

Ivanov Ivan order1 Ivanov Ivan order2 Ivanov Ivan order3

所以你可以看到三个无用的副本Ivanov Ivan(((我不想发送前端很多副本) 我试着写这个:

t1是表SettingsFee,它连接了@OneToMany和t2  t2是" table" s.salesPartner,

// t1模型

@Entity
@Table (name="t1")
@Audited
public class SettingsFee {

    public static enum Status {
        ACTIVE,
        CLOSED,
        DELETE
    }

    @Id
    @Column(name="id")
    private String id;
    @Column(name="name")
    private String name;
    @Column(name="lastname")
    private String lastname;
    @OneToMany(mappedBy = "settingsFee")
    private List<SettingsFeeSalesPartner> salesPartner; 
    //there getters and setters
}

// t2模型

@Entity
@Table(name = "t2")
@Audited
public class SettingsFeeSalesPartner {

    @Id
    @Column(name = "id")
    private String id;

    @JsonIgnore
    @ManyToOne(fetch = FetchType.LAZY)
    @JoinColumn(name = "settingsfee")
    private SettingsFee settingsFee;

    @JsonIgnore
    @ManyToOne(fetch = FetchType.LAZY)
    @JoinColumn(name = "salespartner")
    private Company salesPartner;//Company is another model for another table
//there getters and setters
}   

@Repository
public class SettingsFeeCustomRepository {

    @PersistenceContext
    private EntityManager em;

    public List<SettingsFeeDTO> loadData() {

//我尝试使用过滤器选择满足查询的id, //结果数量等,但为了简单起见,它们被删除了。

    String hql = "SELECT  DISTINCT(s.id) FROM SettingsFee s INNER JOIN s.salesPartner psp WHERE s.status != 'DELETE' ";

    Query PreQuery = em.createQuery(hql)    

//我尝试将结果列表包含在必要的字段中:

String hql1 = "SELECT sf.id, sf.name sf.lastname, sfp.salesPartner.id,, sfp.salesPartner.name  "
                    + "  FROM SettingsFee sf "
                    + "JOIN sf.salesPartner sfp "
                    + "WHERE sf.id IN ( :PreQuery ) "
                    + "ORDER BY sf.id";
            Query query = em.createQuery(hql1);
            List<String> preresult = PreQuery.getResultList();
            if (preresult.isEmpty()) {
                query.setParameter("PreQuery", "_");
            } else {
                query.setParameter("PreQuery", preresult);
            }

//我在那里创建了一个列表

    List<SettingsFeeLiteHQLDTO>list= ((List<Object[]>) query.getResultList()).stream()
            .map(row -> new SettingsFeeLiteHQLDTO((String) row[0], (String) row[1], (String) row[2], (String) row[3]))
            .collect(Collectors.toList());

//我将我的列表打包到&#34;列表中的列表&#34;: //如果id相等,它将在列表中替换。

List<SettingsFeeDTO> FinList= new ArrayList<SettingsFeeDTO>();
            List<String> spName=new ArrayList<String>();

            int NumList=list.size();
            for (int i=0;i<NumList;i++){
                spName.add(list.get(i).getClientName() );
                spId.add(list.get(i).getClientId());

//等于被覆盖,如果两个对象的ID相同,则两个对象相等

if ((i!=NumList-1)&&list.get(i).equals(list.get(i+1))){
                    //do nothing
                }else if((i!=NumList-1)&&(list.get(i).equals(list.get(i+1))==false)){   //if two nabes are different
//there is special constructor          
                    FinList.add(new SettingsFeeDTO(list.get(i),spId,spName));               
                    spName=new ArrayList<String>();
                    spId=new ArrayList<String>();
                }
                else{//for last element
                    FinList.add(new SettingsFeeDTO(list.get(i),spId,spName));
                }
            }
            return FinList;
        }

如何克服???是否有任何变体可以最小化代码量? 附:我使用Eclipse,spring框架,数据:pgAdmin(postgre)

0 个答案:

没有答案