Search in sources :

Example 1 with Example

use of org.hibernate.criterion.Example in project hibernate-orm by hibernate.

the class QueryByExampleTest method testSimpleQBE.

@Test
public void testSimpleQBE() throws Exception {
    deleteData();
    initData();
    Session s = openSession();
    Transaction t = s.beginTransaction();
    Componentizable master = getMaster("hibernate", "open sourc%", "open source1");
    Criteria crit = s.createCriteria(Componentizable.class);
    Example ex = Example.create(master).enableLike();
    crit.add(ex);
    List result = crit.list();
    assertNotNull(result);
    assertEquals(1, result.size());
    t.commit();
    s.close();
}
Also used : Transaction(org.hibernate.Transaction) Example(org.hibernate.criterion.Example) List(java.util.List) Criteria(org.hibernate.Criteria) Session(org.hibernate.Session) Test(org.junit.Test)

Example 2 with Example

use of org.hibernate.criterion.Example in project hibernate-orm by hibernate.

the class QueryByExampleTest method testJunctionNotExpressionQBE.

@Test
@SkipForDialect(value = SybaseASE15Dialect.class, jiraKey = "HHH-4580")
public void testJunctionNotExpressionQBE() throws Exception {
    deleteData();
    initData();
    Session s = openSession();
    Transaction t = s.beginTransaction();
    Componentizable master = getMaster("hibernate", null, "ope%");
    Criteria crit = s.createCriteria(Componentizable.class);
    Example ex = Example.create(master).enableLike();
    crit.add(Restrictions.or(Restrictions.not(ex), ex));
    List result = crit.list();
    assertNotNull(result);
    assertEquals(2, result.size());
    t.commit();
    s.close();
}
Also used : Transaction(org.hibernate.Transaction) Example(org.hibernate.criterion.Example) List(java.util.List) Criteria(org.hibernate.Criteria) Session(org.hibernate.Session) SkipForDialect(org.hibernate.testing.SkipForDialect) Test(org.junit.Test)

Example 3 with Example

use of org.hibernate.criterion.Example in project microservices by pwillhan.

the class HibernateCriteria method executeQueries.

@Test
public void executeQueries() throws Exception {
    TestDataCategoriesItems testData = storeTestData();
    UserTransaction tx = TM.getUserTransaction();
    try {
        tx.begin();
        EntityManager em = JPA.createEntityManager();
        Session session = em.unwrap(Session.class);
        {
            // Selection
            org.hibernate.Criteria criteria = session.createCriteria(Item.class);
            List<Item> items = criteria.list();
            assertEquals(items.size(), 3);
        }
        em.clear();
        {
            DetachedCriteria criteria = DetachedCriteria.forClass(Item.class);
            List<Item> items = criteria.getExecutableCriteria(session).list();
            assertEquals(items.size(), 3);
        }
        em.clear();
        {
            // Ordering
            List<User> users = session.createCriteria(User.class).addOrder(Order.asc("firstname")).addOrder(Order.asc("lastname")).list();
            assertEquals(users.size(), 3);
            assertEquals(users.get(0).getFirstname(), "Jane");
            assertEquals(users.get(1).getFirstname(), "John");
            assertEquals(users.get(2).getFirstname(), "Robert");
        }
        em.clear();
        {
            // Restriction
            List<Item> items = session.createCriteria(Item.class).add(Restrictions.eq("name", "Foo")).list();
            assertEquals(items.size(), 1);
        }
        em.clear();
        {
            List<User> users = session.createCriteria(User.class).add(Restrictions.like("username", "j", MatchMode.START).ignoreCase()).list();
            assertEquals(users.size(), 2);
        }
        em.clear();
        {
            List<User> users = session.createCriteria(User.class).add(Restrictions.eq("homeAddress.city", "Some City")).list();
            assertEquals(users.size(), 1);
            assertEquals(users.get(0).getUsername(), "johndoe");
        }
        em.clear();
        {
            List<User> users = session.createCriteria(User.class).add(Restrictions.sqlRestriction("length({alias}.USERNAME) < ?", 8, StandardBasicTypes.INTEGER)).list();
            assertEquals(users.size(), 2);
        }
        em.clear();
        {
            // Projection
            List<Object[]> result = session.createCriteria(User.class).setProjection(Projections.projectionList().add(Projections.property("id")).add(Projections.property("username")).add(Projections.property("homeAddress"))).list();
            assertEquals(result.size(), 3);
            for (Object[] tuple : result) {
                assertTrue(tuple[0] instanceof Long);
                assertTrue(tuple[1] instanceof String);
                assertTrue(tuple[2] == null || tuple[2] instanceof Address);
            }
        }
        em.clear();
        {
            List<String> result = session.createCriteria(Item.class).setProjection(Projections.projectionList().add(Projections.sqlProjection("NAME || ':' || AUCTIONEND as RESULT", new String[] { "RESULT" }, new Type[] { StandardBasicTypes.STRING }))).list();
            assertEquals(result.size(), 3);
        }
        em.clear();
        {
            List<Object[]> result = session.createCriteria(User.class).setProjection(Projections.projectionList().add(Projections.groupProperty("lastname")).add(Projections.rowCount())).list();
            assertEquals(result.size(), 2);
            for (Object[] tuple : result) {
                assertTrue(tuple[0] instanceof String);
                assertTrue(tuple[1] instanceof Long);
            }
        }
        em.clear();
        {
            List<Object[]> result = session.createCriteria(Bid.class).setProjection(Projections.projectionList().add(Projections.groupProperty("item")).add(Projections.avg("amount"))).list();
            assertEquals(result.size(), 2);
            for (Object[] tuple : result) {
                assertTrue(tuple[0] instanceof Item);
                assertTrue(tuple[1] instanceof Double);
            }
        }
        em.clear();
        {
            // Joins
            List<Bid> result = session.createCriteria(Bid.class).createCriteria(// Inner join
            "item").add(Restrictions.like("name", "Fo", MatchMode.START)).list();
            assertEquals(result.size(), 3);
            for (Bid bid : result) {
                assertEquals(bid.getItem().getId(), testData.items.getFirstId());
            }
        }
        em.clear();
        {
            List<Bid> result = session.createCriteria(Bid.class).createCriteria(// Inner join
            "item").add(Restrictions.isNotNull("buyNowPrice")).createCriteria(// Inner join
            "seller").add(Restrictions.eq("username", "johndoe")).list();
            assertEquals(result.size(), 3);
        }
        em.clear();
        {
            List<Object[]> result = session.createCriteria(Bid.class, "b").createAlias("item", "i", JoinType.RIGHT_OUTER_JOIN).add(Restrictions.or(Restrictions.isNull("b.id"), Restrictions.gt("amount", new BigDecimal(100)))).setResultTransformer(Criteria.PROJECTION).list();
            assertEquals(result.size(), 2);
            // Criteria quirk: the root entity alias is always last in the result tuple
            assertTrue(result.get(0)[0] instanceof Item);
            assertTrue(result.get(0)[1] instanceof Bid);
            assertTrue(result.get(1)[0] instanceof Item);
            assertEquals(result.get(1)[1], null);
        }
        em.clear();
        {
            List<Bid> result = session.createCriteria(Bid.class).createCriteria(// Inner join
            "item").createAlias("seller", // Inner join
            "s").add(Restrictions.and(Restrictions.eq("s.username", "johndoe"), Restrictions.isNotNull("buyNowPrice"))).list();
            assertEquals(result.size(), 3);
        }
        em.clear();
        {
            // Fetching
            List<Item> result = session.createCriteria(Item.class).setFetchMode("bids", FetchMode.JOIN).list();
            // 3 items, 4 bids, 5 "rows" in result!
            assertEquals(result.size(), 5);
            // In-memory "distinct"
            Set<Item> distinctResult = new LinkedHashSet<Item>(result);
            // It was only three items all along...
            assertEquals(distinctResult.size(), 3);
            boolean haveBids = false;
            for (Item item : result) {
                // No more lazy loading!
                em.detach(item);
                if (item.getBids().size() > 0) {
                    haveBids = true;
                    break;
                }
            }
            assertTrue(haveBids);
        }
        em.clear();
        {
            List<Item> result = session.createCriteria(Item.class).setFetchMode("bids", FetchMode.JOIN).setResultTransformer(Criteria.DISTINCT_ROOT_ENTITY).list();
            // Hibernate filtered it for us in-memory
            assertEquals(result.size(), 3);
            boolean haveBids = false;
            for (Item item : result) {
                // No more lazy loading!
                em.detach(item);
                if (item.getBids().size() > 0) {
                    haveBids = true;
                    break;
                }
            }
            assertTrue(haveBids);
        }
        em.clear();
        {
            List<Item> result = session.createCriteria(Item.class).createAlias("bids", "b", JoinType.LEFT_OUTER_JOIN).setFetchMode("b", FetchMode.JOIN).createAlias("b.bidder", "bdr", JoinType.INNER_JOIN).setFetchMode("bdr", FetchMode.JOIN).createAlias("seller", "s", JoinType.LEFT_OUTER_JOIN).setFetchMode("s", FetchMode.JOIN).list();
            result = new ArrayList<Item>(new LinkedHashSet<Item>(result));
            assertEquals(result.size(), 2);
            boolean haveBids = false;
            boolean haveBidder = false;
            boolean haveSeller = false;
            for (Item item : result) {
                // No more lazy loading!
                em.detach(item);
                if (item.getBids().size() > 0) {
                    haveBids = true;
                    Bid bid = item.getBids().iterator().next();
                    if (bid.getBidder() != null && bid.getBidder().getUsername() != null) {
                        haveBidder = true;
                    }
                }
                if (item.getSeller() != null && item.getSeller().getUsername() != null)
                    haveSeller = true;
            }
            assertTrue(haveBids);
            assertTrue(haveBidder);
            assertTrue(haveSeller);
        }
        em.clear();
        {
            // Subquery
            DetachedCriteria sq = DetachedCriteria.forClass(Item.class, "i");
            sq.add(Restrictions.eqProperty("i.seller.id", "u.id"));
            sq.setProjection(Projections.rowCount());
            List<User> result = session.createCriteria(User.class, "u").add(Subqueries.lt(1l, sq)).list();
            assertEquals(result.size(), 1);
            User user = result.iterator().next();
            assertEquals(user.getId(), testData.users.getFirstId());
        }
        em.clear();
        {
            DetachedCriteria sq = DetachedCriteria.forClass(Bid.class, "b");
            sq.add(Restrictions.eqProperty("b.item.id", "i.id"));
            sq.setProjection(Projections.property("amount"));
            List<Item> result = session.createCriteria(Item.class, "i").add(Subqueries.geAll(new BigDecimal(10), sq)).list();
            assertEquals(result.size(), 2);
        }
        em.clear();
        {
            // Query by example: Find all users with last name "Doe"
            /* 
                   First, create an "empty" instance of <code>User</code> as a template for your
                   search and set the property values you are looking for. You want to retrieve
                   people with the last name "Doe".
                 */
            User template = new User();
            template.setLastname("Doe");
            /* 
                   Create an instance of <code>Example</code> with the template, this API allows
                   you to fine-tune the search. You want the case of the last name to be ignored,
                   and a substring search, so "Doe", "DoeX", or "Doe Y" would match. In addition, the
                   <code>User</code> class has a <code>boolean</code> property called
                   <code>activated</code>. As a primitive, it can't be <code>null</code> and its
                   default value is <code>false</code>, so Hibernate would include it in the search
                   and only return users that aren't activated. As you want all users, you tell
                   Hibernate to ignore that property when building the search query.
                 */
            org.hibernate.criterion.Example example = Example.create(template);
            example.ignoreCase();
            example.enableLike(MatchMode.START);
            example.excludeProperty("activated");
            /* 
                   The <code>Example</code> is added to a <code>Criteria</code> as a restriction.
                 */
            List<User> users = session.createCriteria(User.class).add(example).list();
            assertEquals(users.size(), 2);
        }
        em.clear();
        {
            // Find all items which have a name starting with "B" or "b", and a
            // seller with the last name "Doe"
            Item itemTemplate = new Item();
            itemTemplate.setName("B");
            Example exampleItem = Example.create(itemTemplate);
            exampleItem.ignoreCase();
            exampleItem.enableLike(MatchMode.START);
            exampleItem.excludeProperty("auctionType");
            exampleItem.excludeProperty("createdOn");
            User userTemplate = new User();
            userTemplate.setLastname("Doe");
            Example exampleUser = Example.create(userTemplate);
            exampleUser.excludeProperty("activated");
            List<Item> items = session.createCriteria(Item.class).add(exampleItem).createCriteria("seller").add(exampleUser).list();
            assertEquals(items.size(), 1);
            assertTrue(items.get(0).getName().startsWith("B"));
            assertEquals(items.get(0).getSeller().getLastname(), "Doe");
        }
        em.clear();
        tx.commit();
        em.close();
    } finally {
        TM.rollback();
    }
}
Also used : UserTransaction(javax.transaction.UserTransaction) User(org.jpwh.model.querying.User) LinkedHashSet(java.util.LinkedHashSet) Set(java.util.Set) Address(org.jpwh.model.querying.Address) DetachedCriteria(org.hibernate.criterion.DetachedCriteria) ArrayList(java.util.ArrayList) Criteria(org.hibernate.Criteria) DetachedCriteria(org.hibernate.criterion.DetachedCriteria) BigDecimal(java.math.BigDecimal) Item(org.jpwh.model.querying.Item) EntityManager(javax.persistence.EntityManager) Example(org.hibernate.criterion.Example) ArrayList(java.util.ArrayList) List(java.util.List) Bid(org.jpwh.model.querying.Bid) Session(org.hibernate.Session) Test(org.testng.annotations.Test) QueryingTest(org.jpwh.test.querying.QueryingTest)

Example 4 with Example

use of org.hibernate.criterion.Example in project RecordManager2 by moravianlibrary.

the class ObalkyKnihTOCDAOHibernate method findByExample.

@Override
@SuppressWarnings("unchecked")
public List<ObalkyKnihTOC> findByExample(ObalkyKnihTOC example, boolean includeNullProperties, String... excludeProperties) {
    Example exam = Example.create(example);
    if (includeNullProperties) {
        exam.setPropertySelector(Example.AllPropertySelector.INSTANCE);
    }
    if (excludeProperties != null) {
        for (String excludeProperty : excludeProperties) {
            exam.excludeProperty(excludeProperty);
        }
    }
    Criteria crit = sessionFactory.getCurrentSession().createCriteria(ObalkyKnihTOC.class);
    return (List<ObalkyKnihTOC>) crit.add(exam).list();
}
Also used : Example(org.hibernate.criterion.Example) List(java.util.List) Criteria(org.hibernate.Criteria)

Example 5 with Example

use of org.hibernate.criterion.Example in project hibernate-orm by hibernate.

the class QueryByExampleTest method testExcludingQBE.

@Test
public void testExcludingQBE() throws Exception {
    deleteData();
    initData();
    Session s = openSession();
    Transaction t = s.beginTransaction();
    Componentizable master = getMaster("hibernate", null, "ope%");
    Criteria crit = s.createCriteria(Componentizable.class);
    Example ex = Example.create(master).enableLike().excludeProperty("component.subComponent");
    crit.add(ex);
    List result = crit.list();
    assertNotNull(result);
    assertEquals(3, result.size());
    master = getMaster("hibernate", "ORM tool", "fake stuff");
    crit = s.createCriteria(Componentizable.class);
    ex = Example.create(master).enableLike().excludeProperty("component.subComponent.subName1");
    crit.add(ex);
    result = crit.list();
    assertNotNull(result);
    assertEquals(1, result.size());
    t.commit();
    s.close();
}
Also used : Transaction(org.hibernate.Transaction) Example(org.hibernate.criterion.Example) List(java.util.List) Criteria(org.hibernate.Criteria) Session(org.hibernate.Session) Test(org.junit.Test)

Aggregations

List (java.util.List)5 Criteria (org.hibernate.Criteria)5 Example (org.hibernate.criterion.Example)5 Session (org.hibernate.Session)4 Transaction (org.hibernate.Transaction)3 Test (org.junit.Test)3 BigDecimal (java.math.BigDecimal)1 ArrayList (java.util.ArrayList)1 LinkedHashSet (java.util.LinkedHashSet)1 Set (java.util.Set)1 EntityManager (javax.persistence.EntityManager)1 UserTransaction (javax.transaction.UserTransaction)1 DetachedCriteria (org.hibernate.criterion.DetachedCriteria)1 SkipForDialect (org.hibernate.testing.SkipForDialect)1 Address (org.jpwh.model.querying.Address)1 Bid (org.jpwh.model.querying.Bid)1 Item (org.jpwh.model.querying.Item)1 User (org.jpwh.model.querying.User)1 QueryingTest (org.jpwh.test.querying.QueryingTest)1 Test (org.testng.annotations.Test)1