org.hibernate.criterion.Example Java Examples
The following examples show how to use
org.hibernate.criterion.Example.
You can vote up the ones you like or vote down the ones you don't like,
and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: HibernateTemplate.java From java-technology-stack with MIT License | 6 votes |
@Override @SuppressWarnings({"unchecked", "deprecation"}) public <T> List<T> findByExample( @Nullable final String entityName, final T exampleEntity, final int firstResult, final int maxResults) throws DataAccessException { Assert.notNull(exampleEntity, "Example entity must not be null"); return nonNull(executeWithNativeSession((HibernateCallback<List<T>>) session -> { Criteria executableCriteria = (entityName != null ? session.createCriteria(entityName) : session.createCriteria(exampleEntity.getClass())); executableCriteria.add(Example.create(exampleEntity)); prepareCriteria(executableCriteria); if (firstResult >= 0) { executableCriteria.setFirstResult(firstResult); } if (maxResults > 0) { executableCriteria.setMaxResults(maxResults); } return executableCriteria.list(); })); }
Example #2
Source File: GenericHibernateDAO.java From jVoiD with Apache License 2.0 | 6 votes |
@SuppressWarnings("unchecked") @Override public List<T> findByExample(T exampleInstance, String... excludeProperty) { try { Criteria crit = getSession().createCriteria(getPersistentClass()); Example example = Example.create(exampleInstance); for (String exclude : excludeProperty) { example.excludeProperty(exclude); } crit.add(example); return crit.list(); } catch (Exception e) { // e.printStackTrace(); getTransaction().rollback(); return null; } }
Example #3
Source File: BaseDAO.java From QiQuYingServer with Apache License 2.0 | 6 votes |
/** * @Title: countByExample * @Description: 根据模型统计 * @param @param entityBean * @param @return * @return int */ public <T> int countByExample(final T obj) { return (Integer) getHibernateTemplate().executeWithNativeSession(new HibernateCallback<Integer>() { public Integer doInHibernate(Session s) throws HibernateException, SQLException { // 组装属性 Criteria criteria = s.createCriteria(obj.getClass()).setProjection(Projections.projectionList().add(Projections.rowCount())) .add(Example.create(obj)); if (getHibernateTemplate().isCacheQueries()) { criteria.setCacheable(true); if (getHibernateTemplate().getQueryCacheRegion() != null) criteria.setCacheRegion(getHibernateTemplate().getQueryCacheRegion()); } if (getHibernateTemplate().getFetchSize() > 0) criteria.setFetchSize(getHibernateTemplate().getFetchSize()); if (getHibernateTemplate().getMaxResults() > 0) criteria.setMaxResults(getHibernateTemplate().getMaxResults()); SessionFactoryUtils.applyTransactionTimeout(criteria, getSessionFactory()); return (Integer) criteria.uniqueResult(); } }); }
Example #4
Source File: QueryByExampleTest.java From cacheonix-core with GNU Lesser General Public License v2.1 | 6 votes |
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(Expression.or(Expression.not(ex), ex)); List result = crit.list(); assertNotNull(result); assertEquals(2, result.size()); t.commit(); s.close(); }
Example #5
Source File: QueryByExampleTest.java From cacheonix-core with GNU Lesser General Public License v2.1 | 6 votes |
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(); }
Example #6
Source File: HibernateTemplate.java From spring-analysis-note with MIT License | 6 votes |
@Override @SuppressWarnings({"unchecked", "deprecation"}) public <T> List<T> findByExample( @Nullable final String entityName, final T exampleEntity, final int firstResult, final int maxResults) throws DataAccessException { Assert.notNull(exampleEntity, "Example entity must not be null"); return nonNull(executeWithNativeSession((HibernateCallback<List<T>>) session -> { Criteria executableCriteria = (entityName != null ? session.createCriteria(entityName) : session.createCriteria(exampleEntity.getClass())); executableCriteria.add(Example.create(exampleEntity)); prepareCriteria(executableCriteria); if (firstResult >= 0) { executableCriteria.setFirstResult(firstResult); } if (maxResults > 0) { executableCriteria.setMaxResults(maxResults); } return executableCriteria.list(); })); }
Example #7
Source File: HibernateTemplate.java From spring4-understanding with Apache License 2.0 | 6 votes |
@Override public <T> List<T> findByExample( final String entityName, final T exampleEntity, final int firstResult, final int maxResults) throws DataAccessException { Assert.notNull(exampleEntity, "Example entity must not be null"); return executeWithNativeSession(new HibernateCallback<List<T>>() { @Override @SuppressWarnings("unchecked") public List<T> doInHibernate(Session session) throws HibernateException { Criteria executableCriteria = (entityName != null ? session.createCriteria(entityName) : session.createCriteria(exampleEntity.getClass())); executableCriteria.add(Example.create(exampleEntity)); prepareCriteria(executableCriteria); if (firstResult >= 0) { executableCriteria.setFirstResult(firstResult); } if (maxResults > 0) { executableCriteria.setMaxResults(maxResults); } return executableCriteria.list(); } }); }
Example #8
Source File: HibernateTemplate.java From spring4-understanding with Apache License 2.0 | 6 votes |
@Override public <T> List<T> findByExample( final String entityName, final T exampleEntity, final int firstResult, final int maxResults) throws DataAccessException { Assert.notNull(exampleEntity, "Example entity must not be null"); return executeWithNativeSession(new HibernateCallback<List<T>>() { @Override @SuppressWarnings("unchecked") public List<T> doInHibernate(Session session) throws HibernateException { Criteria executableCriteria = (entityName != null ? session.createCriteria(entityName) : session.createCriteria(exampleEntity.getClass())); executableCriteria.add(Example.create(exampleEntity)); prepareCriteria(executableCriteria); if (firstResult >= 0) { executableCriteria.setFirstResult(firstResult); } if (maxResults > 0) { executableCriteria.setMaxResults(maxResults); } return executableCriteria.list(); } }); }
Example #9
Source File: HibernateTemplate.java From spring4-understanding with Apache License 2.0 | 6 votes |
@Override public <T> List<T> findByExample( final String entityName, final T exampleEntity, final int firstResult, final int maxResults) throws DataAccessException { Assert.notNull(exampleEntity, "Example entity must not be null"); return executeWithNativeSession(new HibernateCallback<List<T>>() { @Override @SuppressWarnings("unchecked") public List<T> doInHibernate(Session session) throws HibernateException { Criteria executableCriteria = (entityName != null ? session.createCriteria(entityName) : session.createCriteria(exampleEntity.getClass())); executableCriteria.add(Example.create(exampleEntity)); prepareCriteria(executableCriteria); if (firstResult >= 0) { executableCriteria.setFirstResult(firstResult); } if (maxResults > 0) { executableCriteria.setMaxResults(maxResults); } return executableCriteria.list(); } }); }
Example #10
Source File: HibernateTemplate.java From lams with GNU General Public License v2.0 | 6 votes |
@Override public <T> List<T> findByExample( final String entityName, final T exampleEntity, final int firstResult, final int maxResults) throws DataAccessException { Assert.notNull(exampleEntity, "Example entity must not be null"); return executeWithNativeSession(new HibernateCallback<List<T>>() { @Override @SuppressWarnings("unchecked") public List<T> doInHibernate(Session session) throws HibernateException { Criteria executableCriteria = (entityName != null ? session.createCriteria(entityName) : session.createCriteria(exampleEntity.getClass())); executableCriteria.add(Example.create(exampleEntity)); prepareCriteria(executableCriteria); if (firstResult >= 0) { executableCriteria.setFirstResult(firstResult); } if (maxResults > 0) { executableCriteria.setMaxResults(maxResults); } return executableCriteria.list(); } }); }
Example #11
Source File: HibernateTemplate.java From lams with GNU General Public License v2.0 | 6 votes |
@Override @SuppressWarnings("deprecation") public <T> List<T> findByExample( final String entityName, final T exampleEntity, final int firstResult, final int maxResults) throws DataAccessException { Assert.notNull(exampleEntity, "Example entity must not be null"); return executeWithNativeSession(new HibernateCallback<List<T>>() { @Override @SuppressWarnings("unchecked") public List<T> doInHibernate(Session session) throws HibernateException { Criteria executableCriteria = (entityName != null ? session.createCriteria(entityName) : session.createCriteria(exampleEntity.getClass())); executableCriteria.add(Example.create(exampleEntity)); prepareCriteria(executableCriteria); if (firstResult >= 0) { executableCriteria.setFirstResult(firstResult); } if (maxResults > 0) { executableCriteria.setMaxResults(maxResults); } return executableCriteria.list(); } }); }
Example #12
Source File: HibernateTemplate.java From lams with GNU General Public License v2.0 | 6 votes |
@Override public <T> List<T> findByExample( final String entityName, final T exampleEntity, final int firstResult, final int maxResults) throws DataAccessException { Assert.notNull(exampleEntity, "Example entity must not be null"); return executeWithNativeSession(new HibernateCallback<List<T>>() { @Override @SuppressWarnings("unchecked") public List<T> doInHibernate(Session session) throws HibernateException { Criteria executableCriteria = (entityName != null ? session.createCriteria(entityName) : session.createCriteria(exampleEntity.getClass())); executableCriteria.add(Example.create(exampleEntity)); prepareCriteria(executableCriteria); if (firstResult >= 0) { executableCriteria.setFirstResult(firstResult); } if (maxResults > 0) { executableCriteria.setMaxResults(maxResults); } return executableCriteria.list(); } }); }
Example #13
Source File: HibernateUtils.java From jdal with Apache License 2.0 | 5 votes |
/** * Get a hibernate Example object that excludes zeroes values and excludes * all boolean -primitive and wrapper class- attributes of a given object. * @param instance given object for a build a Example object. * @return a hibernate Example object. */ public static Example excludeBooleanFields (Object instance) { Example result = Example.create(instance).excludeZeroes(); Set<String> fieldNames = getFieldNamesByType(instance, Boolean.class); fieldNames.addAll(getFieldNamesByType(instance, Boolean.TYPE)); for (String fieldName : fieldNames) { result.excludeProperty(fieldName); } return result; }
Example #14
Source File: SakaiPersonManagerImpl.java From sakai with Educational Community License v2.0 | 5 votes |
/** * @see org.sakaiproject.api.common.edu.person.SakaiPersonManager#findSakaiPerson(org.sakaiproject.api.common.edu.person.SakaiPerson) */ public List findSakaiPerson(final SakaiPerson queryByExample) { if (log.isDebugEnabled()) { log.debug("findSakaiPerson(SakaiPerson {})", queryByExample); } if (queryByExample == null) throw new IllegalArgumentException("Illegal queryByExample argument passed!"); final HibernateCallback hcb = new HibernateCallback() { public Object doInHibernate(Session session) throws HibernateException { Criteria criteria = session.createCriteria(queryByExample.getClass()); criteria.add(Example.create(queryByExample)); // criteria.setCacheable(cacheFindSakaiPersonSakaiPerson); return criteria.list(); } }; log.debug("return getHibernateTemplate().executeFind(hcb);"); List hb = (List) getHibernateTemplate().execute(hcb); if (photoService.overRidesDefault()) { return getDiskPhotosForList(hb); } else { return hb; } }
Example #15
Source File: SakaiPersonManagerImpl.java From sakai with Educational Community License v2.0 | 5 votes |
/** * @see org.sakaiproject.api.common.edu.person.SakaiPersonManager#findSakaiPerson(org.sakaiproject.api.common.edu.person.SakaiPerson) */ public List findSakaiPerson(final SakaiPerson queryByExample) { if (log.isDebugEnabled()) { log.debug("findSakaiPerson(SakaiPerson {})", queryByExample); } if (queryByExample == null) throw new IllegalArgumentException("Illegal queryByExample argument passed!"); final HibernateCallback hcb = new HibernateCallback() { public Object doInHibernate(Session session) throws HibernateException { Criteria criteria = session.createCriteria(queryByExample.getClass()); criteria.add(Example.create(queryByExample)); // criteria.setCacheable(cacheFindSakaiPersonSakaiPerson); return criteria.list(); } }; log.debug("return getHibernateTemplate().executeFind(hcb);"); List hb = (List) getHibernateTemplate().execute(hcb); if (photoService.overRidesDefault()) { return getDiskPhotosForList(hb); } else { return hb; } }
Example #16
Source File: CourseDAO.java From CoolSignIn with Apache License 2.0 | 5 votes |
public List findByExample(Course instance) { log.debug("finding Course instance by example"); try { List results = getSession() .createCriteria(Course.class) .add(Example.create(instance)).list(); log.debug("find by example successful, result size: " + results.size()); return results; } catch (RuntimeException re) { log.error("find by example failed", re); throw re; } }
Example #17
Source File: StudentDAO.java From CoolSignIn with Apache License 2.0 | 5 votes |
public List findByExample(Student instance) { log.debug("finding Student instance by example"); try { List results = getSession() .createCriteria(Student.class) .add(Example.create(instance)).list(); log.debug("find by example successful, result size: " + results.size()); return results; } catch (RuntimeException re) { log.error("find by example failed", re); throw re; } }
Example #18
Source File: SignInfoDAO.java From CoolSignIn with Apache License 2.0 | 5 votes |
public List findByExample(SignInfo instance) { log.debug("finding SignInfo instance by example"); try { List results = getSession() .createCriteria(SignInfo.class) .add(Example.create(instance)).list(); log.debug("find by example successful, result size: " + results.size()); return results; } catch (RuntimeException re) { log.error("find by example failed", re); throw re; } }
Example #19
Source File: GenericBaseCommonDao.java From jeecg with Apache License 2.0 | 5 votes |
/** * 根据实体模版查找 * * @param entityName * @param exampleEntity * @return */ public List findByExample(final String entityName, final Object exampleEntity) { Assert.notNull(exampleEntity, "Example entity must not be null"); Criteria executableCriteria = (entityName != null ? getSession() .createCriteria(entityName) : getSession().createCriteria( exampleEntity.getClass())); executableCriteria.add(Example.create(exampleEntity)); return executableCriteria.list(); }
Example #20
Source File: StudentSheetDAO.java From CoolSignIn with Apache License 2.0 | 5 votes |
public List findByExample(StudentSheet instance) { log.debug("finding StudentSheet instance by example"); try { List results = getSession() .createCriteria(StudentSheet.class) .add(Example.create(instance)).list(); log.debug("find by example successful, result size: " + results.size()); return results; } catch (RuntimeException re) { log.error("find by example failed", re); throw re; } }
Example #21
Source File: UserDAO.java From CoolSignIn with Apache License 2.0 | 5 votes |
public List findByExample(User instance) { log.debug("finding User instance by example"); try { List results = getSession().createCriteria(User.class) .add(Example.create(instance)).list(); log.debug("find by example successful, result size: " + results.size()); return results; } catch (RuntimeException re) { log.error("find by example failed", re); throw re; } }
Example #22
Source File: SoapboxDao.java From soapbox-race with GNU General Public License v2.0 | 5 votes |
@Override @SuppressWarnings("unchecked") public List<ISoapBoxEntity> find(ISoapBoxEntity entity) { EntityManager manager = ConnectionDB.getManager(); manager.clear(); Session sessao = (Session) manager.getDelegate(); Example example = Example.create(entity); example.excludeZeroes(); Criteria criteria = sessao.createCriteria(entity.getClass()); criteria.add(example); return criteria.list(); }
Example #23
Source File: PersistentResource.java From micro-server with Apache License 2.0 | 5 votes |
@GET @Produces("application/json") @Path("/get") public List<HibernateEntity> get() { final Session session = sessionFactory.openSession(); Criteria criteria = session.createCriteria(HibernateEntity.class) .add(Example.create(HibernateEntity.builder() .name("test") .build())); return criteria.list(); }
Example #24
Source File: GenericBaseCommonDao.java From jeewx with Apache License 2.0 | 5 votes |
/** * 根据实体模版查找 * * @param entityName * @param exampleEntity * @return */ public List findByExample(final String entityName, final Object exampleEntity) { Assert.notNull(exampleEntity, "Example entity must not be null"); Criteria executableCriteria = (entityName != null ? getSession() .createCriteria(entityName) : getSession().createCriteria( exampleEntity.getClass())); executableCriteria.add(Example.create(exampleEntity)); return executableCriteria.list(); }
Example #25
Source File: Main.java From cacheonix-core with GNU Lesser General Public License v2.1 | 5 votes |
/** * Demonstrates query by example */ public void viewAuctionsByDescription(String description, int condition) throws Exception { String msg = "Viewing auctions containing: " + description; if (condition>0) msg += " with condition: " + condition + "/10"; AuctionItem item = new AuctionItem(); item.setDescription(description); item.setCondition(condition); Session s = factory.openSession(); Transaction tx=null; try { tx = s.beginTransaction(); Iterator iter = s.createCriteria(AuctionItem.class) .add( Example.create(item) .enableLike(MatchMode.ANYWHERE) .ignoreCase() .excludeZeroes() ) .list() .iterator(); System.out.println(msg); while ( iter.hasNext() ) { item = (AuctionItem) iter.next(); System.out.println("Item: " + item.getId() + " - " + item.getDescription() ); } System.out.println(); tx.commit(); } catch (Exception e) { if (tx!=null) tx.rollback(); throw e; } finally { s.close(); } }
Example #26
Source File: CriteriaQueryTest.java From cacheonix-core with GNU Lesser General Public License v2.1 | 5 votes |
public void testEscapeCharacter() { Session session = openSession(); Transaction t = session.beginTransaction(); Course c1 = new Course(); c1.setCourseCode( "course-1" ); c1.setDescription( "%1" ); Course c2 = new Course(); c2.setCourseCode( "course-2" ); c2.setDescription( "%2" ); Course c3 = new Course(); c3.setCourseCode( "course-3" ); c3.setDescription( "control" ); session.persist( c1 ); session.persist( c2 ); session.persist( c3 ); session.flush(); session.clear(); // finds all courses which have a description equal to '%1' Course example = new Course(); example.setDescription( "&%1" ); List result = session.createCriteria( Course.class ) .add( Example.create( example ).ignoreCase().enableLike().setEscapeCharacter( new Character( '&' ) ) ) .list(); assertEquals( 1, result.size() ); // finds all courses which contain '%' as the first char in the description example.setDescription( "&%%" ); result = session.createCriteria( Course.class ) .add( Example.create( example ).ignoreCase().enableLike().setEscapeCharacter( new Character( '&' ) ) ) .list(); assertEquals( 2, result.size() ); session.createQuery( "delete Course" ).executeUpdate(); t.commit(); session.close(); }
Example #27
Source File: QueryByExampleTest.java From cacheonix-core with GNU Lesser General Public License v2.1 | 5 votes |
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(); }
Example #28
Source File: UserDAOImpl.java From OnlineAuction with Apache License 2.0 | 5 votes |
public List<Users> search(Users users) { Object execute = super.getHibernateTemplate().execute(new HibernateCallback<Object>() { public Object doInHibernate(Session session) throws HibernateException { Criteria criteria = session.createCriteria(Users.class); if(users != null) criteria.add(Example.create(users)); return criteria.list(); } }); return (List<Users>) execute; }
Example #29
Source File: MasterDetailTest.java From cacheonix-core with GNU Lesser General Public License v2.1 | 4 votes |
public void testExample() throws Exception { Session s = openSession(); Transaction t = s.beginTransaction(); Master m = new Master(); m.setName("name"); m.setX(5); m.setOtherMaster(m); s.save(m); t.commit(); s.close(); s = openSession(); t = s.beginTransaction(); Master m1 = (Master) s.createCriteria(Master.class) .add( Example.create(m).enableLike().ignoreCase().excludeProperty("bigDecimal") ) .uniqueResult(); assertTrue( m1.getOtherMaster()==m1 ); m1 = (Master) s.createCriteria(Master.class) .add( Expression.eq("name", "foobar") ) .uniqueResult(); assertTrue( m1==null ); m1 = (Master) s.createCriteria(Master.class) .add( Example.create(m).excludeProperty("bigDecimal") ) .createCriteria("otherMaster") .add( Example.create(m).excludeZeroes().excludeProperty("bigDecimal") ) .uniqueResult(); assertTrue( m1.getOtherMaster()==m1 ); Master m2 = (Master) s.createCriteria(Master.class) .add( Example.create(m).excludeNone().excludeProperty("bigDecimal") ) .uniqueResult(); assertTrue( m2==m1 ); m.setName(null); m2 = (Master) s.createCriteria(Master.class) .add( Example.create(m).excludeNone().excludeProperty("bigDecimal") ) .uniqueResult(); assertTrue( null==m2 ); if (getDialect() instanceof HSQLDialect) { m1.setOtherMaster(null); s.flush(); } s.delete(m1); t.commit(); s.close(); }
Example #30
Source File: DataService.java From micro-server with Apache License 2.0 | 3 votes |
private List<Entity> searchByName(String name) { final Session session = sf.openSession(); Criteria criteria = session.createCriteria(Entity.class) .add(Example.create(new Entity(name))); return criteria.list(); }