这是在Dao层获取List<PortfolioMemberView>, 我在该
public List<PortfolioMemberView> getPortfolioMemberViewByPid(Integer pid){
//查看portfolioId 的值
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();
//我用这个print方法来在console里面查看输出结果。
for(PortfolioMemberView pv: pmvl){
System.out.print(pv.getAssetCode());
}
return pmvl;
}catch(Exception e){
logger.info("操作失败:" + e.getMessage() + ", " +e.getCause());
throw new RuntimeException();
}
}
此时 我在数据库的 PortfolioMemberView视图 里有以下数据,
portfolioId assetId accountType assetCode assetLabel value_ annualReturn returnRate
10 1 S wOOWoo1S Canadian National 8699.75 1379.1885 0.15853196930946292
15 1 S wOOWoo1S Canadian National 782 123.97200000000001 0.15853196930946292
13 1 S wOOWoo1S Canadian National 0 0
11 1 S wOOWoo1S Canadian National 879.75 139.4685 0.15853196930946292
5 2 P CMPROP0121 Commercial Property 1.04 56960.05564 54769.28426923077
2 2 P CMPROP0121 Commercial Property 101.92 5582085.45272 54769.284269230775
2 4 P kckb karawokie 630 484097.4 768.4085714285715
2 5 S GYck ClickGym 4806 375580 78.14814814814815
10 5 S GYck ClickGym 4860 379800 78.14814814814815
13 5 S GYck ClickGym 486 37980 78.14814814814815
14 5 S GYck ClickGym 4860 379800 78.14814814814815
2 8 S min7b sd 7182 68731.74 9.57
2 9 D heyBo saveBow 9 171.76983230868902 19.085536923187668
触发该方法时 console给我的输出结果是这样的,
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
我不是很明白,portfolioId为2时, 对应的三条记录的asseCode属性应该时CMPROP0121,kckb,GYck,
但是结果缺连续输出了三个CMPROP0121。这个bug看不出是哪里造成,Hibernate自己在console里面打印生成出来的sql语句我去数据库那里跑了一下,输出结果正确。但是通过这个方法得出的list结果却不正确。
请大神帮一下忙。
你是不是把 portfolioId map 成 id 了。这是 hibernate 的缓存机制,它取第一个 row 会根据 id 缓存它,取第二个 row 时如果 id 一样 hibernate 会认为他们是一样的,会直接从缓存中取出输出。
如果portfolioId有重复的,不要 map 成 id。