Java 代码部分我不明白
我不明白为什么在这段代码中,它是这样做的:Java 代码部分我不明白,java,hibernate,postgresql,Java,Hibernate,Postgresql,我不明白为什么在这段代码中,它是这样做的: query.setString("idFamilleDeProduit", String.valueOf(familleDeProduits.getFamilleDeProduitsGenerique().getId())); 当我在数据库中查看表时,id列是integer 它是PostgreSql-9.4.1207 我的功能: public List recherchePourDuplication(字符串类型Accord、FamilledProd
query.setString("idFamilleDeProduit", String.valueOf(familleDeProduits.getFamilleDeProduitsGenerique().getId()));
当我在数据库中查看表时,id
列是integer
它是PostgreSql-9.4.1207
我的功能:
public List recherchePourDuplication(字符串类型Accord、FamilledProduits门类FamilledProduits、SocieteInterne SocieteInterne、SocieteExterne SocieteExterne、字符串AnneAccord)引发PersistenceException{
List listContratMdd=新建ArrayList();
字符串requete=“”;
如果(!“”.equals(AnneAccord)){
requete+=“退火记录=:退火记录”;
}
如果(!“”.equals(typeAccord)和(!“”.equals(requete)){
重新计算+=“和”;
}
如果(!“”.equals(typeAccord)){
重新报价+=“类型=:类型”;
}
布尔existfamile=false;
requete+=(familleDeProduits!=null&&familleDeProduits.getFamilleDeProduitsGenerique()!=null)?“和”:”;
if(familleDeProduits!=null&&familleDeProduits.getFamilleDeProduitsGenerique()!=null){
ExistFamile=true;
requete+=“EstappliceSur.familleDeProduitsGenerique不为NULL,并且EstappliceSur.familleDeProduitsGenerique.id=:idFamilleDeProduit”;
}
布尔existSocieteInterne=false;
布尔existSocieteExterne=false;
requete+=(SocietInterne!=null)?“和”:”;
如果(societeInterne!=null){
existSocieteInterne=true;
String table=SocietInterne实例主控?“MasterImpl”:“粘附mpl”;
requete+=“contractantInterne.id=:idSocietInterne和contractantInterne IN(来自“+表+”);
}
requete+=(societeExterne!=null)?“和”:”;
if(societeExterne!=null){
existSocieteExterne=true;
String table=GroupeIndustriel的SocietExterne实例?“GroupeIndustrieImpl”:“FourniseUrImpl”;
requete+=“contractantExterne.id=:idsocieteExterne和contractantExterne IN(来自“+表+”);
}
如果(!“”.equals(requete)){
requete=“来自合同模板,其中”+requete;
查询=创建查询(重新查询);
如果(!“”.equals(AnneAccord)){
query.setbiginger(“anneeAccord”,新的BigInteger(anneeAccord));
}
如果(!“”.equals(typeAccord)){
query.setString(“type”,typeAccord);
}
如果(存在家族){
query.setString(“idFamilleDeProduit”,String.valueOf(familleDeProduits.getFamilleDeProduitsGenerique().getId());
}
if(existSocieteInterne){
setInteger(“idSocietInterne”,societInterne.getId());
}
if(existSocieteExterne){
setInteger(“idsocieteExterne”,societeExterne.getId());
}
listcontrad.addAll((List)query.List());
}
返回listContratMdd;
}
之所以发生这种情况,是因为Postgre的DB驱动程序允许。但是对于整数
,您应该使用setInt()
而不是setString()
,因为其他DB驱动程序可能不支持它
以下是java.sql.PreparedStatement
文档必须说明的内容:
注:用于设置的setter方法(setShort、setString等)
IN参数值必须指定与兼容的类型
已定义输入参数的SQL类型。例如,如果
参数具有SQL类型整数,则应使用方法setInt
这是因为Postgre的DB驱动程序允许它。但是对于
整数
,您应该使用setInt()
而不是setString()
,因为其他DB驱动程序可能不支持它
以下是java.sql.PreparedStatement
文档必须说明的内容:
注:用于设置的setter方法(setShort、setString等)
IN参数值必须指定与兼容的类型
已定义输入参数的SQL类型。例如,如果
参数具有SQL类型整数,则应使用方法setInt
我认为最新版本的postgresql-9.4.1207 jdbc驱动程序不再支持它。对于我的代码,我有以下错误:
无法提取结果集
错误:运算符不存在:整数=字符变化我认为最新版本的postgresql-9.4.1207 jdbc驱动程序不再支持它。对于我的代码,我有以下错误:无法提取结果集
错误:运算符不存在:整数=字符变化
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;
}