Home » Support » Forum » Like fails if both underscore and percent are used

Like fails if both underscore and percent are used

#1

When using a like query that has both _ and % in it, ObjectDB seems to fail.

Here is a unit test to demonstrate:

 

import org.junit.*;

import javax.persistence.*;
import javax.persistence.criteria.CriteriaBuilder;
import javax.persistence.criteria.CriteriaQuery;
import javax.persistence.criteria.Root;
import java.util.List;

import static org.junit.Assert.*;

public class LikeTest {

    @Entity
    public static class StringHolder
    {
        @Id
        public Long id;

        public String content;

        public StringHolder(String content)
        {
            this.content = content;
        }
    }

    private EntityManagerFactory emf;
    private EntityManager em;

    @Before
    public void setUp()
    {
        emf = Persistence.createEntityManagerFactory("./foo.odb");
        em = emf.createEntityManager();
        em.getMetamodel().entity(StringHolder.class);

        em.getTransaction().begin();
        for(StringHolder item : getAll())
        {
            em.remove(item);
        }
        em.getTransaction().commit();
    }

    @Test
    public void likeTest()
    {
        StringHolder entry = new StringHolder("foobar");
        assertEquals(0, getAll().size()); //DB is empty
        em.getTransaction().begin();
        em.persist(entry);
        em.getTransaction().commit();

        assertEquals(1, getAll().size()); //Save was successful

        final String queryString = "SELECT holder FROM StringHolder holder WHERE holder.content LIKE :filter";
        Query query = em.createQuery(queryString, StringHolder.class);

        query.setParameter("filter", "_oo%");
        List results = query.getResultList();
        assertEquals(1, results.size());
    }

    private List<StringHolder> getAll()
    {
        CriteriaBuilder builder = em.getCriteriaBuilder();

        CriteriaQuery<StringHolder> query = builder.createQuery(StringHolder.class);
        Root<StringHolder> c = query.from(StringHolder.class);
        query = query.select(c);
        return em.createQuery(query).getResultList();
    }
}
#2

Thank you for this report and for the clear test case.

Please try build 2.5.4_03, which should fix this bug.

ObjectDB Support
ObjectDB - Fast Object Database for Java (JPA/JDO)
#3

Confirmed. Fixes both this test case and my internal one


Post Reply

To post a reply and/or subscribe to update notifications - please login