gpt4 book ai didi

mysql - 异常(exception)是 org.hibernate.hql.internal.ast.QuerySyntaxException : books is not mapped [FROM books. ..

转载 作者:行者123 更新时间:2023-11-29 18:17:28 28 4
gpt4 key购买 nike

我正在尝试制作 CRUD+spring 应用程序来创建/删除/更新书籍。一切正常,但从数据库中搜索一本书。请帮忙。

@Controller
public class BookController {
...................
@RequestMapping("searchBook")
public ModelAndView searchBook(@RequestParam("searchBook") String title){
List<Book> booksList = bookService.getAllBooks(title);
return new ModelAndView("booksearch", "booksList", booksList);
}
}

有一个 books.jsp: ......................
返回主菜单

按标题搜索书籍:



ID 标题 作者 描述 国际标准书号 打印年份 已阅读 编辑 删除 ${图书.id} ${book.bookTitle} ${book.bookAuthor} ${书本.描述} ${书.isbn} ${book.printYear} ${book.readAlready} ">编辑 ">删除 **我的项目的结构是: http://clip2net.com/s/3ON3F6n

There's a getAllBooks() method from BookDaoimpl:**

@SuppressWarnings("unchecked")
@Override
public List<Book> getAllBooks(String bookName) {
Session session = this.sessionFactory.getCurrentSession();
String query = "SELECT * FROM books WHERE BOOK_TITLE like '%" +
bookName + "%'";
List<Object[]> bookObjects = session.createQuery(query).list();
List<Book> books = new ArrayList<Book>();
for (Object[] bookObject : bookObjects) {
Book book = new Book();
int id = (int) bookObject[0];
String title = (String) bookObject[1];
String description = (String) bookObject[2];
String author = (String) bookObject[3];
int isbn = (int) bookObject[4];
int printYear = (int) bookObject[5];
int readAlready = (int) bookObject[6];
book.setId(id);
book.setBookTitle(title);
book.setDescription(description);
book.setBookAuthor(author);
book.setIsbn(isbn);
book.setPrintYear(printYear);
book.setReadAlready(readAlready);
books.add(book);
}
System.out.println(books);

return books;
}

有一个 MySQL 数据库:http://clip2net.com/s/3ON4e7o和这个http://clip2net.com/s/3OPyIHL

My Book.class:
@Entity
@Table(name = "books")
public class Book {

@Id
@Column(name = "ID")
@GeneratedValue(strategy = GenerationType.IDENTITY)
private int id;

@Column(name = "BOOK_TITLE")
private String bookTitle;

@Column(name = "BOOK_AUTHOR")
private String bookAuthor;

@Column(name = "BOOK_DESCRIPTION")
private String description;

@Column(name = "ISBN")
private int isbn;

@Column(name = "PRINTYEAR")
private int printYear;

@Column(name = "READALREADY")
private int readAlready;
......................................


**My mvc-dispatcher-servlet.xml:**
<!-- Specifying base package of the Components like Controller, Service, DAO -->
<context:component-scan base-package="ru.maxim.bookmanager" />

<!-- View Resolver -->
<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix" value="/WEB-INF/pages/" />
<property name="suffix" value=".jsp" />
</bean>

<!-- DataBase Information -->
<bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource"
destroy-method="close">
<property name="driverClassName" value="com.mysql.jdbc.Driver" />
<property name="url" value="jdbc:mysql://localhost:3306/bookmanager" />
<property name="username" value="root" />
<property name="password" value="root" />
</bean>

<!-- Hibernate & SessionFactory Bean definition-->
<bean id="hibernate4AnnotatedSessionFactory"
class="org.springframework.orm.hibernate4.LocalSessionFactoryBean">
<property name="dataSource" ref="dataSource"></property>
<property name="annotatedClasses">
<list>
<value>ru.maxim.bookmanager.model.Book</value>
</list>
</property>
<property name="hibernateProperties">
<props>
<prop key="hibernate.dialect">org.hibernate.dialect.MySQLDialect</prop>
<prop key="hibernate.show_sql">true</prop>
</props>
</property>
</bean>

<!-- BookDao & BookSession beans -->
<bean id="bookDao" class="ru.maxim.bookmanager.dao.BookDaoImpl">
<property name="sessionFactory" ref="hibernate4AnnotatedSessionFactory"></property>
</bean>

<bean id="bookService" class="ru.maxim.bookmanager.service.BookServiceImpl">
<property name="bookDao" ref="bookDao"></property>
</bean>

<context:component-scan base-package="ru.maxim.bookmanager" />
<tx:annotation-driven transaction-manager="transactionManager" />

<!-- Transaction -->
<bean id="transactionManager"
class="org.springframework.orm.hibernate4.HibernateTransactionManager">
<property name="sessionFactory" ref="hibernate4AnnotatedSessionFactory" />
</bean>
<mvc:default-servlet-handler/>
<mvc:annotation-driven/>

如果我将查询更改为“SELECT Book FROM Book b”之类的内容,我会收到一个新的异常:请求处理失败;嵌套异常是 java.lang.ClassCastException: ru.maxim.bookmanager.model.Book 无法转换为 [Ljava.lang.Object;

出了什么问题?谢谢!

最佳答案

我认为您从数据库获取数据的方法是错误的。

您有两种选择来解决该错误:-

  • 使用 SQL

    Session session = this.sessionFactory.getCurrentSession();
    String query = "SELECT * FROM books WHERE BOOK_TITLE like '%" + bookName + "%'";
    List<Object[]> bookObjects = session.createSQLQuery(query).list();
    List<Book> books = new ArrayList<Book>();
    //rest code snippet will be same as your code snippet .
  • 使用 native Hql

    String book_title="book title name";
    org.hibernate.Query que=session.createQuery("from Book where bookTitle like ?");
    que.setString(0,"%"+book_title+"%");
    //rest will be same as your code snippet

    这里在 native HQL 中使用实体名称 (BOOK) 而不是表名称 (BOOK_TITLE),并且将使用就地列名称 (BOOK_TITLE) 类变量名称 (bookTitle)

关于mysql - 异常(exception)是 org.hibernate.hql.internal.ast.QuerySyntaxException : books is not mapped [FROM books. ..,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46901768/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com