休眠查询给出不正确的结果列表

时间:2018-11-25 11:12:26

标签: java mysql database hibernate hql

这是方法道层列表,

public List<PortfolioMemberView> getPortfolioMemberViewByPid(Integer pid){
    //check the portfolioId value in console
    System.out.print(pid);
    try {
        Session session = sessionFactory.getCurrentSession();
        String sql = "from PortfolioMemberView pv where pv.portfolioId = ?0";
        Query query = session.createQuery(sql).setParameter(0, pid);
        List<PortfolioMemberView> pmvl = query.list();
        //check the result list by assetCode in console
        for(PortfolioMemberView pv: pmvl){
            System.out.print(pv.getAssetCode());
        }
        return pmvl;
    }catch(Exception e){
        logger.info("操作失败:"  + e.getMessage() + ", " +e.getCause());
        throw new RuntimeException();
    }

我的数据库中有一个View(我创建的)PortfolioMemberView 并且它具有以下数据

inmage

当我调用该方法时,控制台将打印以下结果。

2
Hibernate: select portfoliom0_.portfolioId as portfoli1_8_, 
portfoliom0_.assetId as assetId2_8_, portfoliom0_.accountType as accountT3_8_, portfoliom0_.assetCode as assetCod4_8_, portfoliom0_.assetLabel as assetLab5_8_, portfoliom0_.value_ as value_6_8_, portfoliom0_.annualReturn as annualRe7_8_, portfoliom0_.returnRate as returnRa8_8_ from PortfolioMemberView portfoliom0_ where portfoliom0_.portfolioId=?
CMPROP0121CMPROP0121CMPROP0121CMPROP0121CMPROP0121

预期结果应为CMPROP0121kckbGYck,因为当PortfolioId = 2时,结果列表中三个对象的资产代码应为CMPROP0121,kckb,GYck。

我还复制生成的查询hibenate并在mysql数据库中运行它,结果是正确的。

    Hibernate: select portfoliom0_.portfolioId as portfoli1_8_, 
portfoliom0_.assetId as assetId2_8_, portfoliom0_.accountType as accountT3_8_, portfoliom0_.assetCode as assetCod4_8_, portfoliom0_.assetLabel as assetLab5_8_, portfoliom0_.value_ as value_6_8_, portfoliom0_.annualReturn as annualRe7_8_, portfoliom0_.returnRate as returnRa8_8_ from PortfolioMemberView portfoliom0_ where portfoliom0_.portfolioId=?

不确定怎么了,请帮忙!!!

1 个答案:

答案 0 :(得分:0)

我将PortfolioId映射为主要ID,并且主要ID无法重复。但显然主键PortfolioId是重复的。我做了另一个主键PortfolioMemberId,问题就解决了