2011-12-28 96 views
0

我想在MySQL和Hibernate中搜索「全字匹配」。Restrictions.sqlRestriction參數索引超出範圍

我的代碼是:

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) 

還有?沒有被標籤取代。

有什麼不對?

回答

1

嘗試使用下列限制:

"tags REGEXP ?" 

並通過以下值作爲參數:

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

謝謝!奇蹟般有效 – Dejell 2011-12-28 14:17:11