I have the following simple application
Users Entity
@Entity
public class Users implements Serializable {
   @Id
   @GeneratedValue
   private long id;
   private String name;
   @OneToMany(mappedBy = "user", fetch = FetchType.EAGER, cascade = {CascadeType.ALL})
   private Set<UserRoleUser> userRoleUser;
   // GETTERS AND SETTERS
}
UserRole Entity
@Entity
public class UserRole implements Serializable {
   @Id
   @GeneratedValue
   private long id;
   private String roleName;
   @OneToMany(mappedBy = "userrole", fetch = FetchType.LAZY, cascade = CascadeType.ALL)
   private Set<UserRoleUser> userRoleUser;
   // GETTERS AND SETTERS
}
UserRoleUser Many to many resolver class
@Entity
public class UserRoleUser implements Serializable {
   @Id
   @GeneratedValue
   private long id;
   @ManyToOne
   @JoinColumn(name = "fk_userId")
   private Users user;
   @ManyToOne
   @JoinColumn(name = "fk_userroleId")
   private UserRole userrole;
   // GETTERS AND SETTERS
}
UserRoleUserRepository
@Repository
@Transactional
public interface UserRoleUserRepository extends JpaRepository<UserRoleUser, Long>, QueryDslPredicateExecutor<UserRoleUser>{
}
Main Application class
@SpringBootApplication
@Configuration
public class Application {
   public static void main(String[] args) {
       ConfigurableApplicationContext context = SpringApplication.run(Application.class, args);
       UserRoleUserRepository userRoleUserRepository = context.getBean(UserRoleUserRepository.class);
       Iterable<UserRoleUser> findAll = userRoleUserRepository.findAll(QUserRoleUser.userRoleUser.id.gt(0));
       for (UserRoleUser userRoleUser : findAll) {
           userRoleUserRepository.delete(userRoleUser);
       }
   }
}
On running the main application, the database records in the UserRoleUser table are not being deleted. What could be the issue? I am using Spring Data and QueryDsl.
I have also tried putting the delete functionality on a Controller but still doesn't work.
@RestController
@RequestMapping("/api")
public class DeleteController {
    @Autowired
    UserRoleUserRepository userRoleUserRepository;
    @GetMapping("/delete")
    public String delete() {
        Iterable<UserRoleUser> findAll = userRoleUserRepository.findAll(QUserRoleUser.userRoleUser.id.gt(0));
        for (UserRoleUser userRoleUser : findAll) {
            userRoleUserRepository.delete(userRoleUser);
        }
        return new Date().toString();
    }
}
                We can use the JPA method deleteById() for deleting the record of the particular primary key.
CrudRepository provides CRUD functions. PagingAndSortingRepository provides methods to do pagination and sort records. JpaRepository provides JPA related methods such as flushing the persistence context and delete records in a batch.
First of all you need to create a jpa query method that brings all records belong to id. After that you can do deleteAll() operation on List.
Run the application Click on Send Button and check the response status to be OK. Now make a Delete Request to delete that records. Set the following parameters in POSTMAN. Click the send button and verify the response status to be OK.
If you need to use the given methods provided by CrudRepository, use the JpaRepository.deleteInBatch(). This solves the problem.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With