- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我有以下代码:
String searchText = "...";
String sqlQuery =
"FROM Studio s " +
"WHERE fts('english', s.companyName, :q) = true";
Query q = JPA.em()
.createQuery(sqlQuery)
.setParameter("q", searchText);
当我将一个单词传递给 searchText
时,它起作用了:
String searchText = "one";
当我传递两个词时,比如
String searchText = "one two";
我明白了
[PersistenceException: org.hibernate.exception.SQLGrammarException: could not execute query]
然而,当我传递一个带引号的字符串时,它再次起作用:
String searchText = "'one two'";
难道 SetParameter
不应该设置正确的引号和转义文本吗?
附言fts
是一个 PostgreSQL 方言函数,定义如下:
public class MyPostgreSQLDialect extends PostgreSQLDialect {
public MyPostgreSQLDialect() {
registerFunction("fts", new PostgreSQLFullTextSearchFunction());
}
}
...
public class PostgreSQLFullTextSearchFunction implements SQLFunction {
public String render(@SuppressWarnings("rawtypes") List args, SessionFactoryImplementor factory) {
if (args.size() != 3) {
throw new IllegalArgumentException(
"The function must be passed 3 arguments");
}
String ftsConfig = (String) args.get(0);
String field = (String) args.get(1);
String value = (String) args.get(2);
String fragment = null;
if (ftsConfig == null) {
fragment = "to_tsvector(" + field + ") @@ " + "to_tsquery('"
+ value + "')";
} else {
fragment = "to_tsvector(" + ftsConfig + "::regconfig, " + field + ") @@ "
+ "to_tsquery(" + ftsConfig + ", " + value + ")";
}
return fragment;
}
@Override
public Type getReturnType(Type columnType, Mapping mapping)
throws QueryException {
return new BooleanType();
}
@Override
public boolean hasArguments() {
return true;
}
@Override
public boolean hasParenthesesIfNoArguments() {
return false;
}
@SuppressWarnings("rawtypes")
@Override
public String render(Type arg0, List arg1, SessionFactoryImplementor arg2)
throws QueryException {
return render(arg1, arg2);
}
}
它将查询转换为:
to_tsvector('english'::regconfig, studio0_.companyName) @@ to_tsquery('english', ?)
附言
PostreSQL 日志:
2014-02-11 15:10:39 CET ERROR: syntax error in tsquery: "one two"
2014-02-11 15:10:39 CET STATEMENT: select studio0_.uuid as uuid164_, studio0_.addressScore as addressS2_164_, studio0_.adwordsAktivity as adwordsA3_164_, studio0_.affDiversityScore as affDiver4_164_, studio0_.allSources as allSources164_, studio0_.backgroundImg as backgrou6_164_, studio0_.city as city164_, studio0_.clusterDiversityScore as clusterD8_164_, studio0_.companyName as companyN9_164_, studio0_.companyNameCount as company10_164_, studio0_.companyType as company11_164_, studio0_.completenessScore as complet12_164_, studio0_.contentTags as content13_164_, studio0_.crefoId as crefoId164_, studio0_.decisionMaker1 as decisio15_164_, studio0_.decisionMaker2 as decisio16_164_, studio0_.description as descrip17_164_, studio0_.email as email164_, studio0_.email2 as email19_164_, studio0_.emailCount as emailCount164_, studio0_.fax1 as fax21_164_, studio0_.fax2 as fax22_164_, studio0_.faxImpressum as faxImpr23_164_, studio0_.fbProfileUrl as fbProfi24_164_, studio0_.formOfAddress as formOfA25_164_, studio0_.hasGeoCode as hasGeoCode164_, studio0_.house as house164_, studio0_.imprintDataExtracted as imprint28_164_, studio0_.industry1 as industry29_164_, studio0_.industry2 as industry30_164_, studio0_.internetIq as internetIq164_, studio0_.internetIqCluster as interne32_164_, studio0_.lastActivityDelta as lastAct33_164_, studio0_.lastActivitySource as lastAct34_164_, studio0_.lastActivityTime as lastAct35_164_, studio0_.logoImg as logoImg164_, studio0_.mgUuid as mgUuid164_, studio0_.mgcUuid as mgcUuid164_, studio0_.minCriticalityLevel as minCrit39_164_, studio0_.numCheckins as numChec40_164_, studio0_.numLikes as numLikes164_, studio0_.numLocations as numLoca42_164_, studio0_.numMentions as numMent43_164_, studio0_.numRatings as numRatings164_, studio0_.numSEMKeywords as numSEMK45_164_, studio0_.numVouchersSold as numVouc46_164_, studio0_.phone1 as phone47_164_, studio0_.phone2 as phone48_164_, studio0_.phone3 as phone49_164_, studio0_.phoneImpressum as phoneIm50_164_, studio0_.premiumRecordUrls as premium51_164_, studio0_.premiumRecords as premium52_164_, studio0_.qualityPrediction as quality53_164_, studio0_.rating as rating164_, studio0_.ratingSources as ratingS55_164_, studio0_.reachabilityScore as reachab56_164_, studio0_.sectorNameScore as sectorN57_164_, studio0_.sectorOverlapScore as sectorO58_164_, studio0_.sectorScore as sectorS59_164_, studio0_.socialMedia as socialM60_164_, studio0_.sourcesWithNoAff as sources61_164_, studio0_.starsOverall as starsOv62_164_, studio0_.street as street164_, studio0_.successfulWebsiteLookup as success64_164_, studio0_.targetGroupTags as targetG65_164_, studio0_.teaser as teaser164_, studio0_.totalSrcEntities as totalSr67_164_, studio0_.totalSrcEntitiesWithNoAff as totalSr68_164_, studio0_.totalSummaryScore as totalSu69_164_, studio0_.videoUrl as videoUrl164_, studio0_.voucherUrls as voucher71_164_, studio0_.vouchers as vouchers164_, studio0_.website as website164_, studio0_.website2 as website74_164_, studio0_.website3 as website75_164_, studio0_.websiteAvailability as website76_164_, studio0_.websiteCount as website77_164_, studio0_.zip as zip164_ from studio studio0_ where to_tsvector('german'::regconfig, studio0_.companyName) @@ to_tsquery('german', $1)=true limit $2
最佳答案
我想到的唯一解决方案是手动转义 searchText:
String escapedSearchText = String.format("'%s'", searchText.replace("'", "''"));
关于hibernate - setParameter() 没有设置正确的引号,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21679264/
我正在将 NHibernate 3 引入 ASP.NET MVC 网络应用程序的数据访问层。 在 SQL Server 2008 R2 数据库中,geography 数据类型用于将纬度/经度坐标存储在
我想播放有效果的声音。 例如,我使用的是低通和高通,我想用鼠标位置(x,y)设置两个效果的参数。 我使用了DSP.reset()和DSP.setParameter(),但是有一个问题。 如果我越来越快
我有以下代码: String searchText = "..."; String sqlQuery = "FROM Studio s " + "WHERE fts('
这可能是我对JSP和JSTL的误解和资料不全造成的。我有一个网页,其中有输入元素,例如 当我尝试保存表单时,我会检查该 elementID 和其他元素是否符合某些约束“数字,小于 XXX”。如果他们
我正在尝试为我的 MongoDB 安装设置 logLevel 参数的自定义值。执行此操作的标准方法似乎是将 --setParameter 参数传递给 mongod.exe 可执行文件。 但我读了Con
在下面的代码片段中,取自 Blazor 示例,StartDate 属性在派生的 SetParameters 方法中初始化,而它可以这样初始化: [Parameter] DateTime StartDa
当我似乎在查询中使用参数时,出现错误 Invalid parameter number: number of bound variables does not match number of toke
作为构建配置的第一步,我尝试动态更改参数并在后续步骤中使用它。网上看的,好像这个方法是调用##teamcity[setParameter .但这似乎不起作用。它甚至不会在同一步骤中更改值。 例如,我创
我们正在尝试在我们的构建部署系统中集成“一次构建,随处部署”的模型。 MSDeploy 为此创造了奇迹,通过 CRC 校验和比较显着缩短了构建时间,并且( 大部分是 )它在使用参数化更改应用程序 we
我所做的事情必须与此处询问和回答的不同,因为该解决方案似乎不适合我:TeamCity, passing an id generated in one build step to a later bui
我在从正在 XLS 文件中添加参数的 Java 项目打印 PDF 文件时遇到问题。该项目读取 XML 文件,然后从 XSL 样式表生成 PDF 文件。文件内容见下文。 正如您在XLS文件中看到的,我想
如您所见,我有两个命名参数,一个由 setParameterList() 设置,一个由 setParmeter() 设置。问题是列表没有排序。当我显式设置订单字段时,它工作正常,但相同的字符串被传递到
我使用的是 Java 1.7 和 Hibernate 4.1.9。我对 Hibernate 比较陌生,所以如果我遗漏了任何关键信息,请告诉我。我有一个javax.persistence.Entity在
我正在尝试使用 hibernate 准备好的语句创建新表。看起来 "setparameter("values", value)" 添加了额外的引号来查询。我的代码: String hql = "cre
大家好我正在使用以下代码从我的应用程序访问相机:- public void onCreate(Bundle savedInstanceState) { super.onCreate(saved
我有一个 HQL 查询: select date_trunc('day', s.date), sum(s.price) from Sale as s group by date_trunc('
我有一个看起来像 tihs 的简单查询构建器: $qb2 = $this->createQueryBuilder('d') ->select('a.name, c.extern
我正在做一个简单的演示,我可以在我的 Activity 中将相机预览提供给 SurfaceView。我开始知道如果您不设置受支持的大小,setParameters() 会失败。但即使我这样做了,我也会
我最近为我的项目升级了 Jasper Reports 库,从 3.7.6 升级到 6.0.0。我终于可以构建 Maven 并且报告工作得很好。但是,setParameter() 函数似乎在不同版本之间
经过多次尝试,我想我终于把文档背熟了。然后,我需要你的帮助..我不明白为什么 Doctrine 向我显示这个错误: Invalid parameter number: number of bound
我是一名优秀的程序员,十分优秀!