JPA / Hibernate: CriteriaBuilder - How to create query using relationship object? - java

I have the following four tables:
SCHEDULE_REQUEST TABLE:
ID,
APPLICATION_ID (FK)
APPLICATION TABLE:
ID,
CODE
USER_APPLICATION TABLE:
APPLICATION_ID (FK),
USER_ID (FK)
USER TABLE:
ID,
NAME
Now I wanted to create a CriteriaBuilder where condition is to select ScheduleRequests for specified user Ids.
I have the following codes:
List<User> usersList = getSelectedUsers(); // userList contains users I wanted to select
CriteriaBuilder builder = getJpaTemplate().getEntityManagerFactory().getCriteriaBuilder();
CriteriaQuery<ScheduleRequest> criteria = builder.createQuery(ScheduleRequest.class);
Root<ScheduleRequest> scheduleRequest = criteria.from(ScheduleRequest.class);
criteria = criteria.select(scheduleRequest);
ParameterExpression<User> usersIdsParam = null;
if (usersList != null) {
usersIdsParam = builder.parameter(User.class);
params.add(builder.equal(scheduleRequest.get("application.userApplications.user"), usersIdsParam));
}
criteria = criteria.where(params.toArray(new Predicate[0]));
TypedQuery<ScheduleRequest> query = getJpaTemplate().getEntityManagerFactory().createEntityManager().createQuery(criteria);
// Compile Time Error here:
// The method setParameter(Parameter<T>, T) in the type TypedQuery<ScheduleRequest> is not
// applicable for the arguments (ParameterExpression<User>, List<User>)
query.setParameter(usersIdsParam, usersList);
return query.getResultList();
Can you please help me how to pass query filter to a relationship object?
I think what I did in "application.userApplications.user" is wrong?
Please really need help.
Thank you in advance!

Using the canonical Metamodel and a couple of joins, it should work. Try if you get some hints from the following pseudo-code (not tested):
...
Predicate predicate = cb.disjunction();
if (usersList != null) {
ListJoin<ScheduleRequest, Application> applications = scheduleRequest.join(ScheduleRequest_.applications);
ListJoin<Application, UserApplication> userApplications = applications.join(Application_.userApplications);
Join<UserApplication, User> user = userApplications.join(UserApplication_.userId);
for (String userName : usersList) {
predicate = builder.or(predicate, builder.equal(user.get(User_.name), userName));
}
}
criteria.where(predicate);
...
In order to understand Criteria Queries, have a look at these tutorials:
http://www.ibm.com/developerworks/java/library/j-typesafejpa/
http://docs.oracle.com/javaee/6/tutorial/doc/gjitv.html
The second link should also guide you on how to use Metamodel classes, that should be built automatically by the compiler / IDE.

Related

How can I use JPA do this SQL query?

Hello everyone I try do this query "SELECT SUM(cevecoin) FROM coin where clid="ABC" in JPA but it's no working. Can somebody help me?
public Integer SumCoin(String Clid) {
CriteriaBuilder builder =getSession().getCriteriaBuilder();
CriteriaQuery<CoinBean> criteria =builder.createQuery(CoinBean.class);
Root<CoinBean> root = criteria.from(CoinBean.class);
criteria.select(builder.sum(root.<Integer>get("clid")).as(CoinBean.class)).where(builder.equal(root.get("clid"),Clid));
return getSession().createQuery(criteria).getSingleResult().getCevecoin();
}
try this:
CriteriaBuilder builder =getSession().getCriteriaBuilder();
// the type of query criteria must correspond to the result we want to obtain
CriteriaQuery<BigDecimal> criteria = builder.createQuery(BigDecimal.class);
Root<CoinBean> root = criteria.from(CoinBean.class);
// use multiselect and sum the field cevecoin
criteria.multiselect(builder.sum(root.get("cevecoin")))
criteria.where(builder.equal(root.get("clid"),Clid));
return getSession().createQuery(criteria).getSingleResult();

Syntax where clause in consultation with criteria

I need to put a where clause in my query, but I'm not hitting the correct syntax.
/*
* Support listing and POSTing back Origem entities (e.g. from inside an
* HtmlSelectOneMenu)
*/
public List<Origem> getAll()
{
CriteriaBuilder cb = this.entityManager.getCriteriaBuilder();
CriteriaQuery<Origem> criteria = cb.createQuery(Origem.class);
Root<Origem> root = criteria.from(Origem.class);
return this.entityManager.createQuery(
criteria.select(root).where())
.getResultList();
}
In my where clause want to bring all "origem" with id equal to the logged in user.
Something like this:
Ex: select * from origin where origem.id = loginBean.origem.getId
Simply use criteria.add(Restrictions.eq()) to your criteria and I think if the id is unique you need to use .uniqueResult() to get the wanted result from your criteria, your code should be like this:
CriteriaBuilder cb = this.entityManager.getCriteriaBuilder();
Criteria cr = cb.createCriteria(Origem.class);
// add the restriction here
cr.add(Restrictions.eq("id", loginBean.origem.getId));
Origem root = (Origem) cr.uniqueResult();
Use criteria.addRestrictions(Restrictions.eq("propertyName","propertyValue");
public List getAll(long idFromLoginBeanOrigin)
{
CriteriaBuilder cb = this.entityManager.getCriteriaBuilder();
CriteriaQuery<Origem> criteria = cb.createQuery(Origem.class);
criteria.add(Restrictions.eq("origem.id", idFromLoginBeanOrigin));
Root<Origem> root = criteria.from(Origem.class);
return this.entityManager.createQuery(
criteria.select(root).where())
.getResultList();
}
I type it now do maybe have a typo, please check !

how to add Hibernate "createAlias" Child Restriction to parent Entity Restriction

I've got a requirment where I need to apply a search keyword to name and and state of Parent entity (Customer) and also name attribute of child entity Order.
List<Criterion> aliasRestrictionsCriterion = new ArrayList<Criterion>();
Junction quotedLikeRestrictions = Restrictions.disjunction();
quotedLikeRestrictions.add(Restrictions.ilike("customerName", token, MatchMode.ANYWHERE));
quotedLikeRestrictions.add(Restrictions.ilike("customerState", token, MatchMode.ANYWHERE));
restrictions.add(quotedLikeRestrictions);
and to include the child table in the query I'm am doing the following
Criteria alias = criteria.createAlias("order", "order")
.add(Restrictions.ilike("order.name", token, MatchMode.ANYWHERE));
Hibernate is generating the following query but what I need is to have order.name like comparison get added to the parent block.
CUSTOMER this_ inner join ORDER order_ on this_.ORDER_ID=order_.ORDER_ID where
(lcase(this_.name) like ? or lcase(this_.state) like ?) and (lcase(order_.NAME)
like ?)
what I want is to have (lcase(order_.NAME) like ?) inside the parent block. So I want is the following
(lcase(this_.name) like ? or lcase(this_.state) like ? or lcase(order_.NAME) like ?)
How can I achieve this ?
Update: this is how I'm calling the criteria
protected List getEntities(List<Criterion> restrictions, String aliasSearchToken) {
Session session = currentSession();
Criteria criteria = session.createCriteria(getEntityClass());
// apply restrictions.
if (restrictions != null) {
for (Criterion criterion : restrictions) {
criteria.add(criterion);
}
}
Criteria alias = criteria.createAlias("order", "order").add(Restrictions.ilike("order.name", searchToken, MatchMode.ANYWHERE));
List list = criteria.list();
if (list != null) {
return list;
}
return Collections.emptyList();
}
Update 2:
protected List getEntities(List<Criterion> restrictions, String aliasSearchToken) {
Session session = currentSession();
Criteria criteria = session.createCriteria(getEntityClass());
Junction or = Restrictions.disjunction();
or.add(Restrictions.ilike("order.name", searchToken, MatchMode.ANYWHERE));
criteria.createAlias("order", "order").add(or);
// apply restrictions.
if (restrictions != null) {
for (Criterion criterion : restrictions) {
criteria.add(criterion);
}
}
List list = criteria.list();
if (list != null) {
return list;
}
return Collections.emptyList();
}
produced
select ....... from CUSTOMER this_ inner join ORDER order_ on this_.ORDER_ID=order_.uid where (lcase(order_.NAME) like ?) and (lcase(this_.name) like ? or lcase(this_.state) like ?)
the orderName is just before but still not in the second or statement ... I'm so confused.
Well, instea of adding the restriction to the Criteria (which creates an AND), you must add it to the quotedLikeRestrictions disjunction:
quotedLikeRestrictions.add(Restrictions.ilike("order.name", token, MatchMode.ANYWHERE));

Hibernate get List from database

In the following code I am trying to get a List of Products which contains all the products in the database:
public List<Products> getAllProducts() throws Exception{
try{
List<Products> products ;
org.hibernate.Transaction tx = session.beginTransaction();
products = session.createSQLQuery("SELECT * FROM Products").list();
if(products.size() > 0)
{
return products;
}
return null;
}
catch(Exception e)
{
throw e;
}
}
however this exception is thrown:
[Ljava.lang.Object; cannot be cast to mediatek.Products
List<Products> list = session.createCriteria(Products.class).list();
This will give you all the records of products table from database
Your answer not only adds a cast, but switches from SQL to HQL. Since your 2nd query is in HQL, Hibernate is able to use mapping information to know what class to return. This is the preferred way to do things in Hibernate, but if you had to use SQL for some reason you could achieve the same thing with:
(List<Products>)session.createSQLQuery("SELECT * FROM Products").addEntity(Products.class).list();
In Hibernate 5 the session.createCriteria methods are deprecated.
You will need to use a CriteriaBuilder and query from there to get a generic list of Products instead of just List.
Imports
import javax.persistence.criteria.CriteriaBuilder;
import javax.persistence.criteria.CriteriaQuery;
Code
CriteriaBuilder builder = session.getCriteriaBuilder();
CriteriaQuery<Products> criteria = builder.createQuery(Products.class);
criteria.from(Products.class);
List<Products> products = session.createQuery(criteria).getResultList();
Forgot to type cast the query. it is working now.
List<Products> products = (List<Products>) session.createQuery("from Products").list();
For example you have code:
Session session = getSessionFactory().openSession();
Transaction transaction = null;
try {
SQLQuery sqlQuery = session.createSQLQuery("SELECT * FROM schema.yourtable WHERE param = :param");
sqlQuery.setString("param", "someParam");
And if your next step will be:
List list = sqlQuery.list();
You will receive list with Rows. You can see your Entity.class parameters in debug, but cat cast to List with your Entities:
List<Entity> list = (List<Entity>) sqlQuery.list();
In this point will be ClassCastException!
And if you need received List with your Entities you must add entity type to sql query:
List<Entity> list = (List<Entity>)sqlQuery.addEntity(Entity.class).list();
That's all. I hope someone will help.
if you using sql query, you should add this line at the last of the query to get the list you want:
.setResultTransformer(Transformers.aliasToBean(testDTO.class)).list();

Hibernate retrieve results from database based on condition

I am a bit lost when it comes to retrieving results from the database.
My MemberModel consists of 4 fields: id, username, password and email. I have been able to successfully save it to database.
Now I need to retrieve an id of a member who's username equals "Test".
I tried something along the lines:
SQLQuery query = session.createSQLQuery("SELECT id FROM members WHERE username = :username");
query.setString("username", username);
List<MemberModel> returnedMembers = query.list();
MemberModel member = returnedMembers.get(0);
int id = member.getId();
However I get an error that member.getId() cannot be converted to int, since it is MemberModel... But the getter getId() returns int.
I am quite confused. The question is: what would be the easiest and fastes way to retrieve member id based on condition (value of username)?
You are using a native SQL query, but should use HQL query. That means you have to change the query to:
session.createQuery("SELECT m FROM MemberModel m WHERE m.username = :username")
I would change your code into something like this:
public MemberModel getMember(String username) {
Query query = sessionFactory.getCurrentSession().createQuery("from " + MemberModel.class.getName() + " where username = :username ");
query.setParameter("username", username);
return (MemberModel) query.uniqueResult();
}
Then you should be able to do:
MemberModel model = someInstance.getMember("someUsername");
int id = model.getId();
You can also use criteria and restrictions api.
Criteria criteria = session.createCriteria(MemberModel.class);
criteria.add(Restrictions.eq("username", username));
MemberModel member=(MemberModel)criteria.uniqueResult();

Categories