我想在MySQL和Hibernate中搜索“全字匹配”。
我的代码是:
public Collection<Template> findByOneTag(String tags) {
// since the tags are in , format we will replace it with |
return getSession().createCriteria(Template.class)
.add(Restrictions.sqlRestriction("tags REGEXP '[[:<:]]?[[:>:]]'", tags.replaceAll(",", "|"), StandardBasicTypes.STRING)).list();
}和模板:
@Entity
public class Template implements Serializable {
private static final long serialVersionUID = 1L;
@Id
@GeneratedValue(strategy=GenerationType.IDENTITY)
@Column(name="template_id")
private int templateId;
@Lob()
private String content;
@Column(name="method_id")
private byte methodId;
private String subject;
private String tags;
@Column(name="template_name")
private String templateName;
private String thumbnail;
}然而,我得到了:
Caused by: java.sql.SQLException: Parameter index out of range (1 > number of parameters, which is 0).
at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:1073)
at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:987)
at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:982)
at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:927)
at com.mysql.jdbc.PreparedStatement.checkBounds(PreparedStatement.java:3729)以及?不会被替换为标签。
怎么啦?
发布于 2011-12-28 22:00:58
尝试使用以下限制:
"tags REGEXP ?"并将以下值作为参数进行传递:
"[[:<:]]" + yourOriginalParameter + "[[:>:]]"https://stackoverflow.com/questions/8656790
复制相似问题