Hello, there was a question with how to get for example a request, i.e. whether it was executed or not. I thought that the same exception should be thrown out if the request is not working, but which one I could not find, and in general, does anyone get the result of the request by catching the exception or not? Now everything is quite primitive and stupid so to say, here is an example:
Entity
@Entity @Getter @Setter @Table(name = "groups") public class Group { @Id @GeneratedValue(generator = "increment") @GenericGenerator(name= "increment", strategy= "increment") @Column(name = "id", unique = true, nullable = false) private long id; @Column(name = "name", length = 20, nullable = false) private String name; public Group() { } public Group(String name) { this.name = name; } } Repository
public interface GroupRepository extends JpaRepository<Group, Long> { @Transactional void deleteByName(String name); @Transactional Group findByName(String name); } Service
@Service public class GroupsServiceImpl implements GroupsService { @Autowired private GroupRepository groupRepository; public List<Group> getGroups() { return groupRepository.findAll(); } public boolean addGroup(String name) { Group group = groupRepository.findByName(name); if(group == null) { groupRepository.saveAndFlush(new Group(name)); return true; } else { return false; } } public boolean changeNameGroup(String oldName, String newName) { Group oldGroup = groupRepository.findByName(oldName); Group newGroup = groupRepository.findByName(newName); if(oldGroup != null && newGroup == null){ oldGroup.setName(newName); groupRepository.save(oldGroup); return true; } else { return false; } } public boolean removeGroup(String name) { Group group = groupRepository.findByName(name); if(group != null) { groupRepository.deleteByName(name); return true; } else { return false; } } } I hope that someone will tell you how to do this, I would just like to give the result to the user if the deletion, addition, etc. came out.
@Transactionalon method declarations in the interface extendingJpaRepositoryuseless. - Sergey Gornostaev 5:26 pm