Hi, I'm sorry I'm sure there was such question before. I tried to search for "query nested object" and didn't get related topic. Looks like my query in wrong.
I'm using hibernate 5.2.x and JPA 2.1, I don't understand how to query
Code:
class Parent {
@Id
id:Long
@OneToOne /** yeah, only one child is allowed **/
@JoinColumn(name = "childId", nullable = false)
child: Child
}
and child:
Code:
class Child{
id:Long
name: String
}
I'm trying to query Parent entity by child.id attribute using CriteriaQuery. Here is my code:
v
Code:
al criteriaBuilder: CriteriaBuilder = session.getCriteriaBuilder
val criteriaQuery = criteriaBuilder.createQuery(classOf[Parent])
val root: Root[Parent] = criteriaQuery.from(classOf[Parent])
// exception here
val restriction = criteriaBuilder.equal(root.get("child.id"), 123L)
criteriaQuery.where(restriction)
Exception is:
java.lang.IllegalArgumentException: Unable to locate Attribute with the the given name [child.id] on this ManagedType [persistence.model.Parent]I'm now quite well understand what I do wrong.
https://docs.jboss.org/hibernate/orm/5. ... Guide.htmlstates:
The FROM clause can only refer to a single entity, which can be aliased. If the entity name is aliased, any property references must be qualified using that alias. If the entity name is not aliased, then it is illegal for any property references to be qualified.
I don't understand what it means...
What is the idea behind?
I'm trying to expose simple API for my model defined as JPA entities.
Users makes call
Code:
search(0, 100L, filter = List(And("child.id", EqualTo, 1L), And("child.name", EqualTo, "best name in the world"))
and it returns children with id=1 and name="best name in the world"