Home > Net >  Hibernate is only returning one record when it's not supposed to
Hibernate is only returning one record when it's not supposed to

Time:10-14

So I have this endpoints that is supposed to get a list of boards where the given user is part of but for some reason it always returns just 1 value.

Context:

    //BoardController.java
    @GetMapping
    @ApiOperation(
        value = "Retrieves all boards",
        httpMethod = "GET",
        response = Page.class,
        code = 200
    )
    public List<BoardDto> findBoardsForUser(@RequestParam(value = "user", defaultValue = "0")                 
 final Long userId) {
         var boards = boardService.findAllForUser(userId);
         System.out.println(boards.size());
         return boards.stream().map(boardMapper::toDto).collect(Collectors.toList());
    }


    //BoardService.java
    public List<Board> findAllForUser(Long userId) {
         return boardRepository.findBoardsByUser(userId);
    }

    //BoardRepository.java
    @Repository
    @Transactional
    public interface BoardRepository extends JpaRepository<Board, Long> {

        @Query("SELECT b FROM Board b, BoardUser bu where b.id = bu.board.id and bu.id = :userId")
        List<Board> findBoardsByUser(Long userId);
    }

Question: Is there any reason why Hibernate would only retrieve one record when I have two records in the database that get retrieved just fine using the same select statement?

CodePudding user response:

@Query("SELECT b FROM BoardUser bu JOIN bu.board b where bu.id = :userId")

CodePudding user response:

This seems to be impossible. If a java.util.Set would be used and no unique ID is defined I could understand this, but you are using a List. Please add to the Spring Boot config to see all executed statements in your log output:

logging.level.org.hibernate.SQL=DEBUG
logging.level.org.hibernate.type.descriptor.sql.BasicBinder=TRACE

There must be a difference how the generated SQL looks like.

  • Related