Hibernate ResultTransformer返回具有所有空值的映射

时间:2018-07-03 10:44:56

标签: java spring hibernate

我正在尝试准备一张地图列表,其中每个地图代表一行表数据,这些表数据具有作为列名的键,而值是列值,即Map。我可以准备地图列表,但是除了 Id 列以外,所有其他列值都返回为 null 。你能帮忙吗?我的代码有什么问题。使用以下代码的会话工厂访问没有NullPointer例外

Criteria crt =  session.createCriteria(SampleCsv.class)
            .add( Restrictions.between("Id", 3, 6) )
    .setResultTransformer(new BasicTransformerAdapter() {
        @SuppressWarnings("deprecation")
        @Override
        public Object transformTuple(Object[] tuple, String[] aliases) {
            PassThroughResultTransformer rt = PassThroughResultTransformer.INSTANCE;
            final Object o = rt.transformTuple(tuple, aliases);

            ClassMetadata cm = getSessionFactory().getClassMetadata(o.getClass());
            List<String> pns = new ArrayList<String>(Arrays.asList(cm.getPropertyNames()));
            Map<String, Object> m = new HashMap<String, Object>();
            for(String pn : pns) {
              m.put(pn, cm.getPropertyValue(o, pn));
            }

            m.put(cm.getIdentifierPropertyName(), cm.getIdentifier(o));
            return m;
        }
    });

    List<Map<String,Object>> aliasToValueMap = (List<Map<String, Object>>)crt.list();

我的pojo课是

@Entity
@Table(name="SAMPLE_CSV")
public class SampleCsv {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
@Column(name="ID", nullable = false)
private int Id;
@Column(name = "Type", nullable = true)
private String Type;
@Column(name = "NomMedia")
private String NomMedia;
@Column(name = "sRep")
private String sRep;
@Column(name = "UrlMedia")
private String UrlMedia;
@Column(name = "Indice")
private String Indice;
@Column(name = "CodeFourSAP")
private String CodeFourSAP;
@Column(name = "NomDepositaire")
private String NomDepositaire;
@Column(name = "PrenomDepositaire")
private String PrenomDepositaire;
@Column(name = "EMailDepositaire")
private String EMailDepositaire;
@Column(name = "DateDepot")
private String DateDepot;
@Column(name = "PhotoPrincipale")
private String PhotoPrincipale;
@Column(name = "NomPhotographe")
private String NomPhotographe;
@Column(name = "PrenomPhotographe")
private String PrenomPhotographe;
@Column(name = "SocieteProprietaireDesDroits")
private String SocieteProprietaireDesDroits;
@Column(name = "DateCreationOuModificationMediaH")
private String DateCreationOuModificationMediaH;
@Column(name = "Commentaire")
private String Commentaire;
@Column(name = "WEB_Valide")
private String WEB_Valide;
@Column(name = "WEB_DateValidation")
private String WEB_DateValidation;
@Column(name = "WEB_ValidePar")
private String WEB_ValidePar;
@Column(name = "WEB_Classement")
private String WEB_Classement;
@Column(name = "CreditPhoto")
private String CreditPhoto;
@Column(name = "WEB_Commentaire")
private String WEB_Commentaire;
@Column(name = "NB_IMAGES")
private String NB_IMAGES;
@Column(name = "Watermark")
private String Watermark;
@Column(name = "Zoom")
private String Zoom;
@Column(name = "Traite")
private String Traite;
@Column(name = "TailleX")
private String TailleX;
@Column(name = "TailleY")
private String TailleY;
@Column(name = "MediaH_CodeArtSAP")
private String MediaH_CodeArtSAP;
@Column(name = "RefLoueur")
private String RefLoueur;
@Column(name = "Extension")
private String Extension;
@Column(name = "MediaP_CodeArtSAP")
private String MediaP_CodeArtSAP;
@Column(name = "DateDebutDroit")
private String DateDebutDroit;
@Column(name = "DateFinDroit")
private String DateFinDroit;
@Column(name = "DateCreationOuModificationCopyright")
private String DateCreationOuModificationCopyright;
public int getId() {
    return Id;
}
public void setId(int id) {
    this.Id = id;
}
public String getType() {
    return Type;
}
public void setType(String type) {
    Type = type;
}
public String getNomMedia() {
    return NomMedia;
}
public void setNomMedia(String nomMedia) {
    NomMedia = nomMedia;
}
public String getsRep() {
    return sRep;
}
public void setsRep(String sRep) {
    this.sRep = sRep;
}
public String getUrlMedia() {
    return UrlMedia;
}
public void setUrlMedia(String urlMedia) {
    UrlMedia = urlMedia;
}
public String getIndice() {
    return Indice;
}
public void setIndice(String indice) {
    Indice = indice;
}
public String getCodeFourSAP() {
    return CodeFourSAP;
}
public void setCodeFourSAP(String codeFourSAP) {
    CodeFourSAP = codeFourSAP;
}
public String getNomDepositaire() {
    return NomDepositaire;
}
public void setNomDepositaire(String nomDepositaire) {
    NomDepositaire = nomDepositaire;
}
public String getPrenomDepositaire() {
    return PrenomDepositaire;
}
public void setPrenomDepositaire(String prenomDepositaire) {
    PrenomDepositaire = prenomDepositaire;
}
public String getEMailDepositaire() {
    return EMailDepositaire;
}
public void setEMailDepositaire(String eMailDepositaire) {
    EMailDepositaire = eMailDepositaire;
}
public String getDateDepot() {
    return DateDepot;
}
public void setDateDepot(String dateDepot) {
    DateDepot = dateDepot;
}
public String getPhotoPrincipale() {
    return PhotoPrincipale;
}
public void setPhotoPrincipale(String photoPrincipale) {
    PhotoPrincipale = photoPrincipale;
}
public String getNomPhotographe() {
    return NomPhotographe;
}
public void setNomPhotographe(String nomPhotographe) {
    NomPhotographe = nomPhotographe;
}
public String getPrenomPhotographe() {
    return PrenomPhotographe;
}
public void setPrenomPhotographe(String prenomPhotographe) {
    PrenomPhotographe = prenomPhotographe;
}
public String getSocieteProprietaireDesDroits() {
    return SocieteProprietaireDesDroits;
}
public void setSocieteProprietaireDesDroits(String societeProprietaireDesDroits) {
    SocieteProprietaireDesDroits = societeProprietaireDesDroits;
}
public String getDateCreationOuModificationMediaH() {
    return DateCreationOuModificationMediaH;
}
public void setDateCreationOuModificationMediaH(
        String dateCreationOuModificationMediaH) {
    DateCreationOuModificationMediaH = dateCreationOuModificationMediaH;
}
public String getCommentaire() {
    return Commentaire;
}
public void setCommentaire(String commentaire) {
    Commentaire = commentaire;
}
public String getWEB_Valide() {
    return WEB_Valide;
}
public void setWEB_Valide(String wEB_Valide) {
    WEB_Valide = wEB_Valide;
}
public String getWEB_DateValidation() {
    return WEB_DateValidation;
}
public void setWEB_DateValidation(String wEB_DateValidation) {
    WEB_DateValidation = wEB_DateValidation;
}
public String getWEB_ValidePar() {
    return WEB_ValidePar;
}
public void setWEB_ValidePar(String wEB_ValidePar) {
    WEB_ValidePar = wEB_ValidePar;
}
public String getWEB_Classement() {
    return WEB_Classement;
}
public void setWEB_Classement(String wEB_Classement) {
    WEB_Classement = wEB_Classement;
}
public String getCreditPhoto() {
    return CreditPhoto;
}
public void setCreditPhoto(String creditPhoto) {
    CreditPhoto = creditPhoto;
}
public String getWEB_Commentaire() {
    return WEB_Commentaire;
}
public void setWEB_Commentaire(String wEB_Commentaire) {
    WEB_Commentaire = wEB_Commentaire;
}
public String getNB_IMAGES() {
    return NB_IMAGES;
}
public void setNB_IMAGES(String nB_IMAGES) {
    NB_IMAGES = nB_IMAGES;
}
public String getWatermark() {
    return Watermark;
}
public void setWatermark(String watermark) {
    Watermark = watermark;
}
public String getZoom() {
    return Zoom;
}
public void setZoom(String zoom) {
    Zoom = zoom;
}
public String getTraite() {
    return Traite;
}
public void setTraite(String traite) {
    Traite = traite;
}
public String getTailleX() {
    return TailleX;
}
public void setTailleX(String tailleX) {
    TailleX = tailleX;
}
public String getTailleY() {
    return TailleY;
}
public void setTailleY(String tailleY) {
    TailleY = tailleY;
}
public String getMediaH_CodeArtSAP() {
    return MediaH_CodeArtSAP;
}
public void setMediaH_CodeArtSAP(String mediaH_CodeArtSAP) {
    MediaH_CodeArtSAP = mediaH_CodeArtSAP;
}
public String getRefLoueur() {
    return RefLoueur;
}
public void setRefLoueur(String refLoueur) {
    RefLoueur = refLoueur;
}
public String getExtension() {
    return Extension;
}
public void setExtension(String extension) {
    Extension = extension;
}
public String getMediaP_CodeArtSAP() {
    return MediaP_CodeArtSAP;
}
public void setMediaP_CodeArtSAP(String mediaP_CodeArtSAP) {
    MediaP_CodeArtSAP = mediaP_CodeArtSAP;
}
public String getDateDebutDroit() {
    return DateDebutDroit;
}
public void setDateDebutDroit(String dateDebutDroit) {
    DateDebutDroit = dateDebutDroit;
}
public String getDateFinDroit() {
    return DateFinDroit;
}
public void setDateFinDroit(String dateFinDroit) {
    DateFinDroit = dateFinDroit;
}
public String getDateCreationOuModificationCopyright() {
    return DateCreationOuModificationCopyright;
}
public void setDateCreationOuModificationCopyright(
        String dateCreationOuModificationCopyright) {
    DateCreationOuModificationCopyright = dateCreationOuModificationCopyright;
}
}

我要发布最终列表的结果,其中包含两个地图,如下所示

[{Id=3,PrenomDepositaire=null, RefLoueur=null, DateCreationOuModificationCopyright=null, EMailDepositaire=null, SocieteProprietaireDesDroits=null, NomMedia=null, TailleX=null, CodeFourSAP=null, PrenomPhotographe=null, Commentaire=null, MediaP_CodeArtSAP=null, NomDepositaire=null, WEB_Commentaire=null, UrlMedia=null, Zoom=null, TailleY=null, WEB_Valide=null, DateDepot=null, sRep=null, WEB_ValidePar=null, Indice=null, CreditPhoto=null, DateCreationOuModificationMediaH=null, MediaH_CodeArtSAP=null, DateDebutDroit=null, WEB_Classement=null, DateFinDroit=null, NB_IMAGES=null, NomPhotographe=null, PhotoPrincipale=null, Extension=null, Type=null, WEB_DateValidation=null, Traite=null, Watermark=null}, 
{ Id=4,PrenomDepositaire=null, RefLoueur=null, DateCreationOuModificationCopyright=null, EMailDepositaire=null, SocieteProprietaireDesDroits=null, NomMedia=null, TailleX=null, CodeFourSAP=null, PrenomPhotographe=null, Commentaire=null, MediaP_CodeArtSAP=null, NomDepositaire=null, WEB_Commentaire=null, UrlMedia=null, Zoom=null, TailleY=null, WEB_Valide=null, DateDepot=null, sRep=null, WEB_ValidePar=null, Indice=null, CreditPhoto=null, DateCreationOuModificationMediaH=null, MediaH_CodeArtSAP=null, DateDebutDroit=null, WEB_Classement=null, DateFinDroit=null, NB_IMAGES=null, NomPhotographe=null, PhotoPrincipale=null, Extension=null, Type=null, WEB_DateValidation=null, Traite=null, Watermark=null}
]

但是表数据也具有非null数据。

enter image description here

0 个答案:

没有答案