In my project, I added a function that removes students, now I want to be able to delete users. I wrote the code, but there is an error and it does not work. You can look at the controller and repositories, maybe on JSP page I made a mistake
Error -
Caused by: java.lang.IllegalArgumentException: Failed to create query for method public abstract adil.java.schoolmaven.entity.User adil.java.schoolmaven.repository.UserRepository.DeleteAccountById(java.lang.Long)! No property deleteAccountById found for type User!
at org.springframework.data.jpa.repository.query.PartTreeJpaQuery.<init>(PartTreeJpaQuery.java:82)
at org.springframework.data.jpa.repository.query.JpaQueryLookupStrategy$CreateQueryLookupStrategy.resolveQuery(JpaQueryLookupStrategy.java:103)
at org.springframework.data.jpa.repository.query.JpaQueryLookupStrategy$CreateIfNotFoundQueryLookupStrategy.resolveQuery(JpaQueryLookupStrategy.java:208)
at org.springframework.data.jpa.repository.query.JpaQueryLookupStrategy$AbstractQueryLookupStrategy.resolveQuery(JpaQueryLookupStrategy.java:79)
at org.springframework.data.repository.core.support.RepositoryFactorySupport$QueryExecutorMethodInterceptor.lookupQuery(RepositoryFactorySupport.java:553)
at org.springframework.data.repository.core.support.RepositoryFactorySupport$QueryExecutorMethodInterceptor.lambda$mapMethodsToQuery$1(RepositoryFactorySupport.java:546)
at java.util.stream.ReferencePipeline$3$1.accept(ReferencePipeline.java:193)
at java.util.Iterator.forEachRemaining(Iterator.java:116)
at java.util.Collections$UnmodifiableCollection$1.forEachRemaining(Collections.java:1049)
at java.util.Spliterators$IteratorSpliterator.forEachRemaining(Spliterators.java:1801)
at java.util.stream.AbstractPipeline.copyInto(AbstractPipeline.java:481)
at java.util.stream.AbstractPipeline.wrapAndCopyInto(AbstractPipeline.java:471)
at java.util.stream.ReduceOps$ReduceOp.evaluateSequential(ReduceOps.java:708)
at java.util.stream.AbstractPipeline.evaluate(AbstractPipeline.java:234)
at java.util.stream.ReferencePipeline.collect(ReferencePipeline.java:499)
at org.springframework.data.repository.core.support.RepositoryFactorySupport$QueryExecutorMethodInterceptor.mapMethodsToQuery(RepositoryFactorySupport.java:548)
at org.springframework.data.repository.core.support.RepositoryFactorySupport$QueryExecutorMethodInterceptor.lambda$new$0(RepositoryFactorySupport.java:538)
at java.util.Optional.map(Optional.java:215)
at org.springframework.data.repository.core.support.RepositoryFactorySupport$QueryExecutorMethodInterceptor.<init>(RepositoryFactorySupport.java:538)
at org.springframework.data.repository.core.support.RepositoryFactorySupport.getRepository(RepositoryFactorySupport.java:317)
at org.springframework.data.repository.core.support.RepositoryFactoryBeanSupport.lambda$afterPropertiesSet$3(RepositoryFactoryBeanSupport.java:287)
at org.springframework.data.util.Lazy.getNullable(Lazy.java:141)
at org.springframework.data.util.Lazy.get(Lazy.java:63)
at org.springframework.data.repository.core.support.RepositoryFactoryBeanSupport.afterPropertiesSet(RepositoryFactoryBeanSupport.java:290)
at org.springframework.data.jpa.repository.support.JpaRepositoryFactoryBean.afterPropertiesSet(JpaRepositoryFactoryBean.java:102)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.invokeInitMethods(AbstractAutowireCapableBeanFactory.java:1769)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.initializeBean(AbstractAutowireCapableBeanFactory.java:1706)
... 84 more
Caused by: org.springframework.data.mapping.PropertyReferenceException: No property deleteAccountById found for type User!
AdminController
@Controller
@RequestMapping("/admin")
public class AdminController {
@Autowired
private StudentService studentService;
@Autowired
private UserService userService;
@GetMapping("/allStudentsAdmin")
public ModelAndView allStudentsForUser() {
ModelAndView mv = new ModelAndView();
List<Student> studentList = studentService.getAllStudents();
mv.addObject("studentList", studentList);
mv.setViewName("allStudentsAdmin");
return mv;
}
@GetMapping(value = "/deleteStudent/{id}")
public ModelAndView deleteUserById(@PathVariable Long id) {
studentService.deleteStudentById(id);
ModelAndView mv = new ModelAndView("redirect:/admin/allStudentsAdmin");
return mv;
}
@GetMapping(value = "/editStudent/{id}")
public ModelAndView displayEditUserForm(@PathVariable Long id) {
ModelAndView mv = new ModelAndView("adminEditStudent");
Student student = studentService.getStudentById(id);
mv.addObject("headerMessage", "Редактирование студента");
mv.addObject("student", student);
return mv;
}
@PostMapping(value = "/editStudent")
public String saveEditedUser(
@RequestParam("id") Long id,
@RequestParam("name") String name,
@RequestParam("surname") String surname,
@RequestParam("avatar") MultipartFile file) {
try {
studentService.updateStudent(name, surname, file, studentService.getStudentById(id));
} catch (FileSystemException ex) {
ex.printStackTrace();
} catch (IOException e) {
return "redirect:/errors";
}
return "redirect:/admin/allStudentsAdmin";
}
@GetMapping(value = "/addStudentAdmin")
public ModelAndView displayNewUserForm() {
ModelAndView mv = new ModelAndView("addStudentAdmin");
mv.addObject("headerMessage", "Add Student Details");
mv.addObject("student", new Student());
return mv;
}
@PostMapping(value = "/addStudentAdmin")
public String saveNewStudent(@RequestParam("name") @NonNull String name,
@RequestParam("surname") @NonNull String surname,
@RequestParam("avatar") MultipartFile file)
throws IOException {
Student student = new Student();
student.setSurname(surname);
student.setName(name);
if (file != null && !file.isEmpty()) {
student.setAvatar(studentService.saveAvatarImage(file).getName());
}
studentService.saveStudent(student);
return "redirect:/admin/allStudentsAdmin";
}
@GetMapping(value = "/addUser")
public ModelAndView displayAddUserForm() {
ModelAndView mv = new ModelAndView("addUser");
mv.addObject("user", new User());
return mv;
}
@PostMapping(value = "/addUser", consumes = "multipart/form-data")
public String saveNewUser(@ModelAttribute User user) {
userService.saveUser(user);
return "redirect:/admin/allUsers";
}
@GetMapping("/allUsers")
public ModelAndView allUsers(@ModelAttribute User user) {
ModelAndView mv = new ModelAndView("allUsers");
List<User> users = userService.getAll();
mv.addObject("users", users);
return mv;
}
@GetMapping("/editUser/{id}")
public ModelAndView editUser(@PathVariable Long id) {
Optional<User> user = userService.findUser(id);
if (user.isPresent()) {
ModelAndView mv = new ModelAndView("editUser");
mv.addObject("user", user.get());
return mv;
}
return new ModelAndView("redirect:/admin/allUsers");
}
@PostMapping("/editUser")
public String saveEditedUser(@ModelAttribute User user) {
userService.updateUser(user);
return "redirect:/admin/allUsers";
}
@GetMapping(value = "/deleteUser/{id}")
public ModelAndView deleteClientById(@PathVariable Long id) {
userService.deleteAccountById(id);
ModelAndView mv = new ModelAndView("redirect:/admin/allUsers");
return mv;
}
}
UserService
import java.io.IOException;
import java.util.List;
import java.util.Optional;
public interface UserService {
User saveUser(User user);
List<User> getAll();
Optional<User> findUser(Long id);
User updateUser(User user);
boolean deleteAccountById(Long id);
}
UserServiceImpl
@Service
@Transactional
public class UserServiceImpl implements UserService {
@Autowired
private UserRepository repository;
@Autowired
public UserServiceImpl(UserRepository repository) {
super();
this.repository = repository;
}
@Override
public List<User> getAll() {
return (List<User>) repository.findAll();
}
@Override
public Optional<User> findUser(Long id) {
return repository.findById(id);
}
@Override
public User saveUser(User user) {
return repository.save(user);
}
@Override
public boolean deleteAccountById(Long id) {
try {
repository.deleteById(id);
return true;
} catch (Exception ex) {
return false;
}
}
@Override
public User updateUser(User user) {
User targetUser = repository.findById(user.getId()).get();
if (user.getLogin() != null) {
targetUser.setLogin(user.getLogin());
}
if (user.getRole() != null) {
targetUser.setRole(user.getRole());
}
if (user.getPassword() != null) {
targetUser.setPassword(user.getPassword());
}
return repository.save(targetUser);
}
}
AllUsers.JSP
<br>
<br>
<table class="table">
<thead>
<tr>
<th bgcolor="#000000"><font color="f5f5f5" scope="col"># </font></th>
<th bgcolor="#000000"><font color="f5f5f5" scope="col">Login</font></th>
<th bgcolor="#000000"><font color="f5f5f5" scope="col">Role</font></th>
<th bgcolor="#000000"><font color="f5f5f5" scope="col">Edit</font></th>
<th bgcolor="#000000"><font color="f5f5f5" scope="col">Delete</font></th>
</tr>
</thead>
<tbody>
<c:forEach var="user" items="${users}" varStatus="loop">
<tr>
<th bgcolor="#000000"><font color="f5f5f5" scope="row">${loop.index+1}</font></th>
<td bgcolor="#000000"><font color="f5f5f5">${user.login}</font></td>
<td bgcolor="#000000"><font color="f5f5f5">${user.role}</font></td>
<td bgcolor="#000000">
<a href="${pageContext.request.contextPath}/admin/editUser/${user.id}">
<button type="button" class="btn btn-primary">Edit</button>
</a>
</td>
<td bgcolor="#000000">
<a href="${pageContext.request.contextPath}/admin/deleteUser/${user.id}">
<button type="button" class="btn btn-primary">Delete</button>
</a>
</td>
</tr>
</c:forEach>
</tbody>
</table>
Aucun commentaire:
Enregistrer un commentaire