简体   繁体   中英

Hibernate Criteria, group/select distinct children of a parent

I have a model Parent, which has a list of Children (List

class Parent {
   @OneToMany(targetEntity = Child.class, cascade = CascadeType.ALL, mappedBy = "parent")
   @JsonManagedReference
   private List<Child> tags =  new ArrayList<>();

   @Column(name = "name")
   public String name;

}

class Child {
     @Column(name = "name")
     public String name;

     @ManyToOne(fetch = FetchType.LAZY)
     @JoinColumn(name = "parent_id", nullable = true)
     @JsonBackReference
     private Parent parent;
}

I select a list of parents (List < Parent> ) with children in EAGER mode.

Now Children can have the same name, but I do not want children with the same name to be more than once in the list. Any suggestions how have children with the same name only once in the collection?

Here is the sample code using set to remove repeated values. If firstName is present once second entry is ignored. You can modify if according to your needs.

However while this is the answer to the question you have asked please note there is a code smell here . They fact you are having to do this in code while database allows for it is questionable and almost guaranteed a design flaw.

import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

public class Test
{
    private String firstName;
    private String lastName;


    @Override
    public int hashCode() {
        final int prime = 31;
        int result = 1;
        result = prime * result
                + ((firstName == null) ? 0 : firstName.hashCode());
        return result;
    }


    @Override
    public boolean equals(Object obj) {
        if (this == obj)
            return true;
        if (obj == null)
            return false;
        if (getClass() != obj.getClass())
            return false;
        Test other = (Test) obj;
        if (firstName == null) {
            if (other.firstName != null)
                return false;
        } else if (!firstName.equals(other.firstName))
            return false;
        return true;
    }


    public Test(String firstName, String lastName) {
        super();
        this.firstName = firstName;
        this.lastName = lastName;
    }


    public static void main(String[] args)
    {
        List<Test> aList = new ArrayList<Test>();
        Test test1 = new Test("Tom","Hardy");
        Test test2 = new Test("Tom","Cruise");
        Test test3 = new Test("Any","Body");
        aList.add(test1);
        aList.add(test2);
        aList.add(test3);

        System.out.println("List values:");
        for (Test test : aList)
        {
            System.out.println(test.firstName + "-"+ test.lastName);
        }
        Set<Test> alphaSet = new HashSet<Test>(aList);
        System.out.println("Set values:");
        for (Test test : alphaSet)
        {
            System.out.println(test.firstName + "-"+ test.lastName);
        }
    }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM