简体   繁体   English

Spring 数据 JPA 页面查询错误结果

[英]Spring Data JPA Page Query Wrong Result

I am creating a query for paging operation.我正在为分页操作创建查询。 But when I give the sort column, the answer is wrong.但是当我给出排序列时,答案是错误的。 All Completed fields come true.所有已完成的字段都成真。 Completed fields in database not true.数据库中的已完成字段不正确。 If there is no sort column, the result is correct.如果没有排序列,则结果是正确的。 The same problem persists when you do not give sort in Pageable and add an order to the query.当您不在 Pageable 中进行排序并向查询添加顺序时,同样的问题仍然存在。

It works when i remove todoitem0_.item_desc as item_des4_0_ from the query.当我从查询中删除todoitem0_.item_desc 作为 item_des4_0_时,它会起作用。 But I couldn't understand the problem但我无法理解问题

Problem is fixed when the description field is defined as varchar(255).当描述字段定义为 varchar(255) 时,问题已得到解决。 But I need to define @Lob但我需要定义@Lob

Controller Controller

@RestController
@CrossOrigin
@RequestMapping("/api/item")
public class TodoItemController {
    @GetMapping(value = "/list")
    private TodoItemDto getUserItems(Authentication authentication,
            @RequestParam("page") int page, @RequestParam("sizePerPage") int sizePerPage){

        Pageable pageable = PageRequest.of(page,sizePerPage, Sort.by("createdAt").descending());
        return todoItemService.getUserItems(((CustomUserDetails) authentication.getPrincipal()).getId(),pageable);

    }
}

Service服务

@Service
@Transactional
public class TodoItemServiceImpl implements TodoItemService {
    @Override
    public TodoItemDto getUserItems(long userId, Pageable pageable){
        
        Page<TodoItem> itemPage = todoItemRepository.findUserItems(userId, pageable);
        TodoItemDto dto = new TodoItemDto();
        dto.setContent(itemPage.getContent());
        dto.setTotal((int)itemPage.getTotalElements());
        return dto;
    }
}

Repository存储库

public interface TodoItemRepository extends JpaRepository<TodoItem, Integer> {
    
    @Query(value = "SELECT i FROM TodoItem i "
            + "INNER JOIN Todo t ON t.user.id = ?1 "
            + "WHERE i.todo.id = t.id ")
    Page<TodoItem> findUserItems(long userId, Pageable pageable);
    
}

Entity实体

@Data
@AllArgsConstructor
@NoArgsConstructor
@Entity
@Table(name = "items")
public class TodoItem{
    
    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    private Integer id;
    
    @NotBlank(message = "Item name not be blank")
    @Column(nullable = false, unique = true)
    private String itemName;
    
    @NotBlank(message = "Item description not be blank")
    @Lob
    private String itemDesc;
    
    @ManyToOne
    @JoinColumn(name = "todoId")
    private Todo todo;
    
    @Column(nullable = false)
    private boolean completed = false;
    
    @Temporal(TemporalType.TIMESTAMP)
    @Column(nullable=false, updatable = false)
    @JsonFormat(shape = JsonFormat.Shape.STRING, pattern = "dd / MM / yyyy")
    private Date createdAt;
    
    @PrePersist
    protected void onCrearedAt() {
        this.createdAt = new Date();
    }
}

Hibernate Sql Query Hibernate Sql 查询

select
    todoitem0_.id as id1_0_,
    todoitem0_.completed as complete2_0_,
    todoitem0_.created_at as created_3_0_,
    todoitem0_.item_desc as item_des4_0_,
    todoitem0_.item_name as item_nam5_0_,
    todoitem0_.todo_id as todo_id6_0_ 
from
    items todoitem0_ 
inner join
    todo todo1_ 
        on (
            todo1_.user_id=?
        ) 
where
    todoitem0_.todo_id=todo1_.id 
order by
    todoitem0_.created_at desc limit ?

Notice this '1' digit in your query,请注意查询中的这个“1”数字,

+ "INNER JOIN Todo t ON t.user.id = ?1 "

This is messing with your resultset这弄乱了您的结果集

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

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