@Test public void queryByExample() { System.out.println("\nFind the Department with the name 'Humanities' \n" + departmentRepository.findOne(Example.of(new Department("Humanities", null)))); System.out.println("\nFind Departments with the first name of the chair is 'John'"); departmentRepository.findAll(Example.of( new Department(null, new Staff(new Person("John", null))))).forEach(System.out::println); System.out.println("\nFind All Departments with the name ending in 'sciences', case insensitive"); departmentRepository.findAll(Example.of(new Department("sciences", null), ExampleMatcher.matching(). withIgnoreCase(). withStringMatcher(ExampleMatcher.StringMatcher.ENDING))).forEach(System.out::println); }
@Embeddable public class Person { @Column private String firstName; @Column private String lastName; public Person(String firstName, String lastName) { this.firstName = firstName; this.lastName = lastName; } protected Person() { } public String getFirstName() { return firstName; } public String getLastName() { return lastName; } @Override public String toString() { return " firstName='" + firstName + '\'' + ", lastName='" + lastName + "\' "; } }
@Entity @Table(name="Department") public class Department { @Id @GeneratedValue private Integer id; @Column private String name; @OneToOne private Staff chair; @OneToMany(fetch = FetchType.EAGER,mappedBy="department", cascade = CascadeType.ALL) private List<Course> courses = new ArrayList<>(); public Department(String name, Staff chair) { this.name = name; this.chair = chair; } protected Department() { } public Integer getId() { return id; } public String getName() { return name; } public void addCourse(Course course) { courses.add(course); } public void setName(String name) { this.name = name; } public void setChair(Staff chair) { this.chair = chair; } public void setCourses(List<Course> courses) { this.courses = courses; } @Override public String toString() { return "Department{" + "chair=" + chair + ", name='" + name + '\'' + ", id=" + id + '}'; } }