I know this may be a very simple question for some of and, but I'm having difficulty trying to figure out how to build a simple Select * From X, where Xa =: MyParam using CriteriaBuilder.
Now this is the code that I have managed to create so far:
CriteriaBuilder cb = getEntityManager().getCriteriaBuilder(); CriteriaQuery cq = cb.createQuery(); Root<MyClass1> r = cq.from(MyClass1.class); cq.select(r); ParameterExpression<Long> p = cb.parameter(Long.class); cq.where(cb.equal(r.get("anotherClass.id"), p)); javax.persistence.Query q = getEntityManager().createQuery(cq);
The class in which I apply this query is as follows:
@Entity public class MyClass1 implements Serializable { @Id private Long id; @ManyToOne(fetch = FetchType.LAZY) @JoinColumn(name = "ANOTHERCLASS_ID") private AnotherClass anotherClass; ... } @Entity public class AnotherClass implements Serializable { @Id private Long id; ... }
I just need to select all the entries from MyClass1 "WHERE" anotherClass.id = 1L , and where can I set to "1L", I know this goes to p , but where?
It's all. It looks simple, but I'm really not familiar with this CriteriaBuilder thing, so I hope you can have the answers.
Thanks.
source share