Find Non-Empty Field Values Using Criteria - java

Under EJB I am using criteria. In that i need to fetch non empty field values from database.
For Example
Field name : subject
(i) value 1 : "ABC"
(ii) value 2 : ""
Here I need to fetch only "ABC" alone. How to do this one using hibernate criteria.
Please suggest your solution.. Thanks in advance

Yes as Ritika suggested you can use Restrictions as follows:
crit.add(Restrictions.isNotNull("subject"))

First of all create session object then use the below code
Criteria crit = session.createCriteria(TableName.class);
crit.add(Restrictions.isNotNull("subject"));
List<Object[]> objectList = crit.list();

Related

Update a list of entity using Hibernate in Java

I get a list of entity from database using the code below:
List<NeginHotData> neginHotData = getNeginHotDataByStatus(null);
Then i need to change the Status field of them from NULL to "Active" and that's OK, the problem is that ,Is it possible to do this without iterating through the list of objects and updating them one by one? That doesn't sound efficient to me.
I need a solution to update them completely with each other because i have about 7000 record and iterating through the list of objects and updating them one by one isn't really a good solution .
You can use HQL like this:
String hqlUpdate = "update NeginHotData c set c.status = :status where c.bla= :bla";
int updatedEntities = s.createQuery( hqlUpdate ).setString( "bla", bla).setString( "status", status).executeUpdate();
This works only if you set the same status for all entities involved.

JPA CriteriaBuilder value like column

I'm trying to create a query using CriteriaBuilder where I need to have a predicate where the value of the predicate is like the value in the database.
Basically, I need to be able to do the following:
WHERE myTestValue LIKE columnValue
In native queries, it is an option to do that, but using the CriteriaBuilder or NamedQueries, it does not seem to work.
String myValue = "foo#bar.com";
cb.where(cb.like(myValue, root.get(Entity_.email));
Is there an option in JPA to do it like this? Or should I fall back to native queries?
EDIT
I need to be able to check if a given value matches a wildcard entry in database. So the database has a record %#bar.com%, and I need to check if my given value foo#bar.com matches to that record.
I think your params should be other way round:
cb.where(cb.like(root.get(Entity_.email),myValue);
Aditionally you may need to use add this to the second param:
cb.where(cb.like(root.get(Entity_.email),"%"+myValue+"%");
Chris found the answer. First I need to "generate" a parameter.
ParameterExpression<String> senderEmailParameter = cb.parameter(String.class, "senderEmailParameter");
Path<String> senderEmailPath = root.get(Entity_.senderEmail);
Predicate predEmail = cb.like(senderEmailParameter, senderEmailPath);
And then I need to fill the parameter in the query.
q.where(predEmail);
return em.createQuery(q).setParameter("senderEmailParameter", senderEmail).getSingleResult();
This works! Thanks Chris!

Hibernate Criteria - Max value from a given list

I'm using Hibernate Criteria, I want to select the maximun value for each group of values, this maximun value must be contained in a given list.
Example:
select t.field1, max(t.field2) as total
from table t
where t.field2 in :givenList
group by t.field1
order by total desc;
I've tried doing this:
Criteria criteria = session.createCriteria(Table.class);
DetachedCriteria maxNo = DetachedCriteria.forClass(Table.class);
ProjectionList projection = Projections.projectionList();
projection.add(Projections.groupProperty("id.field1"));
projection.add(Projections.max("id.field2"));
maxNo.setProjection(projection);
criteria.add(Subqueries.propertiesIn(new String[] {"id.field1", "id.field2"}, maxNo));
This code works perfectly but it returns only the max value for each group.
If I try to add other constrains like:
criteria.add(Property.forName("id.field2").in(givenList));
The request doesn't work properly. Basic case, the maximun for the group is 2, and the given list is (0,1), in this case we receive nothing for this group.
I hope you could help me. Thanks in advance!
I've found the solution.
maxNo.add(Restrictions.in("id.field2", givenList));
Actually, I make a mistake placing the Restrictionsin the Criteria. We should place the Restrictions in the DetachedCriteria.
Thank you anyway

How do I limit the result data by using limit in criteria

How do I add 'limit value' in Hibernate criteria.
My code is like this
Criteria criteria = session.createCriteria(Sample.class);
criteria.add(Restrictions.eq("colOne", colOne));
criteria.add(Restrictions.eq("colThree", colThree));
criteria.addOrder(Order.desc("colFour"));
How do i retrieve a single row instead of a list?
You can use setMaxResults:
public Criteria setMaxResults(int maxResults)
Get first element form your list and do cast with your class.
Sample sample= (Sample)ctriteria.list().get(0);

how do I add another search parameter in hibernate mapping?

I am new to hibernate and still learning the basics. I'd appreciate if someone can point me in the right direction.
I have a class:
Destination
id
name
longitude
latitude
I can read destinations based on id with something like this:
List result = session.createQuery("from Destination as d where d.id=2").list();
However, I want to read destinations from database using name. I can perhaps write something like this as a query:
String name; // name set somewhere else, say a function argument
List result = session.createQuery("from Destination as d where d.name LIKE %"+name).list();
I believe this will yield all destinations with names similar to (variable) name.
Is there something inbuilt in hibernate for such use cases or is there a better way to handle this ?
EDIT:
One thing that follows from my thought process is: name column on destination db table will have an index setup. Can I map this index in some way to the Destination class using hibernate ?
You could build your query by concatenating strings. A more elegant solution would be to use the Hibernate Criteria API.
You query would then look something like:
List result = session.createCriteria(Destination.class)
.add(Restrictions.like("name", "%" + name)
.list();

Categories

Resources