简体   繁体   English

Hibernate 只在不应该返回一条记录时返回

[英]Hibernate is only returning one record when it's not supposed to

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.所以我有这个端点,它应该获取给定用户所在的板列表,但由于某种原因,它总是只返回 1 个值。

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?问题:当我在数据库中有两条记录使用相同的 select 语句可以正常检索时,Hibernate 是否只检索一条记录?

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

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 .如果将使用java.util.Set并且未定义唯一 ID,我可以理解这一点,但您使用的是List Please add to the Spring Boot config to see all executed statements in your log output:请添加到 Spring Boot 配置以查看日志输出中的所有已执行语句:

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.生成的 SQL 的外观肯定有所不同。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM