Association example request

It is unfortunate that you cannot use QBE for associations.

I have a large amount of data consisting of 8 many-to-one columns. There is a drop-down list for each column to expand the table.

Assume the following:

Table user

User { id, UserStatus, UserAuthorization }

I want to use this code:

Criteria crit = getSession().createCriteria(class);
crit.add(Example.create(userObject));

This does not work in the following example userObject:

User id=1 { UserStatus=Active, UserAuthorization=Admin }

because QBE does not support collections.

One way to solve this problem is to use it as follows:

crit.createCriteria("UserStatus").add(Example.create(userStatusObject));
crit.createCriteria("UserAuthorization").add(Example.create(userAuthorizationObject));

My question is how can this be programmed dynamically only with a given object User. Is there any other way than using QBE?

+5
source share
3 answers

QBE QBE

Criteria crit = getSession().createCriteria(class);
    .add(Example.create(userObject));
    .add(Expression.eq("UserStatus", userObject.getUserStatus()));
+2

, , , :

protected T GetByExample(T example)
{
    var c = DetachedCriteria.For<T>().Add(Example.Create(example).ExcludeNone());
    var props = typeof (T).GetProperties()
        .Where(p => p.PropertyType.GetInterfaces().Contains(typeof(IEntityBase)));
    foreach (var pInfo in props)
    {
        c.Add(Restrictions.Eq(pInfo.Name, pInfo.GetValue(example)));
    }
    return Query(c);
}

, IEntityBase, , . - (.. C.GetExecutableCriteria())

+1

Here is the code that can be used for each entity to use a request using an example in sleep mode.

  /**
                 * This method will use for query by example with association 
                 * @param exampleInstance the persistent class(T) object
                 * @param restrictPropertyName the string object contains the field name of the association
                 * @param restrictPropertyValue the association object 
                 * @return list the persistent class list
                 */
public List<T> queryByExampleWithRestriction(T exampleInstance, String restrictPropertyName, Object restrictPropertyValue) {
            log.info("Inside queryByExampleWithRestriction method of GenericHibernateDAO");
            List<T> list = null;
            try {
                Criteria criteria = getSession().createCriteria(exampleInstance.getClass());  
                Example example =  Example.create(exampleInstance);
                criteria.add(example);
                criteria.add(Restrictions.eq(restrictPropertyName, restrictPropertyValue));
                list = criteria.list();
                log.info("Executed the queryByExampleWithRestriction query with criteria successfully!");
            } catch(HibernateException e){
                throw (e);
            }
            finally{
                this.closeSession();
            }
            return list;  
        }
0
source

All Articles