我四处寻找答案,看起来它与思考有关,但由于我还没有处理它们,我不确定如何继续。我正在尝试构建一个HQL查询,该查询只返回一个表中的行,该表将一个FK列与另一个表中的PK匹配。
这是我的代码:
实体书:
@Entity
@Table(name="book")
public class Book {
@Id
private String bookNumber;
private String bookName;
private String bookDescription;
private double bookPrice;
@ManyToOne(cascade=CascadeType.ALL)
@JoinColumn(name="shelfnumber")
Shelf shelf;
实体货架:
@Entity
@Table(name="shelf")
public class Shelf {
@Id
private String shelfNumber;
@OneToMany(mappedBy="shelf",cascade=CascadeType.ALL)
Set<Book> shelfBooks;
注意:两个实体类都有getter / setters
ActionClass:
DynaActionForm searchForm =((DynaActionForm)form);
String shelfNum = searchForm.getString("shelfNum");
SessionFactory sessionFactory = new Configuration().configure().buildSessionFactory();
Session session = sessionFactory.openSession();
session.beginTransaction();
try{
session.getEntityMode();
Query query = session.createQuery("FROM Book B WHERE B.shelf = :shelfNum"); //here is where the error happens: B.shelf is the problem but don't know why
query.setParameter("shelfNum", shelfNum);
List books = query.list();
request.setAttribute("books", books);
} finally{
session.close();
}
我收到错误消息:
org.hibernate.PropertyAccessException: could not get a field value by reflection getter of com.library.entity.Shelf.shelfNumber
进一步下来我看到了:
Caused by: java.lang.IllegalArgumentException: Can not set java.lang.String field com.library.entity.Shelf.shelfNumber to java.lang.String
感谢您的帮助!
答案 0 :(得分:4)
您的查询将Shelf
的实例与String
的实例进行比较。那不行。 Shelf
是Shelf
。它不是String
。
查询应为
select b from Book b where b.shelf.shelfNumber = :shelfNum
另外,请告诉我你实际上并没有在每个请求中重新创建一个SessionFactory!
请stop using raw types。我们不再是2003年了。