protected void setNestedAttributes(Part part, Filter filter) { List<Filter.NestedPathSegment> segments = new ArrayList<>(); PropertyPath property = part.getProperty(); if (property.hasNext()) { filter.setOwnerEntityType(property.getOwningType().getType()); segments.add(new Filter.NestedPathSegment(property.getSegment(), property.getType())); segments.addAll(deepNestedProperty(property)); filter.setPropertyName(property.getLeafProperty().getSegment()); filter.setNestedPath(segments.toArray(new Filter.NestedPathSegment[0])); } }
Filter createDstSwitchFilter(SwitchId switchId) { Filter dstSwitchFilter = new Filter(SWITCH_NAME_PROPERTY_NAME, ComparisonOperator.EQUALS, switchId.toString()); dstSwitchFilter.setNestedPath(new Filter.NestedPathSegment(DEST_SWITCH_FIELD, Switch.class)); return dstSwitchFilter; } }
Filter createSrcSwitchFilter(SwitchId switchId) { Filter srcSwitchFilter = new Filter(SWITCH_NAME_PROPERTY_NAME, ComparisonOperator.EQUALS, switchId.toString()); srcSwitchFilter.setNestedPath(new Filter.NestedPathSegment(SRC_SWITCH_FIELD, Switch.class)); return srcSwitchFilter; }
protected void setNestedAttributes(Part part, Filter filter) { List<Filter.NestedPathSegment> segments = new ArrayList<>(); PropertyPath property = part.getProperty(); if (property.hasNext()) { filter.setOwnerEntityType(property.getOwningType().getType()); segments.add(new Filter.NestedPathSegment(property.getSegment(), property.getType())); segments.addAll(deepNestedProperty(property)); filter.setPropertyName(property.getLeafProperty().getSegment()); filter.setNestedPath(segments.toArray(new Filter.NestedPathSegment[0])); } }
@Test public void loadRatingByUserNameAndStars() { Filter userNameFilter = new Filter("name", ComparisonOperator.EQUALS, "Michal"); userNameFilter.setNestedPath( new Filter.NestedPathSegment("user", User.class) ); Collection<Rating> ratings = session.loadAll(Rating.class, userNameFilter); assertThat(ratings).hasSize(2); }
@Test public void loadFilmByRating() { Filter filter = new Filter("stars", ComparisonOperator.EQUALS, 5); filter.setOwnerEntityType(Movie.class); filter.setNestedPath( new Filter.NestedPathSegment("ratings", Rating.class) ); Collection<Movie> films = session.loadAll(Movie.class, filter); assertThat(films).hasSize(1); }
@Test public void loadRatingByUserName() { Filter userNameFilter = new Filter("name", ComparisonOperator.EQUALS, "Michal"); Filter ratingFilter = new Filter("stars", ComparisonOperator.EQUALS, 5); userNameFilter.setNestedPath( new Filter.NestedPathSegment("user", User.class) ); Collection<Rating> ratings = session.loadAll(Rating.class, userNameFilter.and(ratingFilter)); assertThat(ratings).hasSize(1); } @Test
@Test public void loadFilmByRatingUserPlays() { Filter filter = new Filter("level", ComparisonOperator.EQUALS, "ok"); filter.setOwnerEntityType(Movie.class); filter.setNestedPath( new Filter.NestedPathSegment("ratings", Rating.class), new Filter.NestedPathSegment("user", User.class), new Filter.NestedPathSegment("plays", Plays.class) ); Collection<Movie> films = session.loadAll(Movie.class, filter); assertThat(films).hasSize(2); }
@Test public void loadFilmByRatingUsersPet() { // Pulp Fiction and Top Gear rated by Michal who owns Catty Filter filter = new Filter("name", ComparisonOperator.EQUALS, "Catty"); filter.setOwnerEntityType(Movie.class); filter.setNestedPath( new Filter.NestedPathSegment("ratings", Rating.class), new Filter.NestedPathSegment("user", User.class), new Filter.NestedPathSegment("pets", Pet.class) ); Collection<Movie> films = session.loadAll(Movie.class, filter); assertThat(films).hasSize(2); }
@Test public void loadFilmByUserAndRatingUserPlays() { Filter userFilter = new Filter("name", ComparisonOperator.EQUALS, "Michal"); userFilter.setNestedPath( new Filter.NestedPathSegment("ratings", Rating.class), new Filter.NestedPathSegment("user", User.class) ); Filter playsFilter = new Filter("level", ComparisonOperator.EQUALS, "ok"); playsFilter.setOwnerEntityType(Movie.class); playsFilter.setNestedPath( new Filter.NestedPathSegment("ratings", Rating.class), new Filter.NestedPathSegment("user", User.class), new Filter.NestedPathSegment("plays", Plays.class) ); Filters filters = userFilter.and(playsFilter); Collection<Movie> films = session.loadAll(Movie.class, filters); assertThat(films).hasSize(2); }