Я пытаюсь выполнить namedQuery из объекта Item
, но это приводит к ошибке, указывающей, что namedQuery не может быть найден.NamedQuery не найден
Я также пытался получить другие namedQuery, но они также не могли быть найдены.
Сущность с именованным запросом.
@Entity
@NamedQueries({
@NamedQuery(name = "Item.getAll", query = "select a from Item as a"),
@NamedQuery(name = "Item.count", query = "select count(a) from Item as a"),
@NamedQuery(name = "Item.findByDescription", query = "select a from Item as a where a.description = :descriptions")
})
public class Item implements Comparable, Serializable {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Long id;
@OneToOne
@JoinColumn(name="email")
private User seller;
private Category category;
private String description;
@OneToOne
private Bid highest;
public Item(User seller, Category category, String description) {
this.seller = seller;
this.category = category;
this.description = description;
}
public Item() {
}
(Getters and setters)
Где я пытаюсь найти namedQuery из Item.
EntityManagerFactory emf = Persistence.createEntityManagerFactory("auctionPU");
public List<Item> findByDescription(String description) {
EntityManager em = emf.createEntityManager();
em.getTransaction().begin();
try {
Query q = em.createNamedQuery("Item.findByDescription", Item.class);
q.setParameter("descriptions", description);
List<Item> a = q.getResultList();
em.close();
return a;
}
catch (NoResultException e) {
return null;
}
finally {
em.close();
}
}
post pe rsistence.xml –
Вы сделали это, забыли добавить Enity в persistence.xml – Davidto