繁体   English   中英

我不明白的代码部分

[英]code part I do not understand

我不明白为什么在这段代码中这样做:

query.setString("idFamilleDeProduit", String.valueOf(familleDeProduits.getFamilleDeProduitsGenerique().getId()));

当我在数据库中查看表时, id列是integer

这是PostgreSql-9.4.1207

我的功能:

public List<ContratMdd> recherchePourDuplication(String typeAccord, FamilleDeProduitsNomenclature familleDeProduits, SocieteInterne societeInterne, SocieteExterne societeExterne, String anneeAccord) throws PersistenceException {

    List<ContratMdd> listContratMdd = new ArrayList<ContratMdd>();

    String requete = "";

    if (!"".equals(anneeAccord)){
        requete += " anneeAccord = :anneeAccord";
    }
    if (!"".equals(typeAccord) && ! "".equals(requete)){
        requete += " AND";
    }
    if (!"".equals(typeAccord)){
        requete += " type = :type";
    }

    boolean existFamille = false;

    requete += (familleDeProduits != null && familleDeProduits.getFamilleDeProduitsGenerique() != null) ? " AND " : "";

    if(familleDeProduits != null && familleDeProduits.getFamilleDeProduitsGenerique() != null){
        existFamille = true;
        requete += " estAppliqueSur.familleDeProduitsGenerique IS NOT NULL AND  estAppliqueSur.familleDeProduitsGenerique.id = :idFamilleDeProduit";
    }


    boolean existSocieteInterne = false;
    boolean existSocieteExterne = false;

    requete += (societeInterne != null) ? " AND " : "";

    if(societeInterne != null){

        existSocieteInterne = true;
        String table = societeInterne instanceof Master ? "MasterImpl" : "AdherentImpl";
        requete += " contractantInterne.id = :idsocieteInterne AND contractantInterne IN (FROM "+table+") ";
    }

    requete += (societeExterne != null) ? " AND " : "";

    if(societeExterne!=null){

        existSocieteExterne = true;
        String table = societeExterne instanceof GroupeIndustriel ? "GroupeIndustrielImpl" : "FournisseurImpl";
        requete += " contractantExterne.id = :idsocieteExterne AND contractantExterne IN (FROM "+table+") ";

    }


    if (!"".equals(requete)) {

        requete = "from ContratMddImpl where" + requete;
        Query query = createQuery(requete);

        if (!"".equals(anneeAccord)){
            query.setBigInteger("anneeAccord", new BigInteger(anneeAccord));
        }
        if (!"".equals(typeAccord)){
            query.setString("type", typeAccord);
        }
        if(existFamille){
            query.setString("idFamilleDeProduit", String.valueOf(familleDeProduits.getFamilleDeProduitsGenerique().getId()));
        }
        if (existSocieteInterne){
            query.setInteger("idsocieteInterne", societeInterne.getId());
        }
        if (existSocieteExterne){
            query.setInteger("idsocieteExterne", societeExterne.getId());
        }

        listContratMdd.addAll((List<ContratMdd>) query.list());     
    }

    return listContratMdd;
}

之所以发生这种情况,是因为Postgre的数据库驱动程序允许它。 但是您应该对一个Integer使用setInt()而不是setString() ,因为其他数据库驱动程序可能不支持它。

这是java.sql.PreparedStatement文档必须说的内容:

注意:用于设置IN参数值的setter方法(setShort,setString等)必须指定与输入参数的已定义SQL类型兼容的类型。 例如,如果IN参数的SQL类型为INTEGER,则应使用setInt方法。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM