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.

  • 2
    You can instead of the void type of the method put the type of int, then the number of affected records in the database will be returned. And then compare with 0. - aleshka-batman
  • one
    And putting @Transactional on method declarations in the interface extending JpaRepository useless. - Sergey Gornostaev 5:26 pm

1 answer 1

If an exception does not occur, you can safely send the result that the add or delete operation was successful. Otherwise, you should handle an error which indicates that the operation is not successful.

  • What kind of exception should that arise? Because catching an Exception without specifying a specific type is somehow not desirable. - QuickDzen
  • one
    Why do not you want? - Roman C
  • Well, I’m not a fan to catch any exceptions, other people who will read the code, maybe it will not be possible - QuickDzen
  • If you do not know what exceptions you need to catch, then your people do not need to know what exactly you want to tell them and the result of the operation will be uncertain. - Roman C