我尝试从实体Item
执行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)
我试图从Item中找到namedQuery。
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();
}
}
答案 0 :(得分:0)
配置不正确时会显示此错误。您使用的是弹簧启动还是弹簧? 您似乎没有扫描您命名查询所在的软件包,扫描
<property name="packagesToScan" value="your_base_package_of_model_pojo.**"/>
OR
在春季启动
中添加
@EntityScan("your_base_package_of_model_pojo")
到您的配置类