【发布时间】:2016-02-19 13:37:40
【问题描述】:
所以这是我的问题:
我有两个表:User 和 Book,它们是 ManyToOne 关系。 Book 表具有名为user_id 的属性,用于连接两个表。
使用 Eclipse,我生成了实体类,并且直到现在都可以毫无问题地处理它们。
问题是,当我想获得具有特定 user_id 的“书籍”时,我得到一个错误:
java.lang.IllegalArgumentException: Parameter value [1] did not match expected type [model.User (n/a)]
“值”是我从会话中获取的 id,我在 int 和 String 中都试过了。
BookDao 的一部分:
public List<Book> getFullListWithId(Integer id) {
List<Book> list = null;
Query query = em.createQuery("select b from Book b WHERE b.user = :id");
if (id!= null) {
query.setParameter("id", id);
}
try {
list = query.getResultList();
} catch (Exception e) {
e.printStackTrace();
}
return list;
}
BookBB 的一部分:
public List<Book> getFullListWithId(){
HttpSession session = (HttpSession) FacesContext.getCurrentInstance().getExternalContext().getSession(true);
Integer str =(Integer)session.getAttribute("userId");
return bookDAO.getFullListWithId(str);
}
Book.java 的一部分
@Id
@GeneratedValue(strategy=GenerationType.IDENTITY)
@Column(name="id_offer")
private int idOffer;
private String adress;
private String contact;
@Column(name="is_ready")
private String isReady;
private String name;
private String others;
private int price;
private int rooms;
private String size;
private String surname;
//bi-directional many-to-one association to User
@ManyToOne
@JoinColumn(name="user_id")
private User user;
User.java 的一部分
@Id
@GeneratedValue(strategy=GenerationType.IDENTITY)
@Column(name="id_user")
private int idUser;
private String login;
private String password;
//bi-directional many-to-one association to Book
@OneToMany(mappedBy="user")
private List<Book> books;
非常感谢您提供的任何帮助。
【问题讨论】: