Norwich
@Norwich
Web-developer

Почему не работает запрос?

Всем привет!
У меня падает запрос с ошибкой:
GET localhost:8080/api/groups
java.sql.SQLSyntaxErrorException: You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'groups group0_ order by group0_.created_at desc limit 30' at line 1

Не могу понять, где я ошибся(Кроме того, что не использую JDBC)
таблица:
CREATE TABLE `groups`
(
    `id`         bigint(19) unsigned NOT NULL AUTO_INCREMENT,
    `name`       varchar(255)        NOT NULL,
    `created_at` timestamp           NOT NULL DEFAULT CURRENT_TIMESTAMP,
    `updated_at` timestamp           NOT NULL DEFAULT CURRENT_TIMESTAMP,
    `created_by` bigint(19) unsigned          DEFAULT NULL,
    `updated_by` bigint(19) unsigned          DEFAULT NULL,
    PRIMARY KEY (`id`)
) ENGINE = InnoDB
  AUTO_INCREMENT = 1
  DEFAULT CHARSET = utf8;

Group.java
@EqualsAndHashCode(callSuper = true)
@Entity
@Data
@Table(name = "groups")
public class Group extends UserDateAudit {
    private static final long serialVersionUID = 1L;

    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    private Long id;

    @Column(name = "name")
    private String name;

    @OneToMany(mappedBy = "group", cascade = CascadeType.ALL, orphanRemoval = true)
    private List<User> users;


    public Long getId() {
        return id;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }
    
    public List<User> getUsers() {
        return users == null ? null : new ArrayList<>(users);
    }

    public void setUsers(List<Comment> comments) {
        if (comments == null) {
            this.users = null;
        } else {
            this.users = Collections.unmodifiableList(users);
        }
    }

}


GroupRepository.java
@Repository
public interface GroupRepository extends JpaRepository<Group, Long> {
    //Group findByName(String name);
}


GroupService.java
public interface GroupService {
    PagedResponse<Group> getAllGroup(int page, int size);
    Group addGroup(Group group, UserPrincipal currentUser);
}


GroupServiceImpl.java
@Service
public class GroupServiceImpl implements GroupService {
    @Autowired
    private GroupRepository groupRepository;

    @Override
    public PagedResponse<Group> getAllGroup(int page, int size) {
        AppUtils.validatePageNumberAndSize(page, size);
        Pageable pageable = PageRequest.of(page, size, Sort.Direction.DESC, "createdAt");
        Page<Group> groups = groupRepository.findAll(pageable);
        List<Group> content = groups.getNumberOfElements() == 0 ? Collections.emptyList() : groups.getContent();
        return new PagedResponse<>(content, groups.getNumber(), groups.getSize(), groups.getTotalElements(), groups.getTotalPages(), groups.isLast());
    }
}


GroupController.java
@RestController
@RequestMapping("/api/groups")
public class GroupController {
    @Autowired
    private GroupService groupService;

    @GetMapping
    public ResponseEntity<PagedResponse<Group>> getAllGroups(
            @RequestParam(name = "page", required = false, defaultValue = AppConstants.DEFAULT_PAGE_NUMBER) Integer page,
            @RequestParam(name = "size", required = false, defaultValue = AppConstants.DEFAULT_PAGE_SIZE) Integer size) {

        PagedResponse<Group> response = groupService.getAllGroup(page, size);

        return new ResponseEntity<>(response, HttpStatus.OK);
    }
}
  • Вопрос задан
  • 87 просмотров
Пригласить эксперта
Ваш ответ на вопрос

Войдите, чтобы написать ответ

Войти через центр авторизации
Похожие вопросы