我在数据库中有一个包含这些列的表:
id serial NOT NULL,
name character varying(255) NOT NULL,
url character varying(255)
实体:
@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为我生成了JPA实体。我在数据库中有5条记录。当我em.createNamedQuery("Topmenu.findAll").getResultList();
时,我得到一个包含5个条目的List<Topmenu>
,但是当我执行以下操作时,不会返回任何内容:
for(Topmenu t : em.createNamedQuery("Topmenu.findAll").getResultList()){
logger.warn(t.getName());
}
命名查询:
@NamedQuery(name = "Topmenu.findAll", query = "Topmenu t FROM Topmenu t")
输出:
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) -
答案 0 :(得分:0)
根据我的经验,当你没有使用TypedQueries并且需要它们时会出现一些问题。
使用
尝试使用代码for(Topmenu t : em.createNamedQuery("Topmenu.findAll", Topmenu.class).getResultList()){
logger.warn(t.getName());
}
使用additional参数,createNamedQuery将返回TypedQuery而不是Query。
让我知道它是否有帮助:)
答案 1 :(得分:0)
这与实体或EntityManager无关,而是与EclipseLink编织的事实有关。我忘记了一个细节,即获得Entities的EJB是一个远程EJB。因此序列化对象时会出现错误,导致TopmenuBean获取对象,但无法对其进行反序列化。
问题现在解决了