简体   繁体   中英

MsSQL named JDBC template LIKE match

I'm trying to run a like match using the named JDBC template with the query...

SELECT
    id, guid, body, summary, status_id, language_id, is_mce, content_type_id, 
    last_edited_by, locked_by, owner_id, coordinator_id, writer_id, paperless, 
    content_body_type, created, updated, locked_timestamp
FROM dbo.content
WHERE notes LIKE :notes

:notes is bound to %test% and unfortunately it's not returning any results. I've checked the database and verified that this data exists. Is this not the correct way to perform a like match?

update

Part of the code to generate this query...

    String notes = search.getNotes();
    if (notes instanceof String && notes.length() > 0) {
        if (!previousClause) {
            searchQuery.append("WHERE ");
            previousClause = true;
        }
        searchQuery.append("notes LIKE :notes AND ");
        queryMap.put("notes", "%"+notes+"%");
    }

    Collection<Category> categories = search.getCategoryCollection();
    if (categories != null && categories.size() > 0) {
        Short x = 0;
        for (Category category : categories) {
            searchQuery.append("INNER JOIN dbo.content_to_category AS content_to_category"+x.toString()+" ON (content_to_category"+x.toString()+".category_id IN (:categoryId"+x.toString()+") AND content_to_category"+x.toString()+".content_id = content.id) ");
            queryMap.put("categoryId"+x.toString(), category.getId());
            x++;
        }
    }

I'll assume: (1) you're using Hibernate from a reasonable version; and (2) that the above selected fields are a subset of content 's fields.

Should go something like so:

Criteria crit = session.createCriteria(Content.class);

// add all selected fields:
ProjectionList projList = Projections.projectionList();
projList.add(Projections.property("id"));
projList.add(Projections.property("guid"));
projList.add(Projections.property("body"));
projList.add(Projections.property("summary"));
projList.add(Projections.property("status_id"));
projList.add(Projections.property("language_id"));
projList.add(Projections.property("is_mce"));
projList.add(Projections.property("content_type_id"));
projList.add(Projections.property("last_edited_by"));
projList.add(Projections.property("locked_by"));
projList.add(Projections.property("owner_id"));
projList.add(Projections.property("coordinator_id"));
projList.add(Projections.property("writer_id"));
projList.add(Projections.property("coordinator_id"));
projList.add(Projections.property("paperless"));
projList.add(Projections.property("content_body_type"));
projList.add(Projections.property("created"));
projList.add(Projections.property("updated"));
projList.add(Projections.property("locked_timestamp"));
crit.setProjection(projList);

// add a Restriction on notes field, using like:
crit.add(Restrictions.like("notes", "test", MatchMode.ANYWHERE));

// invoke query:
List list = crit.list();

UPDATE

Just to be sure, the correct syntax you should be using is (relying on intuition and several blogs out there; I couldn't pin down a documentation excerpt):

Query query = // create long complicated query that ends with "WHERE notes LIKE :notes"
query.setParameter("notes", "%test%");

The reason this might not work in your case, is that like operator applies for varchar operands and such... And it seems from this piece of your code:

if (notes instanceof String && notes.length() > 0) {

that you're not absolutely sure about the underlying type of that field... Could it be that notes isn't a String ?

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM