6 Replies Latest reply on Nov 9, 2006 10:14 AM by Arnaud Beausoleil

    problem with EJB3 QL

    tamri boss Newbie

      I use EJB3 , I have query:
      em.createQuery("delete from Assettypes asset WHERE asset.id= :id").
      setParameter("id",idd);
      when i run this query, jboss throws WARN
      (
      WARN [FromElementType] Using non-qualified column reference [id -<[id]>]
      )
      and it doen's execute.

      the @Entity Assettypes is created, and it has a field id correcond to column from table.

      why it dos't work, what reazon is it?

        • 1. Re: problem with EJB3 QL
          Chris Malan Novice

          Sorry, can't help you with the EJBQL. But there is another way you can do this. You are using the id (PK).

          You can do:

          Assettypes type = entityManager.find(Assettypes.class, id);
          entityManager.remove(type);
          


          That will accomplish the same.

          • 2. Re: problem with EJB3 QL
            Cyril JOUI Newbie

            Can you give us the Assettypes class code ?
            Do you have an "id" property ?

            • 3. Re: problem with EJB3 QL
              tamri boss Newbie

               

              "cyril.joui@supinfo.com" wrote:
              Can you give us the Assettypes class code ?
              Do you have an "id" property ?


              yes, I have, now I'll trye so, if it dosn't work, I'll post you code

              Assettypes type = entityManager.find(Assettypes.class, id);
              entityManager.remove(type);



              • 4. Re: problem with EJB3 QL
                tamri boss Newbie

                 

                "cyril.joui@supinfo.com" wrote:
                Can you give us the Assettypes class code ?
                Do you have an "id" property ?

                this is code:

                @Entity(access=PROPERTY)
                @NamedQuery(name="findAllAssettypes", query="select object(o) from Assettypes o")
                @Table(name="AssetTypes")
                public class Assettypes implements Serializable {
                private long chartId;
                private String code;
                private String description;
                private long id;
                private long parentId;
                // private Collection items;

                public Assettypes() {
                }

                @Column(name="CHART_ID")
                public long getChartId() {
                return chartId;
                }

                public void setChartId(long chartId) {
                this.chartId = chartId;
                }

                public String getCode() {
                return code;
                }

                public void setCode(String code) {
                this.code = code;
                }

                public String getDescription() {
                return description;
                }

                public void setDescription(String description) {
                this.description = description;
                }
                @Id @GeneratedValue(strategy=GenerationType.IDENTITY)
                public long getId() {
                return id;
                }

                public void setId(long id) {
                this.id = id;
                }

                @Column(name="PARENT_ID")
                public long getParentId() {
                return parentId;
                }

                public void setParentId(long parentId) {
                this.parentId = parentId;
                }


                • 5. Re: problem with EJB3 QL
                  tamri boss Newbie

                  I have a the same problem with 'update' query. when I write
                  entityManager.createQuery("update Iteminfo item set item.assetType="+aaa+" where item.id="+id);
                  there is the same problem:
                  jboss throws WARN
                  (
                  WARN [FromElementType] Using non-qualified column reference [id -<[id]>]
                  )
                  and it doen's execute.

                  when you gove me advise , in this case, it is'not good advise because find(...) method must return a list, so I have to use for ( ) operator and then in this for write entityManager.merge(...).
                  is it enythins else that I do more friendly code?I mean without entityManager.find(...) and for operator?
                  Or how can I write EJBQL correctly, that it works.
                  can enyone to help me?

                  • 6. Re: problem with EJB3 QL
                    Arnaud Beausoleil Newbie

                    Sure, i guess you have forgotten to do the transaction management on it :

                    for example this works :

                    EntityManager manager = getJpaTemplate().getEntityManagerFactory().createEntityManager();

                    try {
                    manager.getTransaction().begin();

                    Query query = manager.createQuery("UPDATE Profile p SET p.status=?1 WHERE p.status=?2");
                    query.setParameter(1, statusReplacing);
                    query.setParameter(2, statusToReplace);
                    query.executeUpdate();

                    manager.getTransaction().commit();
                    }
                    finally {
                    manager.close();
                    }
                    }

                    Not that with others providers, the begin/commit is not always necessary, this is why it's confusing. But the hibernate way seems the most logical.