JavaEE查询两张表
Java EE Query two tables
所以这是我的问题:
我有两个 table:User 和 Book,它们是 ManyToOne 关系。 Book
table 具有名为 user_id
的属性,它连接两个 table。
使用 Eclipse 我生成了实体 类 并在它们上工作直到现在都没有问题。
问题是,当我想获得具有特定 user_id
的 "books" 时,出现错误:
java.lang.IllegalArgumentException: Parameter value [1] did not match expected type [model.User (n/a)]
"value" 是我从会话中得到的一个 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;
非常感谢您的帮助。
我认为查询应该是 select b from Book b WHERE b.user.idUser = :id
您可能想看看休眠文档
https://docs.jboss.org/hibernate/orm/3.6/reference/en-US/html/queryhql.html {第 16 章}
你有两个选择。
1) 你将一个带有 id 作为参数的用户对象传递给查询(我更喜欢那个解决方案):
Query query = em.createQuery("select b from Book b WHERE b.user = :user");
User u = new User();
u.setId(id)
query.setParameter("user", u);
2) 您将 id 作为参数传递:
Query query = em.createQuery("select b from Book b WHERE b.user.id = :id");
if (id!= null) {
query.setParameter("id", id);
}
请注意查询中的b.user.id
所以这是我的问题:
我有两个 table:User 和 Book,它们是 ManyToOne 关系。 Book
table 具有名为 user_id
的属性,它连接两个 table。
使用 Eclipse 我生成了实体 类 并在它们上工作直到现在都没有问题。
问题是,当我想获得具有特定 user_id
的 "books" 时,出现错误:
java.lang.IllegalArgumentException: Parameter value [1] did not match expected type [model.User (n/a)]
"value" 是我从会话中得到的一个 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;
非常感谢您的帮助。
我认为查询应该是 select b from Book b WHERE b.user.idUser = :id
您可能想看看休眠文档
https://docs.jboss.org/hibernate/orm/3.6/reference/en-US/html/queryhql.html {第 16 章}
你有两个选择。
1) 你将一个带有 id 作为参数的用户对象传递给查询(我更喜欢那个解决方案):
Query query = em.createQuery("select b from Book b WHERE b.user = :user");
User u = new User();
u.setId(id)
query.setParameter("user", u);
2) 您将 id 作为参数传递:
Query query = em.createQuery("select b from Book b WHERE b.user.id = :id");
if (id!= null) {
query.setParameter("id", id);
}
请注意查询中的b.user.id