I am using spring data jpa:
What is the best way to filter child object on the parent.
on my below example
I want Parent objects which has active childs, also wanted only active childs as list with parent
#Query(select distinct p from Parent p inner join p.child c where c.active=:active)
Page<Parent> getAllPArents(#Param("active") int active);
#Entity
Parent{
#OneToMany(fetch=FetchType.LAZY)
List<Child> child;
}
#Entity
Child{
#ManyToOne
Parent parent;
}
I had exactly the same problem and it took me a while to find out how this works. The child list will be filtered when you add FETCH after your JOIN like this:
SELECT p FROM Parent p JOIN FETCH p.child c WHERE c.active=:active
After spending lot of time, I could not find any solution to filter the child while querying the parent.
I decided to change the database structure so that I will be able to query child that I wanted. Added another filed/property that can put child into different categories. When querying child using unique criteria that will give me only the child I needed, also gave me the parent which I needed too.
I found the way to do this:
You can use the #Where annotation to filter the childs of a one to many or a many to many relationship.
public class Parent {
#OneToMany(mappedBy="Parent")
#Where(clause = "active = 1") //In case of a boolean but it can be a LIKE, a comparator...
private Set<Child> childs; //This gets filled with pets with a name starting with N
//getters & setters
}
Related
I have two objects one parent model have ToOne relation and a child model with ToMany relation back to the parent. Now if I have two parents who are setting their relation to child then the first parent's reference to child is getting null when the second parent reference is set. Also, if I remove the id field from the child then both parents child is stored in the database.
Suppose I have a class like
Student{
String id;
final teacher = ToOne<Teacher>();
}
and another class Teacher as
Teacher{
#Unique(onConflict: ConflictStrategy.replace)
String? id;
#Backlink()
final students = ToMany<Student>();
}
so now if I try to do something like
student1.teacher.target = teacher1
followed by
student2.teacher.target = teacher1
then if I try to check for student1 then its teacher is getting null. Why ?
I have an entity which contains a list of elements and now I want to search over attributes of these elements. This constraint should be "and" connected. Please see these simple example:
#Entity
public class Parent {
#Column
#Enumerated(EnumType.STRING)
private City city;
#OneToMany(...)
private List<Children> childrens;
}
#Entity
public class Children {
#Column
#Enumerated(EnumType.STRING)
private School school;
#Column
private Integer yearInSchool;
}
Now I want to find Parents in a certain city, lets say "BigCity" with children in School "AwesomeSchool" which are in class/ year 6. I want to get the search result only via CriteriaBuilder.
So far I got:
final CriteriaBuilder c = getCriteriaBuilder();
final CriteriaQuery<Parent> query = c.createQuery(Parent.class);
final Root<Parent> r = query.from(Parent.class);
query.select(r)
.where(c.and(c.equal(r.get("city"), City.BigCity)),
c.equal(r.get("childrens").get("school"), School.AwesomeSchool),
c.equal(r.get("childrens").get("yearInSchool"), 6));
Unfortunately there are two problems here:
- it looks like I can't call get("school") on the list attribute
- this will return all parents with children which are either in "AwesomeSchool" or are 6 years in the school.
Can you help me please? I thought about using a join, but there the same question is: how can I define the where part of the join so that it considers that both attributes (school and yearInSchool) have to be fulfilled at the same time.
I found similar posts about querying for objects whose children fulfill one condition - but here the children has to fulfill two conditions at the same time.
Update 1
If I use a join to assert e.g. the "school" of one child, I get so far concerning the predicate:
Predicate predicate = r.join("childrens").get("school").in(School.AwesomeSchool)
How can I reuse this joined object to assert is also for the second filter condition?
You need to JOIN and then use the JOIN object you got when forming the join when forming the WHERE clauses.
Join childrenJoin = r.join("childrens");
query.where(c.and(c.equal(r.get("city"), City.BigCity)),
c.equal(childrenJoin.get("school"), School.AwesomeSchool),
c.equal(childrenJoin.get("yearInSchool"), 6));
Perhaps you mean your JPQL to be :
SELECT p FROM Parent p JOIN p.childrens c
WHERE p.city = :theCity AND c.school = :theSchool AND c.yearInSchool = 6
Suppose the following tables
ParentEntities
ParentID
ChildEntities
ChildID
ParentID
These tables do not have a FK defined in the schema.
In EF designer, after generating from DB, I add an association:
- Parent Multiplicity: 1
- Child Multiplicity: 0 or 1
When I build, I get the error: "Error 3027: No mapping specified for the following EntitySet/AssociationSet - ParentChild"
But if I try to configure table mapping for the association like this..
Maps to ChildEntities
Parent
ParentID <-> ParentID (parent entity prop <-> child table column)
Child
ChildID <-> ChildID (child entity prop <-> child table column)
.. I get this: Error 3007: Problem in mapping fragments starting at lines xxx, xxx: Column(s) [ParentID] are being mapped in both fragments to different conceptual side properties.
Why this is an error doesn't make sense. Limitation of the current implementation?
[EDIT 1]
I'm able to make this work by creating a 1-N association. That's not ideal, but it works just the same, just have to add a read-only child property in a partial:
public partial class Parent
{
public Child Child { get { return Childs.Any() ? null : Childs.First(); } }
}
This seems like the best solution for me. I had to add a FK to the database to get EF to generate the association and navigation property, but once it was added I was able to remove the FK, and further updates to the model from the DB did not remove the association or Navigation properties.
[EDIT 2]
As I was investigating how to work around not caring about the association being modeled in EF, I ran into another issue. Instead of the read-only Child property I made it normal ..
public partial class Parent
{
public Child Child { get; set; }
}
.. but now I need a way to materialize that from the query:
var query = from parents in context.Parents
// pointless join given select
join child in context.Childs
on parents.ParentID equals child.ParentID
select parents;
I can select an anonymous type ..
// step 1
var query = from parents in context.Parents
join child in context.Childs
on parents.ParentID equals child.ParentID
select new { Parent = parents, Child = child };
.. but then I've got to consume more cycles getting that into my entity:
// step 2
var results = query.Select(x => {
var parent = x.Parent;
parent.Child = x.Child;
return parent; });
Is there a better/streamlined way to do this from the query select so the EF materializer can do it from the get-go? If not, then I'll resort to Edit 1 methodology ..
Ef Code first requires 1->0..1 relationships for the Child to have the same primary key.
Maybe this a similar restriction In the modeler in this circumstance.
ParentId (Key) required in Both tables.
I have never tried adding such relationships in designer afterwords in DB first.
EDIT: to match your EDIT2:
I would stay on the direction . Use Navigation properties to get from Join back to original class A and B.
query = context.Set<JoinTable>.Where(Jt=>Jt.NavA.Id == ClassAId
&& Jt.navB.Id == ClassBId)
use a select if your need entries returned from either ClassA or ClassB.
Suppose i have user collection and children collection.
Assume I have a specific user and children (which is the many side of user) has a field
status of type string. I want to search for that child in user where status= "active".
Would the query be different if only one child in user can have status="active" at any time, although none of children may have status equal to "active" ?
Children are not embedded but reference in user.
This is my solution but does not look very efficient to me;
for (c : user.children) {
if (c.status == "active") {
child = c
}
}
To search db directly you got add a "parentId" field to your Children class:
public class Children {
public String status;
public ObjectId parentId; // point to the parent of this child
}
Then here is the way to go:
If you use pure morphia:
Datastore ds = ...;
Children activeChild = ds.find(Children.class).filter("status", "active").filter("parentId", user.getId()).get();
If you are using Play!Framework with PlayMorphia module:
Children activeChild = Children.find("status,parentId", "active", user.getId()).get();
Hy all,
I have three tables Child, Pet and Toy. Pet has a reference key the child id, and a toy has a reference key to a dog id.
I want to load all data about a Child and his pets, but i don't want to load the toy data
#OneToMany(mappedBy = "petEntity", fetch = FetchType.LAZY)
public Set<PetEntity> getPetEntitySet() {
return petEntitySet;
}
#ManyToOne(fetch = FetchType.LAZY)
#JoinColumn(name = "ChildId", insertable = false, updatable = false)
public ChildEntity getChildEntity() {
return childEntity;
}
same for set of toys.
to load data i have
List<ChildEntity> list = entityManager.createQuery(
"SELECT c FROM ChildEntity c "+
"LEFT JOIN FETCH c.petEntitySet ",
ChildEntity.class).getResultList();
return list;
but this thing loads me all data, not just the information about child and his pets.
How can i suppress the entity manager to load only the data in the table, and not to make joins when i don't want that
Thanks for your advices
i forgot to mention that in this chase it doesn't only load all data, but it returns more than just one copy of a child elemnt
The reason why it is loading more data is because you are using the keyword fetch. This will EAGERLY load whatever the child's pets.
Just remove the fetch and lazy loading will occur.
Update
I see that this is actually what you want, so just ensure equals() and hashCode() is correctly overriden in all relevant classes, as I assume you are using Set.
In this way, jpa knows how to look for duplicates
Second update
Yes you can easily use DISTINCT in your queries.
Just add DISTINCT in your select
List<ChildEntity> list = entityManager.createQuery(
"SELECT DISTINCT c FROM ChildEntity c "+
"LEFT JOIN FETCH c.petEntitySet ",
ChildEntity.class).getResultList();
return list;