Skip to content

Commit cb252b0

Browse files
committed
6_06_HW5_optional_fetch_join
1 parent f4d996d commit cb252b0

14 files changed

Lines changed: 82 additions & 3 deletions

‎src/main/java/ru/javawebinar/topjava/model/User.java‎

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,10 @@ public class User extends AbstractNamedEntity {
5454
@Range(min = 10, max = 10000)
5555
private int caloriesPerDay = DEFAULT_CALORIES_PER_DAY;
5656

57+
@OneToMany(fetch = FetchType.LAZY, mappedBy = "user")
58+
@OrderBy("dateTime DESC")
59+
private List<Meal> meals;
60+
5761
public User() {
5862
}
5963

@@ -123,6 +127,10 @@ public String getPassword() {
123127
return password;
124128
}
125129

130+
public List<Meal> getMeals() {
131+
return meals;
132+
}
133+
126134
@Override
127135
public String toString() {
128136
return "User{" +

‎src/main/java/ru/javawebinar/topjava/repository/MealRepository.java‎

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,4 +20,8 @@ public interface MealRepository {
2020

2121
// ORDERED dateTime desc
2222
List<Meal> getBetweenHalfOpen(LocalDateTime startDateTime, LocalDateTime endDateTime, int userId);
23+
24+
default Meal getWithUser(int id, int userId) {
25+
throw new UnsupportedOperationException();
26+
}
2327
}

‎src/main/java/ru/javawebinar/topjava/repository/UserRepository.java‎

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,4 +18,8 @@ public interface UserRepository {
1818
User getByEmail(String email);
1919

2020
List<User> getAll();
21+
22+
default User getWithMeals(int id) {
23+
throw new UnsupportedOperationException();
24+
}
2125
}

‎src/main/java/ru/javawebinar/topjava/repository/datajpa/CrudMealRepository.java‎

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,4 +23,7 @@ public interface CrudMealRepository extends JpaRepository<Meal, Integer> {
2323

2424
@Query("SELECT m from Meal m WHERE m.user.id=:userId AND m.dateTime >= :startDate AND m.dateTime < :endDate ORDER BY m.dateTime DESC")
2525
List<Meal> getBetweenHalfOpen(@Param("startDate") LocalDateTime startDate, @Param("endDate") LocalDateTime endDate, @Param("userId") int userId);
26+
27+
@Query("SELECT m FROM Meal m JOIN FETCH m.user WHERE m.id = ?1 and m.user.id = ?2")
28+
Meal getWithUser(int id, int userId);
2629
}

‎src/main/java/ru/javawebinar/topjava/repository/datajpa/CrudUserRepository.java‎

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,4 +16,7 @@ public interface CrudUserRepository extends JpaRepository<User, Integer> {
1616
int delete(@Param("id") int id);
1717

1818
User getByEmail(String email);
19+
20+
@Query("SELECT u FROM User u LEFT JOIN FETCH u.meals WHERE u.id = ?1")
21+
User getWithMeals(int id);
1922
}

‎src/main/java/ru/javawebinar/topjava/repository/datajpa/DataJpaMealRepository.java‎

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,4 +50,9 @@ public List<Meal> getAll(int userId) {
5050
public List<Meal> getBetweenHalfOpen(LocalDateTime startDateTime, LocalDateTime endDateTime, int userId) {
5151
return crudMealRepository.getBetweenHalfOpen(startDateTime, endDateTime, userId);
5252
}
53+
54+
@Override
55+
public Meal getWithUser(int id, int userId) {
56+
return crudMealRepository.getWithUser(id, userId);
57+
}
5358
}

‎src/main/java/ru/javawebinar/topjava/repository/datajpa/DataJpaUserRepository.java‎

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,4 +41,9 @@ public User getByEmail(String email) {
4141
public List<User> getAll() {
4242
return crudRepository.findAll(SORT_NAME_EMAIL);
4343
}
44+
45+
@Override
46+
public User getWithMeals(int id) {
47+
return crudRepository.getWithMeals(id);
48+
}
4449
}

‎src/main/java/ru/javawebinar/topjava/service/MealService.java‎

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,4 +47,8 @@ public Meal create(Meal meal, int userId) {
4747
Assert.notNull(meal, "meal must not be null");
4848
return repository.save(meal, userId);
4949
}
50+
51+
public Meal getWithUser(int id, int userId) {
52+
return checkNotFound(repository.getWithUser(id, userId), id);
53+
}
5054
}

‎src/main/java/ru/javawebinar/topjava/service/UserService.java‎

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,4 +50,8 @@ public void update(User user) {
5050
Assert.notNull(user, "user must not be null");
5151
checkNotFound(repository.save(user), user.id());
5252
}
53+
54+
public User getWithMeals(int id) {
55+
return checkNotFound(repository.getWithMeals(id), id);
56+
}
5357
}

‎src/test/java/ru/javawebinar/topjava/UserTestData.java‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
import static ru.javawebinar.topjava.model.AbstractBaseEntity.START_SEQ;
1010

1111
public class UserTestData {
12-
public static final MatcherFactory.Matcher<User> USER_MATCHER = MatcherFactory.usingIgnoringFieldsComparator("registered", "roles");
12+
public static final MatcherFactory.Matcher<User> USER_MATCHER = MatcherFactory.usingIgnoringFieldsComparator("registered", "roles", "meals");
1313

1414
public static final int USER_ID = START_SEQ;
1515
public static final int ADMIN_ID = START_SEQ + 1;

0 commit comments

Comments
 (0)