gpt4 book ai didi

java - Restrictions.sqlRestriction 参数索引超出范围

转载 作者:行者123 更新时间:2023-11-29 14:37:42 29 4
gpt4 key购买 nike

我想在 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)

以及?没有被标签替换。

出了什么问题?

最佳答案

尝试使用以下限制:

"tags REGEXP ?"

并将以下值作为参数传递:

"[[:<:]]" + yourOriginalParameter + "[[:>:]]"

关于java - Restrictions.sqlRestriction 参数索引超出范围,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8656790/

29 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com