I'm trying to use Hibernate Criteria for a select using tables related in a many-to-many relation. The n-m table has some additional columns not just the id's from each of the tables.
CriteriaBuilder builder = em.getCriteriaBuilder();
CriteriaQuery<User> criteria = builder.createQuery(Fornecedor.class);
Root<User> root = criteria.from(User.class );
criteria.select(root);
root.fetch("userRolesList");
List<User> users = em.createQuery(criteria).getResultList();
In User class I have the userRolesList (n-m table) which has the roles. So I have User -> UserRoles -> Role. Every property are mapped as FetchType.LAZY
When I try to print the user roles, for example, hibernate throws the org.hibernate.LazyInitializationException cause the roles where not fetched.
When I change code for the one that follows.
code:
CriteriaBuilder builder = em.getCriteriaBuilder();
CriteriaQuery<User> criteria = builder.createQuery(Fornecedor.class);
Root<User> root = criteria.from(User.class );
criteria.select(root);
root.fetch("userRolesList");
root.fetch("userRolesList.role");
List<User> users = em.createQuery(criteria).getResultList();
then I get:
java.lang.IllegalArgumentException: Unable to locate Attribute with
the the given name [userRolesList.role] on this ManagedType [User]
I have tryed many options for the situation like joins, but still couldn't make it work.
It seems you are trying to go a bit too far with the fetching.
Try doing it in baby steps:
CriteriaBuilder builder = em.getCriteriaBuilder();
CriteriaQuery<User> criteria = builder.createQuery(Fornecedor.class);
Root<User> root = criteria.from(User.class );
criteria.select(root);
Fetch<User, UserRoles> userRolesFetch = root.fetch("userRolesList", JoinType.INNER);
Fetch<UserRoles, Role> roleFetch = userRolesFetch.fetch("role", JoinType.INNER);
List<User> users = em.createQuery(criteria).getResultList();
Related
I have a Student entity and want to select only two fields - id and age. After reading different posts I wrote the following code:
CriteriaQuery<Student> criteriaQuery = ..
var root = criteriaQuery.from(Student.class);
criteriaQuery.multiselect(root.get("id"), root.get("age"));
typedQuery = entityManager.createQuery(criteriaQuery);
List<Student> students = typedQuery.getResultList();
However, it doesn't work. How to do it using hibernate jpa provider?
If you only want two fields and use multiselect you can use a TupleQuery like in the example below:
CriteriaQuery<Tuple> criteriaQuery = criteriaBuilder.createTupleQuery();
var root = criteriaQuery.from(Student.class);
criteriaQuery.multiselect(root.get("id"), root.get("age"));
typedQuery = entityManager.createQuery(criteriaQuery);
List<Tuple> students = typedQuery.getResultList();
To access the values of the tuples use the get method of it.
E.g.:
Long id = (Long) students.get(0).get(0);
I've used hibernate for a long, now i started using JPA and i can't find a short way to write a simply select in less than seven lines (the use of criteria is a must in this project), is there a shorter way to build this query?
CriteriaBuilder cb = em.getCriteriaBuilder();
CriteriaQuery<Transaction> cq = cb.createQuery(Transaction.class);
Root<Transaction> root = cq.from(Transaction.class);
Collection<Predicate> predicates = new ArrayList<Predicate>();
predicates.add(cb.equal(root.get("originalOperationId"), originalOperationId));
cq.where(predicates.toArray(new Predicate[predicates.size()]));
List<Transaction> resultado = em.createQuery(cq).getResultList();
return resultado;
I have two entities, ShoppingCart and ShoppingCartLine. ShoppingCart has a collection of ShoppingCartLines. I am trying to create a JPA query using criteria to get a list of ShoppingCart ids and the number of ShoppingCartLines each ShoppingCart has.
Here is the mapping in the ShoppingCart class:
#OneToMany(mappedBy = "shoppingCart", fetch = FetchType.EAGER, cascade = CascadeType.ALL, orphanRemoval=true)
private Set<ShoppingCartLine> shoppingCartLines = new TreeSet<ShoppingCartLine>();
Here is the code I am attempting to create my JPA query with:
CriteriaBuilder cb = em.getCriteriaBuilder();
CriteriaQuery<Tuple> cq = cb.createTupleQuery();
Root<ShoppingCart> carts = cq.from( ShoppingCart.class );
Join<ShoppingCart, ShoppingCartLine> lines = carts.join( "shoppingCartLines", JoinType.LEFT);
cq.multiselect( carts.<Long>get( "id" ), cb.count( lines ));
cq.groupBy( carts.<Long>get("id") );
Query tq = em.createQuery( cq );
return tq.getResultList();
When I run this I get an SQLGrammerException, the SQL produced does not look correct to me.
select
shoppingca0_.id as col_0_0_,
count(.) as col_1_0_
from
SHOPPINGCART shoppingca0_
left outer join
SHOPPINGCARTLINE shoppingca1_
on shoppingca0_.id=shoppingca1_.shoppingCart_id,
SHOPPINGCARTLINE shoppingca2_
where
shoppingca0_.id=shoppingca2_.shoppingCart_id
group by
shoppingca0_.id
I should mention I am using Hibernate 3.5.4 with MySQL5Dialect
This is the query I am wanting to generate:
select
sc.id,
count(scl.id)
from
shoppingcart sc
left join
shoppingcartline scl
on
scl.shoppingCart_id = sc.id
group by
sc.id
Any idea what I'm doing wrong? Thanks.
Try to replace join and multiselect lines with
SetJoin<ShoppingCart, ShoppingCartLine> lines =
carts.joinSet( "shoppingCartLines", JoinType.LEFT);
cq.multiselect( carts.<Long>get( "id" ), cb.count( lines.<Long>get( "id" ) ));
I have an entity called User, which has the following relationship to an entity called Company:
#Entity
public class User {
...
#ManyToOne
#JoinColumn(name="COMPANY_ID",referencedColumnName="ID")
private Company company = null;
...
}
And on my database, I have a User table with a "COMPANY_ID" column. How can I create a JPA criteria query using this field?
Using a criteria builder, I've tried the following expressions without success:
CriteriaBuilder cb = entityManager.getCriteriaBuilder();
CriteriaQuery cq = cb.createQuery(User.class);
Root mockEntityRoot = cq.from(User.class);
//cq.where(cb.equal(mockEntityRoot.get("company"), 2));
//cq.where(cb.equal(mockEntityRoot.get("COMPANY_ID"), 12));
cq.where(cb.equal(mockEntityRoot.get("company.id"), 8));
entityManager.createQuery(cq).getResultList();
But I got the following error: "The attribute [company.id] from the managed type User is not present."
Thanks in advance.
I think you need an explicit join.
Notice I am using Criteria's metamodel.
This is a snippet I have, it's not the same thing as yours but you can have an idea
CriteriaBuilder cb = entityManager.getCriteriaBuilder();
CriteriaQuery<Site> q = cb.createQuery(Site.class);
Root<Site> e = q.from(Site.class);
Join<Site,SiteType> siteType = e.join(Site_.siteType);
Predicate predicate = cb.conjunction();
Predicate p1 = cb.equal(siteType.get(SiteType_.id), selectedSiteType.getId());
Predicate p2 = cb.equal(e.get(Site_.markedAsDeleted), false);
predicate = cb.and(p1,p2);
q.where(predicate);
q.select(e);
TypedQuery<Site> tq = entityManager.createQuery(q);
List<Site> all = tq.getResultList();
return all;
I have following jpa criteria query:
CriteriaBuilder cb = em.getCriteriaBuilder();
CriteriaQuery<Company> cq = cb.createQuery(Company.class);
Root<Company> root = cq.from(Company.class);
cq.select(root);
Subquery<String> sq = cq.subquery(String.class);
Root<Employee> subroot = sq.from(Employee.class);
sq.select(subroot.get(Employee_.lastName));
Predicate typePredicate = cb.equal(subroot.get(Employee_.lastName), "Doe");
Predicate correlatePredicate = cb.equal(root.get(Company_.employees), subroot);
sq.where(cb.and(typePredicate, correlatePredicate));
cq.where(cb.exists(sq));
TypedQuery<Company> typedQuery = em.createQuery(cq);
List<Company> companies = typedQuery.getResultList();
Eclipselink produces following SQL:
SELECT t0.ID, ... FROM COMPANY t0
WHERE EXISTS (SELECT t1.LASTNAME FROM EMPLOYEES t2, EMPLOYEES t1
WHERE (((t1.LASTNAME = ?) AND (t1.ID = t2.ID))
AND (t2.COMPANY_ID = t0.ID)))
As you can see there is an unneccessary join on table EMPLOYEES. How do I get rid of this join?
You don't seem to need a subquery for the query, just a join should be enough,
http://en.wikibooks.org/wiki/Java_Persistence/Criteria#Join
Otherwise, what version are you using? Can you try EclipseLink 2.4.
If it still has the duplicate, please log a bug and vote for it.
You might be able to use the inverse ManyToOne, instead of the OneToMany (i.e. root == subroot.get("company") ).
Also try JPQL in 2.4, is the join optimized?