org.hibernate.criterion.Example类的使用及代码示例

x33g5p2x  于2022-01-19 转载在 其他  
字(9.5k)|赞(0)|评价(0)|浏览(178)

本文整理了Java中org.hibernate.criterion.Example类的一些代码示例,展示了Example类的具体用法。这些代码示例主要来源于Github/Stackoverflow/Maven等平台,是从一些精选项目中提取出来的代码,具有较强的参考意义,能在一定程度帮忙到你。Example类的具体详情如下:
包路径:org.hibernate.criterion.Example
类名称:Example

Example介绍

[英]Support for query by example.

List results = session.createCriteria(Parent.class) 
.add( Example.create(parent).ignoreCase() ) 
.createCriteria("child") 
.add( Example.create( parent.getChild() ) ) 
.list();

"Examples" may be mixed and matched with "Expressions" in the same Criteria.
[中]支持通过示例进行查询

List results = session.createCriteria(Parent.class) 
.add( Example.create(parent).ignoreCase() ) 
.createCriteria("child") 
.add( Example.create( parent.getChild() ) ) 
.list();

“示例”可能与相同条件下的“表达式”混合匹配。

代码示例

代码示例来源:origin: hibernate/hibernate-orm

t = s.beginTransaction();
Master m1 = (Master) s.createCriteria(Master.class)
  .add( Example.create(m).enableLike().ignoreCase().excludeProperty("bigDecimal") )
  .uniqueResult();
assertTrue( m1.getOtherMaster()==m1 );
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 );

代码示例来源:origin: hibernate/hibernate-orm

/**
 * Exclude zero-valued properties.
 *
 * Equivalent to calling {@link #setPropertySelector} passing in {@link NotNullOrZeroPropertySelector#INSTANCE}
 *
 * @return {@code this}, for method chaining
 *
 * @see #setPropertySelector
 */
public Example excludeZeroes() {
  setPropertySelector( NotNullOrZeroPropertySelector.INSTANCE );
  return this;
}

代码示例来源:origin: hibernate/hibernate-orm

/**
 * Create a new Example criterion instance, which includes all non-null properties by default
 *
 * @param exampleEntity The example bean to use.
 *
 * @return a new instance of Example
 */
public static Example create(Object exampleEntity) {
  if ( exampleEntity == null ) {
    throw new NullPointerException( "null example entity" );
  }
  return new Example( exampleEntity, NotNullPropertySelector.INSTANCE );
}

代码示例来源:origin: hibernate/hibernate-orm

@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();
}

代码示例来源:origin: hibernate/hibernate-orm

@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();
}

代码示例来源:origin: spring-projects/spring-framework

@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();
  }));
}

代码示例来源:origin: org.beangle.commons/beangle-commons-orm

public static Example getExampleCriterion(Object entity, String[] excludePropertes, MatchMode mode) {
 Example example = Example.create(entity).setPropertySelector(new NotEmptyPropertySelector());
 if (null != mode) {
  example.enableLike(mode);
 }
 if (null != excludePropertes) {
  for (int i = 0; i < excludePropertes.length; i++) {
   example.excludeProperty(excludePropertes[i]);
  }
 }
 return example;
}

代码示例来源:origin: de.alpharogroup/dao.api

/**
 * {@inheritDoc}
 */
@SuppressWarnings("unchecked")
public List<T> findByExample(final T exampleInstance,
    final String... excludeProperty) {
  Criteria crit = getSession().createCriteria(getPersistentClass());
  Example example = Example.create(exampleInstance);
  for (String exclude : excludeProperty) {
    example.excludeProperty(exclude);
  }
  crit.add(example);
  return crit.list();
}

代码示例来源:origin: ro.fortsoft/generic-data-dao

Example example = Example.create(filter);
if (qp.getFilterState() == QueryParameter.FILTER_STATE_IGNORECASE_ENABLELIKE) {
  list.add(example.enableLike(getMatchMode(qp)).ignoreCase());
} else if (qp.getFilterState() == QueryParameter.FILTER_STATE_IGNORECASE_NOENABLELIKE) {
  list.add(example.ignoreCase());
} else if (qp.getFilterState() == QueryParameter.FILTER_STATE_NOIGNORECASE_ENABLELIKE) {
  list.add(example.enableLike(getMatchMode(qp)));
} else {
  list.add(example);
  example.setEscapeCharacter(QueryParameter.ESCAPE_CHARACTER);
  example = example.excludeNone();
  for (final String property : qp.getExcludedProperties()) {
    example = example.excludeProperty(property);
list.add(Example.create(filter));

代码示例来源:origin: org.grails/grails-hibernate

public Object doInHibernate(Session session) throws HibernateException, SQLException {
    Example example = Example.create(arg).ignoreCase();
    Criteria crit = session.createCriteria(clazz);
    getHibernateTemplate().applySettings(crit);
    crit.add(example);
    Map argsMap = (arguments.length > 1 && (arguments[1] instanceof Map)) ? (Map) arguments[1] : Collections.EMPTY_MAP;
    GrailsHibernateUtil.populateArgumentsForCriteria(application,clazz, crit, argsMap);
    return crit.list();
  }
});

代码示例来源:origin: hibernate/hibernate-orm

/**
 * Use the "like" operator for all string-valued properties.  This form implicitly uses {@link MatchMode#EXACT}
 *
 * @return {@code this}, for method chaining
 */
public Example enableLike() {
  return enableLike( MatchMode.EXACT );
}

代码示例来源:origin: nilzao/soapbox-race

@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();
}

代码示例来源:origin: stackoverflow.com

Example ex = new Example();
ex.setUp();
ex.testExample();
ex.isElementPresent();
ex.tearDown();

代码示例来源:origin: org.fornax.cartridges/fornax-cartridges-sculptor-framework

@Override
public void performExecute() throws PersistenceException {
  Example example = createExample();
  if (excludeProperties != null) {
    for (int i = 0; i < excludeProperties.length; i++) {
      example.excludeProperty(excludeProperties[i]);
    }
  }
  DetachedCriteria crit = createCriteria(example);
  if (orderBy != null) {
    if (orderByAsc) {
      crit.addOrder(Order.asc(orderBy));
    } else {
      crit.addOrder(Order.desc(orderBy));
    }
  }
  Session hibernateSession = HibernateSessionHelper.getHibernateSession(getEntityManager());
  final Criteria criteria = crit.getExecutableCriteria(hibernateSession);
  prepareCache(criteria);
  result = executeFind(criteria);
}

代码示例来源:origin: stackoverflow.com

public void testRemoveInner() {
  Example.Inner[] inner = new Example.Inner(45);

  Example e = new Example();
  e.addInner(inner);
  e.addInner(inner);
  e.removeInner(inner);
  assertEquals(0, e.getInners().size());
}

代码示例来源:origin: stackoverflow.com

JSONParser parser = new JSONParser();
 ArrayList list = parser.parse(ArrayList.class, json);
 List<Example> result = new ArrayList<Example>();
 for(int i = 0 ; i < list.size() ; i++){
   HashMap<String, String> map = (HashMap) list.get(i);
   Example example = new Example();
   example.setFoo(map.get("foo"));
   example.setBar(map.get("bar"));
   example.setFubar(map.get("fubar"));
   result.add(example);
 }

代码示例来源:origin: stackoverflow.com

Example e = new Example();

// do lots of stuff

e.setId(12345L);
e.build();

// at this point, e is immutable

代码示例来源:origin: stackoverflow.com

Example e1 = new Example();
Example e2 = new Example();

e2.setNumber(3);
e1.setNumber(5);

System.out.println(e2.getNumber()); // surprise! prints 5,

代码示例来源:origin: stackoverflow.com

String a = "Hello";
String b = "World";
// For the duration of the constructor, a becomes firstName,
// and b becomes secondName
Example e = new Example(a, b);
// Once the constructor exists, firstName and secondName disappear.
// Resetting a and b after the call is no longer relevant
a = "Quick brown fox jumps";
b = "over the lazy dog";
System.out.println(e.getName()+" "+e.getSurname());

代码示例来源:origin: stackoverflow.com

public static void main(String[] args) {
  Example sharedData = new Example();
  for (int i = 0; i < 1000; i++)
    sharedData.increment();
  System.out.println("Incrementer finished");
  for (int i = 0; i < 1000; i++)
    sharedData.decrement();
  System.out.println("Decrementer finished");
  System.out.println(sharedData.count);
}

相关文章