2017-02-27 10 views
0

のタイプである静的なコンテキストから参照することはできません、私は私のサーブレットを経由して名前付きクエリProperty.findAllを取得しようとしているが、私は問題を抱えています。 この非静的メソッドは、こんにちは、私はEJB(NetBeansの)に新たなんだTが変数

非静的メソッドは、T が可変

AbstractFacadeクラス

public abstract class AbstractFacade<T> { 

    private Class<T> entityClass; 

    public AbstractFacade(Class<T> entityClass) { 
     this.entityClass = entityClass; 
    } 

    protected abstract EntityManager getEntityManager(); 

    public void create(T entity) { 
     getEntityManager().persist(entity); 
    } 

    public void edit(T entity) { 
     getEntityManager().merge(entity); 
    } 

    public void remove(T entity) { 
     getEntityManager().remove(getEntityManager().merge(entity)); 
    } 

    public T find(Object id) { 
     return getEntityManager().find(entityClass, id); 
    } 

    public List<T> findAll() { 
     javax.persistence.criteria.CriteriaQuery cq = getEntityManager().getCriteriaBuilder().createQuery(); 
     cq.select(cq.from(entityClass)); 
     return getEntityManager().createQuery(cq).getResultList(); 
    } 

    public List<T> findRange(int[] range) { 
     javax.persistence.criteria.CriteriaQuery cq = getEntityManager().getCriteriaBuilder().createQuery(); 
     cq.select(cq.from(entityClass)); 
     javax.persistence.Query q = getEntityManager().createQuery(cq); 
     q.setMaxResults(range[1] - range[0] + 1); 
     q.setFirstResult(range[0]); 
     return q.getResultList(); 
    } 

    public int count() { 
     javax.persistence.criteria.CriteriaQuery cq = getEntityManager().getCriteriaBuilder().createQuery(); 
     javax.persistence.criteria.Root<T> rt = cq.from(entityClass); 
     cq.select(getEntityManager().getCriteriaBuilder().count(rt)); 
     javax.persistence.Query q = getEntityManager().createQuery(cq); 
     return ((Long) q.getSingleResult()).intValue(); 
    } 

} 

エンティティクラス(プロパティ)

@Entity 
@Table(name = "property") 
@XmlRootElement 
@NamedQueries({ 
    @NamedQuery(name = "Property.findAll", query = "SELECT p FROM Property p") 
    , @NamedQuery(name = "Property.findByPropertyId", query = "SELECT p FROM Property p WHERE p.propertyId = :propertyId") 
    , @NamedQuery(name = "Property.findByPropertyType", query = "SELECT p FROM Property p WHERE p.propertyType = :propertyType") 
    , @NamedQuery(name = "Property.findByNumOfBedroom", query = "SELECT p FROM Property p WHERE p.numOfBedroom = :numOfBedroom") 
    , @NamedQuery(name = "Property.findByNumOfBathroom", query = "SELECT p FROM Property p WHERE p.numOfBathroom = :numOfBathroom") 
    , @NamedQuery(name = "Property.findByAddress", query = "SELECT p FROM Property p WHERE p.address = :address") 
    , @NamedQuery(name = "Property.findByDescription", query = "SELECT p FROM Property p WHERE p.description = :description") 
    , @NamedQuery(name = "Property.findByFurnish", query = "SELECT p FROM Property p WHERE p.furnish = :furnish") 
    , @NamedQuery(name = "Property.findByGarden", query = "SELECT p FROM Property p WHERE p.garden = :garden") 
    , @NamedQuery(name = "Property.findByArea", query = "SELECT p FROM Property p WHERE p.area = :area") 
    , @NamedQuery(name = "Property.findByBuyType", query = "SELECT p FROM Property p WHERE p.buyType = :buyType") 
    , @NamedQuery(name = "Property.findByPropertyPrice", query = "SELECT p FROM Property p WHERE p.propertyPrice = :propertyPrice")}) 
public class Property implements Serializable { 

    private static final long serialVersionUID = 1L; 
    @Id 
    @GeneratedValue(strategy = GenerationType.IDENTITY) 
    @Basic(optional = false) 
    @Column(name = "PropertyId") 
    private Integer propertyId; 
    @Size(max = 45) 
    @Column(name = "PropertyType") 
    private String propertyType; 
    @Column(name = "NumOfBedroom") 
    private Long numOfBedroom; 
    @Column(name = "NumOfBathroom") 
    private Long numOfBathroom; 
    @Size(max = 250) 
    @Column(name = "Address") 
    private String address; 
    @Size(max = 500) 
    @Column(name = "Description") 
    private String description; 
    @Size(max = 45) 
    @Column(name = "Furnish") 
    private String furnish; 
    @Size(max = 45) 
    @Column(name = "Garden") 
    private String garden; 
    @Column(name = "Area") 
    private Long area; 
    @Size(max = 45) 
    @Column(name = "BuyType") 
    private String buyType; 
    @Column(name = "PropertyPrice") 
    private Integer propertyPrice; 
    @Lob 
    @Column(name = "ImageUrl") 
    private byte[] imageUrl; 
    @OneToMany(mappedBy = "propertyId") 
    private Collection<Offer> offerCollection; 
    @JoinColumn(name = "agentsId", referencedColumnName = "AgentsId") 
    @ManyToOne 
    private Agents agentsId; 
    @JoinColumn(name = "OwnerId", referencedColumnName = "OwnerId") 
    @ManyToOne 
    private Owner ownerId; 

    public Property() { 
    } 

    public Property(Integer propertyId) { 
     this.propertyId = propertyId; 
    } 

    public Integer getPropertyId() { 
     return propertyId; 
    } 

    public void setPropertyId(Integer propertyId) { 
     this.propertyId = propertyId; 
    } 

    public String getPropertyType() { 
     return propertyType; 
    } 

    public void setPropertyType(String propertyType) { 
     this.propertyType = propertyType; 
    } 

    public Long getNumOfBedroom() { 
     return numOfBedroom; 
    } 

    public void setNumOfBedroom(Long numOfBedroom) { 
     this.numOfBedroom = numOfBedroom; 
    } 

    public Long getNumOfBathroom() { 
     return numOfBathroom; 
    } 

    public void setNumOfBathroom(Long numOfBathroom) { 
     this.numOfBathroom = numOfBathroom; 
    } 

    public String getAddress() { 
     return address; 
    } 

    public void setAddress(String address) { 
     this.address = address; 
    } 

    public String getDescription() { 
     return description; 
    } 

    public void setDescription(String description) { 
     this.description = description; 
    } 

    public String getFurnish() { 
     return furnish; 
    } 

    public void setFurnish(String furnish) { 
     this.furnish = furnish; 
    } 

    public String getGarden() { 
     return garden; 
    } 

    public void setGarden(String garden) { 
     this.garden = garden; 
    } 

    public Long getArea() { 
     return area; 
    } 

    public void setArea(Long area) { 
     this.area = area; 
    } 

    public String getBuyType() { 
     return buyType; 
    } 

    public void setBuyType(String buyType) { 
     this.buyType = buyType; 
    } 

    public Integer getPropertyPrice() { 
     return propertyPrice; 
    } 

    public void setPropertyPrice(Integer propertyPrice) { 
     this.propertyPrice = propertyPrice; 
    } 

    public byte[] getImageUrl() { 
     return imageUrl; 
    } 

    public void setImageUrl(byte[] imageUrl) { 
     this.imageUrl = imageUrl; 
    } 

    @XmlTransient 
    public Collection<Offer> getOfferCollection() { 
     return offerCollection; 
    } 

    public void setOfferCollection(Collection<Offer> offerCollection) { 
     this.offerCollection = offerCollection; 
    } 

    public Agents getAgentsId() { 
     return agentsId; 
    } 

    public void setAgentsId(Agents agentsId) { 
     this.agentsId = agentsId; 
    } 

    public Owner getOwnerId() { 
     return ownerId; 
    } 

    public void setOwnerId(Owner ownerId) { 
     this.ownerId = ownerId; 
    } 

    @Override 
    public int hashCode() { 
     int hash = 0; 
     hash += (propertyId != null ? propertyId.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 Property)) { 
      return false; 
     } 
     Property other = (Property) object; 
     if ((this.propertyId == null && other.propertyId != null) || (this.propertyId != null && !this.propertyId.equals(other.propertyId))) { 
      return false; 
     } 
     return true; 
    } 

    @Override 
    public String toString() { 
     return "Entities.Property[ propertyId=" + propertyId + " ]"; 
    } 

} 
のタイプである静的コンテキストから参照することができないという問題

セルヴェットページ

@Override 
    public void init() throws ServletException { 
     List<Property> PropertyList= PropertyFacade.findAll(); 
     getServletContext().setAttribute("property", PropertyList);  

    } 

PropertyFacadeクラス

public class PropertyFacade extends AbstractFacade<Property> { 

    @PersistenceContext(unitName = "testRealPU") 
    private EntityManager em; 

    @Override 
    protected EntityManager getEntityManager() { 
     return em; 
    } 

    public PropertyFacade() { 
     super(Property.class); 
    } 
+0

「静的文脈は、」あなたはどこオブジェクトまだ「内部」ではないことを意味@Stateless注釈を追加あなたが命を呼ぶ方法。 –

答えて

0

のfindAllは静的ではありませんので、あなたはあなたのPropertyFaçadeのインスタンスを作成する必要があります。 PropertyFacade.findAll()を呼び出すことはできません。春になると、このインスタンスがApplicationContextから取得され、ejbでどのように作成されているのか分かりません。

注:Javaでの命名規則を気:変数は小文字で始まり、クラスは大文字で始まり。したがって、PropertyListをpropertyListに変更する必要があります。サーブレットで

+0

ありがとうございました – sacrificateur

0

、次の操作を行います

@EJB 
PropertyFacade propertyFacade; 

@Override 
public void init() throws ServletException { 
    List<Property> PropertyList= propertyFacade.findAll(); 
    getServletContext().setAttribute("property", PropertyList);  

} 

そして、あなたのPropertyFacade.classに

@Stateless 
public class PropertyFacade extends AbstractFacade<Property> { 

    @PersistenceContext(unitName = "testRealPU") 
    private EntityManager em; 

    @Override 
    protected EntityManager getEntityManager() { 
     return em; 
    } 

    public PropertyFacade() { 
     super(Property.class); 
    } 
} 
+0

ありがとうございます – sacrificateur

関連する問題