2

I have a table in database with these columns:

   id serial NOT NULL, 
   name character varying(255) NOT NULL, 
   url character varying(255)

The Entity:

    @Entity
    @Table(name = "Topmenu")
    @XmlRootElement
    @NamedQueries({
        @NamedQuery(name = "Topmenu.findAll", query = "SELECT t FROM Topmenu t"),
        @NamedQuery(name = "Topmenu.findById", query = "SELECT t FROM Topmenu t WHERE t.id = :id")})
    public class Topmenu implements Serializable {

        private static final long serialVersionUID = 1L;
        @Id
        @GeneratedValue(strategy = GenerationType.IDENTITY)
        @Basic(optional = false)
        @Column(name = "id")
        private Integer id;
        @Basic(optional = false)
        @Column(name = "name")
        private String name;
        @Column(name = "url")
        private String url;

        public Topmenu() {
        }

        public Topmenu(Integer id) {
            this.id = id;
        }

        public Topmenu(Integer id, String name) {
            this.id = id;
            this.name = name;
        }

        public Integer getId() {
            return id;
        }

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

        public String getName() {
            return name;
        }

        public void setName(String name) {
            this.name = name;
        }

        public String getUrl() {
            return url;
        }

        public void setUrl(String url) {
            this.url = url;
        }

        @Override
        public int hashCode() {
            int hash = 0;
            hash += (id != null ? id.hashCode() : 0);
            return hash;
        }

        @Override
        public boolean equals(Object object) {
            // TODO: Warning - this method won't work in the case the id fields are not set
            if(!(object instanceof Topmenu)) {
                return false;
            }
            Topmenu other = (Topmenu) object;
            return !((this.id == null && other.id != null) || (this.id != null && !this.id.equals(other.id)));
        }

        @Override
        public String toString() {
            return "erates.entity.Topmenu[ id=" + id + " ]";
        }
}

Netbeans generated the JPA Entity for me. I have 5 records in the database. When I do em.createNamedQuery("Topmenu.findAll").getResultList(); I get a List<Topmenu> with 5 entries, but when I do the following, nothing is returned:

for(Topmenu t : em.createNamedQuery("Topmenu.findAll").getResultList()){
    logger.warn(t.getName());
}

The Named query:

@NamedQuery(name = "Topmenu.findAll", query = "Topmenu t FROM Topmenu t")

Output:

2015-06-01 11:23:27,024 WARN erates.beans.TopmenuBean (TopmenuBean.java:33) -
2015-06-01 11:23:27,034 WARN erates.beans.TopmenuBean (TopmenuBean.java:33) -
2015-06-01 11:23:27,045 WARN erates.beans.TopmenuBean (TopmenuBean.java:33) -
2015-06-01 11:23:27,055 WARN erates.beans.TopmenuBean (TopmenuBean.java:33) -
2015-06-01 11:23:27,066 WARN erates.beans.TopmenuBean (TopmenuBean.java:33) -
Unihedron
  • 10,902
  • 13
  • 62
  • 72
Erates
  • 646
  • 1
  • 9
  • 24

2 Answers2

0

In my experience, some problems occur when you don't used TypedQueries and need them to be.

Try your code with

for(Topmenu t : em.createNamedQuery("Topmenu.findAll", Topmenu.class).getResultList()){
    logger.warn(t.getName());
}

With the additional parameter, the createNamedQuery will return a TypedQuery instead of a Query.

Let me know if it helps :)

Delfic
  • 167
  • 2
  • 15
0

This had nothing to do with entities or EntityManager, but with the fact that EclipseLink was weaving. I forgot one detail, being that the EJB getting the Entities was a Remote EJB. So there were errors while Serializing the objects, causing the TopmenuBean getting objects, but wasn't able to deserialize them.

Problem is solved now

Erates
  • 646
  • 1
  • 9
  • 24