这似乎是重复的问题,但我无法找到我正在寻找的答案,而且我的问题略有不同所以请在标记或复制之前耐心等待 -
我有一个保存和删除功能,它使Book Class对象执行插入和删除操作,并且Spring以某种方式将其作为Book类的属性,是否有任何特定的方法来实现 insert 和使用我缺少的CrudRepository时的删除操作,我几乎已经尝试了所有可用的答案但没有帮助,请你指导我解决这个问题。
这是我目前面临的一个例外,下面给出了代码 -
OrderDesctiption = oReader.GetNString(3),
我的实体类
org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'booksController': Unsatisfied dependency expressed through field 'bookService'; nested exception is org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'bookServiceImpl': Unsatisfied dependency expressed through field 'booksDao'; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'booksDao': Invocation of init method failed; nested exception is org.springframework.data.mapping.PropertyReferenceException: No property book found for type Books!
我的DAO界面
package com.nerdbot.lms.model;
import java.sql.Timestamp;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
@Entity
public class Books {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Long buid;
private String book_Name;
private String isbn;
private String author;
private boolean avialable;
private String teachers_note;
private String suid;
private Timestamp return_date;
private String meta_tags;
private boolean award_winner;
private String subject;
private boolean banned_in_any_country;
private String country_of_ban;
private String aisle;
public String getIsbn() {
return isbn;
}
public void setIsbn(String isbn) {
this.isbn = isbn;
}
public boolean isAvialable() {
return avialable;
}
public void setAvialable(boolean avialable) {
this.avialable = avialable;
}
public String getMeta_tags() {
return meta_tags;
}
public void setMeta_tags(String meta_tags) {
this.meta_tags = meta_tags;
}
public boolean isAward_winner() {
return award_winner;
}
public void setAward_winner(boolean award_winner) {
this.award_winner = award_winner;
}
public String getSubject() {
return subject;
}
public void setSubject(String subject) {
this.subject = subject;
}
public String getCountry_of_ban() {
return country_of_ban;
}
public void setCountry_of_ban(String country_of_ban) {
this.country_of_ban = country_of_ban;
}
public String getAisle() {
return aisle;
}
public void setAisle(String aisle) {
this.aisle = aisle;
}
public String getSuid() {
return suid;
}
public void setSuid(String suid) {
this.suid = suid;
}
public Long getBuid() {
return buid;
}
public void setBuid(Long buid) {
this.buid = buid;
}
public String getAuthor() {
return author;
}
public void setAuthor(String author) {
this.author = author;
}
public String getBook_Name() {
return book_Name;
}
public void setBook_Name(String book_Name) {
this.book_Name = book_Name;
}
public String getTeachers_note() {
return teachers_note;
}
public void setTeachers_note(String teachers_note) {
this.teachers_note = teachers_note;
}
public Timestamp getReturn_date() {
return return_date;
}
public void setReturn_date(Timestamp return_date) {
this.return_date = return_date;
}
public boolean isBanned_in_any_country() {
return banned_in_any_country;
}
public void setBanned_in_any_country(boolean banned_in_any_country) {
this.banned_in_any_country = banned_in_any_country;
}
}
我的服务界面
package com.nerdbot.lms.dao;
import org.springframework.data.repository.CrudRepository;
import org.springframework.stereotype.Repository;
import com.nerdbot.lms.model.Books;
@Repository
public interface BooksDao extends CrudRepository<Books, Long>{
Iterable<Books> findAll();
Iterable<Books> findBybuid(int buid);
Iterable<Books> findByAuthor(String author);
Iterable<Books> findByMeta_Tags(String meta_tags);
Iterable<Books> findByBook_Name(String book_Name);
Iterable<Books> findByIsbn(String isbn);
Iterable<Books> findBySubject(String subject);
}
我的休息控制器
package com.nerdbot.lms.service;
import com.nerdbot.lms.model.Books;
public interface BookService {
Iterable<Books> findAll();
Iterable<Books> findBybuid(int buid);
Iterable<Books> findByAuthor(String author);
Iterable<Books> findByMeta_Tags(String meta_tags);
Iterable<Books> findByBookName(String book_Name);
Iterable<Books> findByIsbn(String isbn);
Iterable<Books> findBySubject(String subject);
void save( Iterable<Books> books);
void save(Books book);
void delete(Iterable<Books> books);
void delete(Books book);
}
完全例外
package com.nerdbot.lms.controller;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import com.nerdbot.lms.model.Books;
import com.nerdbot.lms.service.BookService;
@RestController
public class BooksController {
@Autowired
BookService bookService;
@RequestMapping("/getallbooks")
public Iterable<Books> getAllBooks(Model model) {
Iterable<Books> books = bookService.findAll();
model.addAttribute("books", books);
return books;
}
@RequestMapping(value = "/addbook",headers = {
"content-type=application/json" }, consumes = MediaType.APPLICATION_JSON_VALUE, method = RequestMethod.POST)
public void addBook(@RequestBody Books book) {
bookService.save(book);
}
@RequestMapping("/getbyname")
public Iterable<Books> getBookByName(@RequestParam(value="bookName", defaultValue="all") String book_Name) {
return bookService.findByBookName(book_Name);
}
}
答案 0 :(得分:2)
问题在于Repository
方法
Iterable<Books> findByBook_Name(String book_Name);
请参阅此link
我们应该避免将下划线写为JPA实体中的数据成员,因为_
在Spring JPA存储库中具有特殊含义。
JPA认为您在book
内有一个名为Books
的媒体资源,而您正在追溯到book.name