Reputation: 548
I looked around for answers on this and it looks like it's something to do with reflections but since I haven't dealt with them I'm not sure how to proceed. I'm trying to build a HQL query that returns only rows from a table that match a column that is a FK to a PK in another table.
Here's what my code looks like:
Entity Book:
@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 Shelf:
@Entity
@Table(name="shelf")
public class Shelf {
@Id
private String shelfNumber;
@OneToMany(mappedBy="shelf",cascade=CascadeType.ALL)
Set<Book> shelfBooks;
Note: both entity classes have getters/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();
}
Error message I'm getting:
org.hibernate.PropertyAccessException: could not get a field value by reflection getter of com.library.entity.Shelf.shelfNumber
and further down I see:
Caused by: java.lang.IllegalArgumentException: Can not set java.lang.String field com.library.entity.Shelf.shelfNumber to java.lang.String
Thank you for any help!
Upvotes: 4
Views: 6594
Reputation: 691635
Your query compares an instance of Shelf
with an instance of String
. That can't work. A Shelf
is a Shelf
. It's not a String
.
The query should be
select b from Book b where b.shelf.shelfNumber = :shelfNum
Also, please tell me you don't actually recreate a SessionFactory at each and every request!
And please stop using raw types. We're not in 2003 anymore.
Upvotes: 4