找不到命名查询

时间:2013-11-08 14:57:57

标签: java hibernate ejb

我使用EJB 3,我试图从数据库中获取一个简单的字符串,但是我收到了这条消息

  

未找到命名查询:SELECT tr FROM Travauxdereseauurbain tr

我并不真正明白这意味着什么。

这是实体

@Entity
@Table(name="Travauxdereseauurbain")
public class Traveauxdereseauurbain implements Serializable {
    private static final long serialVersionUID = 1L;

@Id
@Column(name="idtru")
private int idtru;
@Column(name ="article")
private String article;
@Column (name="designationtraveau")
private String designationtraveau;
@Column(name="unite")
private String unite;
@Column(name="prixHTVA")
private float prixHTVA;
@Column(name="prixTTC")
private float prixTTC;
@Column (name="qtt")
private float qtt;
@Column(name="montantHTVA")
private float montantHTVA;
@Column(name="montantTTC")
private float montantTTC;


///GETTERS, SETTERS AND CONSTRUCTOR   
}

和DAO类

@Stateless
public class TravauxdereseauurbainDAO {     

    @PersistenceContext
    private EntityManager em;       

    public Traveauxdereseauurbain affichernimpr()
    {


      Query q =em.createNamedQuery("SELECT tr FROM Travauxdereseauurbain tr");

      return  (Traveauxdereseauurbain) q.getResultList().get(0);
    }
}

最后错误信息是:

  

Servlet.service()“pour la servlet Faces Servletagénéréneexception:java.lang.IllegalArgumentException:找不到命名查询:SELECT tr FROM Travauxdereseauurbain tr

提前让你

3 个答案:

答案 0 :(得分:2)

你没有调用正确的方法。您需要致电em.createQuery

答案 1 :(得分:0)

您正在使用createNamedQuery检索预编译的HQL以执行查询。 您可以将查询的名称和查询内容放在域对象上,例如

@NamedQuery(name = "aName", query="aHql")
public class Traveauxdereseauurbain implements Serializable {

然后在你的道歉

em.createNamedQuery("aName);

答案 2 :(得分:-1)

您的查询返回对象列表

 public List<Traveauxdereseauurbain> affichernimpr() {       
            Query query = em.createNamedQuery(""SELECT tr FROM Travauxdereseauurbain tr");
            return (List<Traveauxdereseauurbain>) query.getResultList();