• 开发过程遇到的问题


    1.hibernate 原生sql查询问题:

     1 public void getNurseStatInfo() {
     2         String sql = "select 'col018' id,count(*) count  from PHC_RECORD_CARD t where t.phc_model_def_id='932a9b81000080810af4' "
     3                 + "union select 'col024' id,count(*) count from PHC_RECORD_CARD t where t.phc_model_def_id='7ca18efa000b0f7d44dc' "
     4                 + "union select 'col032' id,count(*) count from PHC_RECORD_CARD t where t.phc_model_def_id='27f913b6000080e515e0' "
     5                 + "union select 'col056' id,count(*) count from PHC_RECORD_CARD t where t.phc_model_def_id='243c447a00000f7d6958' "
     6                 + "union select 'col062' id,count(*) count from PHC_RECORD_CARD t where t.phc_model_def_id='f786355d001c6a81ea34' "
     7                 + "union select 'col027' id,count(*) count from PHC_RECORD_CARD t where t.phc_model_def_id='3f03ade200020f7d4b1c' "
     8                 + "union select 'col059' id,count(*) count from PHC_RECORD_CARD t where t.phc_model_def_id='0305c2e600d57f1509a0' ";
     9         List<NurseStatInfo> statList = phcRecordCardDao.getEntityManager().createNativeQuery(sql, NurseStatInfo.class).getResultList();
    10         System.out.println("-----------------------------");
    11         for (NurseStatInfo info : statList) {
    12             System.out.println(info.getId());
    13             System.out.println(info.getCount());
    14         }
    15     }
    View Code
     1 import javax.persistence.Entity;
     2 import javax.persistence.Id;
     3 
     4 @Entity
     5 public class NurseStatInfo {
     6     @Id
     7     private String id;
     8     private int count;
     9     
    10     public String getId() {
    11         return id;
    12     }
    13     public void setId(String id) {
    14         this.id = id;
    15     }
    16     public int getCount() {
    17         return count;
    18     }
    19     public void setCount(int count) {
    20         this.count = count;
    21     }
    22     
    23 }
    View Code

    NurseStatInfo类中需要加@Entity、@Id两个注解,不然会报以下错误:

    Caused by: org.hibernate.MappingException: Unknown entity

    参考网站:http://stackoverflow.com/questions/30595445/how-to-map-a-native-query-to-a-pojo-when-i-do-not-have-any-entity-on-my-project

     
  • 相关阅读:
    Hibernate之lazy延迟加载(转)
    Hibernate中的一级缓存、二级缓存和懒加载(转)
    Hibernate框架之关联映射入门
    Hibernate框架之入门
    S​Q​L​获​取​当​前​时​间​(​日​期​)
    网页选项卡的应用
    动态获取设置提示框和小箭头的位置
    Jquery实现下拉联动表单
    jquery自己手写表单验证
    鼠标移动到图片上时,显示大图片
  • 原文地址:https://www.cnblogs.com/Aaronqcd/p/5856450.html
Copyright © 2020-2023  润新知