Убрал fetch order

This commit is contained in:
Максим Яковлев 2024-04-16 13:58:32 +04:00
parent d9dc12f427
commit fd9982fdcb
5 changed files with 6 additions and 13 deletions

Binary file not shown.

View File

@ -67,9 +67,9 @@ public class DemoApplication implements CommandLineRunner {
games.add(game2);
var user1 = userService.create(new UserEntity( "login1", "email@mail.com", "qwerty123"));
// var user2 = userService.create(new UserEntity( "login2", "email@gmail.com", "qwerty1234"));
var user2 = userService.create(new UserEntity( "login2", "email@gmail.com", "qwerty1234"));
orderService.create(7, new OrderEntity(user1,games));
// orderService.create(1, new OrderEntity(user1,games));
orderService.create(8, new OrderEntity(user2,games));
}
}

View File

@ -11,10 +11,9 @@ import com.example.demo.games.model.GameEntity;
import com.example.demo.users.model.UserEntity;
import jakarta.persistence.Entity;
import jakarta.persistence.FetchType;
import jakarta.persistence.JoinColumn;
import jakarta.persistence.ManyToMany;
import jakarta.persistence.ManyToOne;
import jakarta.persistence.OneToMany;
import jakarta.persistence.Table;
@Entity
@ -25,7 +24,7 @@ public class OrderEntity extends BaseEntity{
@ManyToOne
@JoinColumn(name = "userId", nullable = false)
private UserEntity user;
@OneToMany(fetch=FetchType.EAGER) //елать запрос jpa
@ManyToMany() //елать запрос jpa
private Set<GameEntity> games = new HashSet<>();
public OrderEntity(){

View File

@ -5,7 +5,7 @@ import java.util.Optional;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.data.jpa.repository.Query;
import org.springframework.data.repository.CrudRepository;
import org.springframework.data.repository.PagingAndSortingRepository;
@ -16,6 +16,7 @@ public interface OrderRepository extends CrudRepository<OrderEntity, Long>, Pagi
List<OrderEntity> findByUserId(long userId);
@Query("select o from OrderEntity o join fetch o.games where o.user.id = ?1")
Page<OrderEntity> findByUserId(long userId, Pageable pageable);
//Можно сделать запрос на сумму JPQL

View File

@ -6,8 +6,6 @@ import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import java.util.List;
import com.example.demo.core.error.NotFoundException;
import com.example.demo.orders.model.OrderEntity;
import com.example.demo.orders.repository.OrderRepository;
@ -24,11 +22,6 @@ public class OrderService {
this.userService = userService;
}
@Transactional(readOnly = true)
public List<OrderEntity> getAll(long userId){
userService.get(userId);
return repository.findByUserId(userId);
}
@Transactional(readOnly = true)
public Page<OrderEntity> getAll(long userId, int page, int size){