I have a chunk of java code which hard codes a hibernate disjunction query that looks like this
session = HibernateUtils.beginTransaction("outpatient");
Criteria criteria = session.createCriteria(AugmentToken.class);
session.beginTransaction();
if (type == Constants.ICD9CPT) {
criteria.add(Restrictions.disjunction()
.add(Restrictions.eq("codeType", "d"))
.add(Restrictions.eq("codeType", "p"))
.add(Restrictions.eq("codeType", "c")));
} else if (type == Constants.EM) {
criteria.add(Restrictions.disjunction()
.add(Restrictions.eq("codeType", "eros"))
.add(Restrictions.eq("codeType", "ehpi"))
.add(Restrictions.eq("codeType", "epe")));
}
But this is not very elegant code. What I would like to do is pass an array of codetypes to a method, and dynamically construct the dijunction criteria. Every website I look at provides examples of disjunctive queries that look like the above, but this will not work for me because I don't want to hard code the construction of the restriction for the criteria since the number of code types can vary.
How do I do this?
Thank you,
Elliott
I think I figured this out. You create the disjunction as a variable, then sequentially add to it.
Specifically:
String [] codeTypes = new String[3];
codeTyes[0]="d";
codeTypes[1]="p";
codetypes[2]="c";
/* note the above would normally be passed into the method containing the code below */
Criteria criteria = session.createCriteria(AugmentToken.class);
session.beginTransaction();
Disjunction disjunction = Restrictions.disjunction();
for (int x = 0; x < codeTypes.length; x++ ) {
disjucntion.add(Restrictions.eq("codeType",codeTypes[x]);
}
criteria.add(disjunction);
I found the answer in Beginning Hibernate on page 214. The book is accessible from books.google.com.
Related
I implemented a hibernate query and would like to assign the result to one of my class variables.
The problem is that the results of hibernate queries seem to be objects or something, as the syso of a result looks very strange:
[exercise.java.basics.storage.WarehouseProduct#77f6d2e3]
This is the method executing the query:
public void updateStock() {
Session session = getSessionFactory().getCurrentSession();
Criteria criteriaNail = session.createCriteria( WarehouseProduct.class );
criteriaNail.add( Restrictions.like( "productName", String.valueOf( Product.NAIL ) ) );
List nailCountResult = criteriaNail.list();
System.out.println( nailCountResult.toString() );
}
The database has only 2 colums and the value I need is in the second.
What I would like to do is something like this:
this.nailCount = nailCountResult.[XYZ --> Get the value from the second column];
Is something like this possible? How can I cast these result objects to something readable?
best regards
daZza
First of all I suggest to change the line to
List<WarehouseProduct> nailCountResult = criteriaNail.list();
And now it is not a ResultSet, it's a list of WarehouseProduct Objects.
You can access each object with index.
You can loop over the result list and see them like
for( WarehouseProduct wp : nailCountResult ) {
System.out.println( wp.nailCount);
}
As a side note, you are breaking encapsulation here. Please look in to it.
All you need to to is this
String value=nailCountResult.get(0).getXXXX();
I have a criteria query that works just fine.
Criteria criteria = getSession().createCriteria(getPersistentClass());
criteria.add(Restrictions.eq("employerId",employerId))
.setFetchMode("card", FetchMode.JOIN)
.createCriteria("card")
.addOrder(Order.desc("cardId"))
.createCriteria("salary")
.add(Restrictions.eq("salaryType",SalaryIdentifierType.CONTRACTOR))
.add(Restrictions.eq("active","YES"));
Now, i need to add another filter(condition) to this criteria. The new filter is joinDate. If the joinDate value has been passed from the Frontend, i will have to add it to this query or else, the filter joinDate should not be added.
I have managed to do this using a disjunction(I know, its weird...but AFAIK, only disjunction gives the the facility to add the filter at the runtime, only it if is present). I did it like this, but i do not want to use a disjunction here and I am looking for other options.
Criteria criteria = getSession().createCriteria(getPersistentClass());
-------------------------------------------------------------------------
Disjunction disjunctionDate = Restrictions.disjunction();
if(utilDate!=null){
disjunctionDate = (Disjunction) disjunctionDate.add(Restrictions.ge("startDate", utilDate));
}
-----------------------------------------------------------------------
criteria.add(Restrictions.eq("employerId",employerId))
.setFetchMode("card", FetchMode.JOIN)
.createCriteria("card")
.addOrder(Order.desc("cardId"))
.createCriteria("salary")
.add(Restrictions.eq("salaryType",SalaryIdentifierType.CONTRACTOR))
.add(Restrictions.eq("active","YES"))
.add(disjunctionDate);
I have tried just using if condition, like this but does not work(To even make this compile, i had to modify the query format from above to having criteria in every line and semicolon in every line).
Criteria criteria = getSession().createCriteria(getPersistentClass());
criteria.add(Restrictions.eq("employerId",employerId));
criteria.setFetchMode("card", FetchMode.JOIN);
criteria.createCriteria("card");
criteria.addOrder(Order.desc("cardId"));
criteria.createCriteria("salary");
criteria.add(Restrictions.eq("salaryType",SalaryIdentifierType.CONTRACTOR));
criteria.add(Restrictions.eq("active","YES"));
if(startDate!=null){
criteria.add(Restrictions.ge("startDate",startDate));
}
Any ideas on how to make this work with better options than using disjunction?
So you are saying this is not working for you?
Criteria criteria = getSession().createCriteria(getPersistentClass())
.add(Restrictions.eq("employerId",employerId))
.setFetchMode("card", FetchMode.JOIN)
.createCriteria("card")
.addOrder(Order.desc("cardId"))
.createCriteria("salary", "sl") // <- alias for salary
.add(Restrictions.eq("sl.salaryType",SalaryIdentifierType.CONTRACTOR))
.add(Restrictions.eq("sl.active","YES"));
if(startDate!=null) {
criteria.add(Restrictions.ge("sl.startDate",startDate));
}
See http://docs.jboss.org/hibernate/orm/4.1/manual/en-US/html_single/#querycriteria-associations
I have a problem with using Conjunctions and Disjunctions. My program receives an arbitrary number of filter elements (each representing a Criterion) from the ui and is intended to chain together them as an AND or OR.
So for example I can have 3 elements like this (I represent a Criterion with a letter):
a OR b AND c
My code looks like this:
// ...
Criteria rootCriteria = createCriteria(entityClass);
Conjunction conjunction = Restrictions.conjunction();
Disjunction disjunction = Restrictions.disjunction();
boolean isFirst = true;
for (InternalFilterElement element : elements) {
if (isFirst) {
isFirst = false;
rootCriteria.add(createCriterion(element.getFilterRelation(), element.getValue()));
} else if (InternalFilterOperand.AND.equals(element.getInternalFilterOperand())) {
addCriterionToJunction(conjunction, element);
} else {
addCriterionToJunction(disjunction, element);
}
}
rootCriteria.add(disjunction);
rootCriteria.add(conjunction);
// ...
My problem is that I always get a AND b AND c and some unnecessary parentheses.
What I really wish to know is that I am using the wrong tool for this task or not? How can I mix AND and OR operators?
edit
InternalFilterOperand is basically an enum containing OR and AND
addCriterionToJunction just adds a Criterion to the Junction based on the relation (<, >, ...) and the value. It does not have any side effects.
By using the following code, you mix AND and OR operators in Hibernate:
Criteria rootCriteria = createCriteria(entityClass);
rootCriteria.add(Restrictions.or(
Restrictions.eq("a","a"),
Restrictions.and(
Restrictions.eq("b","b"),
Restrictions.eq("c","c")
)
));
This example results in the following output: a=a OR b=b AND c=c without the parenthesis you would get with conjunction and disjunction.
You're making it harder than necessary. Why not just use the following:
Junction junction =
InternalFilterOperand.AND.equals(element.getInternalFilterOperand()) ?
Restrictions.conjunction() :
Restrictions.disjunction();
for (InternalFilterElement element : elements) {
addCriterionToJunction(junction, element);
}
rootCriteria.add(junction);
I am close to believe that InternalFilterOperand is not set correctly in your elements as everything else looks right.
Please print/debug the element.getInternalFilterOperand() values in your loop as first statement to verify and correct.
EDIT: Try adding conjunction/disjunction directly in the rootCriteria
for (InternalFilterElement element : elements) {
if (isFirst) {
isFirst = false;
rootCriteria.add(
createCriterion(element.getFilterRelation(), element.getValue()));
}else if (InternalFilterOperand.AND.equals(element.getInternalFilterOperand())){
//add debugg/sys out: adding conjunction
System.out.println("adding conjunction");
rootCriteria.add(Restrictions.conjunction().add(element));
} else {
//add debugg/sys out: adding disjunction
System.out.println("adding disjunction");
rootCriteria.add(Restrictions.disjunction().add(element));
}
}
I like the idea of Named Queries in JPA for static queries I'm going to do, but I often want to get the count result for the query as well as a result list from some subset of the query. I'd rather not write two nearly identical NamedQueries. Ideally, what I'd like to have is something like:
#NamedQuery(name = "getAccounts", query = "SELECT a FROM Account")
.
.
Query q = em.createNamedQuery("getAccounts");
List r = q.setFirstResult(s).setMaxResults(m).getResultList();
int count = q.getCount();
So let's say m is 10, s is 0 and there are 400 rows in Account. I would expect r to have a list of 10 items in it, but I'd want to know there are 400 rows total. I could write a second #NamedQuery:
#NamedQuery(name = "getAccountCount", query = "SELECT COUNT(a) FROM Account")
but it seems a DRY violation to do that if I'm always just going to want the count. In this simple case it is easy to keep the two in sync, but if the query changes, it seems less than ideal that I have to update both #NamedQueries to keep the values in line.
A common use case here would be fetching some subset of the items, but needing some way of indicating total count ("Displaying 1-10 of 400").
So the solution I ended up using was to create two #NamedQuerys, one for the result set and one for the count, but capturing the base query in a static string to maintain DRY and ensure that both queries remain consistent. So for the above, I'd have something like:
#NamedQuery(name = "getAccounts", query = "SELECT a" + accountQuery)
#NamedQuery(name = "getAccounts.count", query = "SELECT COUNT(a)" + accountQuery)
.
static final String accountQuery = " FROM Account";
.
Query q = em.createNamedQuery("getAccounts");
List r = q.setFirstResult(s).setMaxResults(m).getResultList();
int count = ((Long)em.createNamedQuery("getAccounts.count").getSingleResult()).intValue();
Obviously, with this example, the query body is trivial and this is overkill. But with much more complex queries, you end up with a single definition of the query body and can ensure you have the two queries in sync. You also get the advantage that the queries are precompiled and at least with Eclipselink, you get validation at startup time instead of when you call the query.
By doing consistent naming between the two queries, it is possible to wrap the body of the code to run both sets just by basing the base name of the query.
Using setFirstResult/setMaxResults do not return a subset of a result set, the query hasn't even been run when you call these methods, they affect the generated SELECT query that will be executed when calling getResultList. If you want to get the total records count, you'll have to SELECT COUNT your entities in a separate query (typically before to paginate).
For a complete example, check out Pagination of Data Sets in a Sample Application using JSF, Catalog Facade Stateless Session, and Java Persistence APIs.
oh well you can use introspection to get named queries annotations like:
String getNamedQueryCode(Class<? extends Object> clazz, String namedQueryKey) {
NamedQueries namedQueriesAnnotation = clazz.getAnnotation(NamedQueries.class);
NamedQuery[] namedQueryAnnotations = namedQueriesAnnotation.value();
String code = null;
for (NamedQuery namedQuery : namedQueryAnnotations) {
if (namedQuery.name().equals(namedQueryKey)) {
code = namedQuery.query();
break;
}
}
if (code == null) {
if (clazz.getSuperclass().getAnnotation(MappedSuperclass.class) != null) {
code = getNamedQueryCode(clazz.getSuperclass(), namedQueryKey);
}
}
//if not found
return code;
}
I'm trying to implement paging using row-based limiting (for example: setFirstResult(5) and setMaxResults(10)) on a Hibernate Criteria query that has joins to other tables.
Understandably, data is getting cut off randomly; and the reason for that is explained here.
As a solution, the page suggests using a "second sql select" instead of a join.
How can I convert my existing criteria query (which has joins using createAlias()) to use a nested select instead?
You can achieve the desired result by requesting a list of distinct ids instead of a list of distinct hydrated objects.
Simply add this to your criteria:
criteria.setProjection(Projections.distinct(Projections.property("id")));
Now you'll get the correct number of results according to your row-based limiting. The reason this works is because the projection will perform the distinctness check as part of the sql query, instead of what a ResultTransformer does which is to filter the results for distinctness after the sql query has been performed.
Worth noting is that instead of getting a list of objects, you will now get a list of ids, which you can use to hydrate objects from hibernate later.
I am using this one with my code.
Simply add this to your criteria:
criteria.setResultTransformer(Criteria.DISTINCT_ROOT_ENTITY);
that code will be like the select distinct * from table of the native sql.
A slight improvement building on FishBoy's suggestion.
It is possible to do this kind of query in one hit, rather than in two separate stages. i.e. the single query below will page distinct results correctly, and also return entities instead of just IDs.
Simply use a DetachedCriteria with an id projection as a subquery, and then add paging values on the main Criteria object.
It will look something like this:
DetachedCriteria idsOnlyCriteria = DetachedCriteria.forClass(MyClass.class);
//add other joins and query params here
idsOnlyCriteria.setProjection(Projections.distinct(Projections.id()));
Criteria criteria = getSession().createCriteria(myClass);
criteria.add(Subqueries.propertyIn("id", idsOnlyCriteria));
criteria.setFirstResult(0).setMaxResults(50);
return criteria.list();
A small improvement to #FishBoy's suggestion is to use the id projection, so you don't have to hard-code the identifier property name.
criteria.setProjection(Projections.distinct(Projections.id()));
The solution:
criteria.setResultTransformer(Criteria.DISTINCT_ROOT_ENTITY);
works very well.
session = (Session) getEntityManager().getDelegate();
Criteria criteria = session.createCriteria(ComputedProdDaily.class);
ProjectionList projList = Projections.projectionList();
projList.add(Projections.property("user.id"), "userid");
projList.add(Projections.property("loanState"), "state");
criteria.setProjection(Projections.distinct(projList));
criteria.add(Restrictions.isNotNull("this.loanState"));
criteria.setResultTransformer(Transformers.aliasToBean(UserStateTransformer.class));
This helped me :D
if you want to use ORDER BY, just add:
criteria.setProjection(
Projections.distinct(
Projections.projectionList()
.add(Projections.id())
.add(Projections.property("the property that you want to ordered by"))
)
);
I will now explain a different solution, where you can use the normal query and pagination method without having the problem of possibly duplicates or suppressed items.
This Solution has the advance that it is:
faster than the PK id solution mentioned in this article
preserves the Ordering and don’t use the 'in clause' on a possibly large Dataset of PK’s
The complete Article can be found on my blog
Hibernate gives the possibility to define the association fetching method not only at design time but also at runtime by a query execution. So we use this aproach in conjunction with a simple relfection stuff and can also automate the process of changing the query property fetching algorithm only for collection properties.
First we create a method which resolves all collection properties from the Entity Class:
public static List<String> resolveCollectionProperties(Class<?> type) {
List<String> ret = new ArrayList<String>();
try {
BeanInfo beanInfo = Introspector.getBeanInfo(type);
for (PropertyDescriptor pd : beanInfo.getPropertyDescriptors()) {
if (Collection.class.isAssignableFrom(pd.getPropertyType()))
ret.add(pd.getName());
}
} catch (IntrospectionException e) {
e.printStackTrace();
}
return ret;
}
After doing that you can use this little helper method do advise your criteria object to change the FetchMode to SELECT on that query.
Criteria criteria = …
// … add your expression here …
// set fetchmode for every Collection Property to SELECT
for (String property : ReflectUtil.resolveCollectionProperties(YourEntity.class)) {
criteria.setFetchMode(property, org.hibernate.FetchMode.SELECT);
}
criteria.setFirstResult(firstResult);
criteria.setMaxResults(maxResults);
criteria.list();
Doing that is different from define the FetchMode of your entities at design time. So you can use the normal join association fetching on paging algorithms in you UI, because this is most of the time not the critical part and it is more important to have your results as quick as possible.
Below is the way we can do Multiple projection to perform Distinct
package org.hibernate.criterion;
import org.hibernate.Criteria;
import org.hibernate.Hibernate;
import org.hibernate.HibernateException;
import org.hibernate.type.Type;
/**
* A count for style : count (distinct (a || b || c))
*/
public class MultipleCountProjection extends AggregateProjection {
private boolean distinct;
protected MultipleCountProjection(String prop) {
super("count", prop);
}
public String toString() {
if(distinct) {
return "distinct " + super.toString();
} else {
return super.toString();
}
}
public Type[] getTypes(Criteria criteria, CriteriaQuery criteriaQuery)
throws HibernateException {
return new Type[] { Hibernate.INTEGER };
}
public String toSqlString(Criteria criteria, int position, CriteriaQuery criteriaQuery)
throws HibernateException {
StringBuffer buf = new StringBuffer();
buf.append("count(");
if (distinct) buf.append("distinct ");
String[] properties = propertyName.split(";");
for (int i = 0; i < properties.length; i++) {
buf.append( criteriaQuery.getColumn(criteria, properties[i]) );
if(i != properties.length - 1)
buf.append(" || ");
}
buf.append(") as y");
buf.append(position);
buf.append('_');
return buf.toString();
}
public MultipleCountProjection setDistinct() {
distinct = true;
return this;
}
}
ExtraProjections.java
package org.hibernate.criterion;
public final class ExtraProjections
{
public static MultipleCountProjection countMultipleDistinct(String propertyNames) {
return new MultipleCountProjection(propertyNames).setDistinct();
}
}
Sample Usage:
String propertyNames = "titleName;titleDescr;titleVersion"
criteria countCriteria = ....
countCriteria.setProjection(ExtraProjections.countMultipleDistinct(propertyNames);
Referenced from https://forum.hibernate.org/viewtopic.php?t=964506
NullPointerException in some cases!
Without criteria.setProjection(Projections.distinct(Projections.property("id")))
all query goes well!
This solution is bad!
Another way is use SQLQuery. In my case following code works fine:
List result = getSession().createSQLQuery(
"SELECT distinct u.id as usrId, b.currentBillingAccountType as oldUser_type,"
+ " r.accountTypeWhenRegister as newUser_type, count(r.accountTypeWhenRegister) as numOfRegUsers"
+ " FROM recommendations r, users u, billing_accounts b WHERE "
+ " r.user_fk = u.id and"
+ " b.user_fk = u.id and"
+ " r.activated = true and"
+ " r.audit_CD > :monthAgo and"
+ " r.bonusExceeded is null and"
+ " group by u.id, r.accountTypeWhenRegister")
.addScalar("usrId", Hibernate.LONG)
.addScalar("oldUser_type", Hibernate.INTEGER)
.addScalar("newUser_type", Hibernate.INTEGER)
.addScalar("numOfRegUsers", Hibernate.BIG_INTEGER)
.setParameter("monthAgo", monthAgo)
.setMaxResults(20)
.list();
Distinction is done in data base! In opposite to:
criteria.setResultTransformer(Criteria.DISTINCT_ROOT_ENTITY);
where distinction is done in memory, after load entities!