使用EJB和JPA将记录插入数据库

时间:2015-09-26 11:42:08

标签: java mysql jpa ejb

我正在尝试使用EJB,JPA和JSF在NetBeans中使用简单的CRUD企业应用程序在产品表中插入记录

https://www.youtube.com/watch?v=_yDSptmtC38 我是EJB的新手我通过以上链接学习。我遵循所有步骤。 我正在使用MySQL Netbeans IDE EJB JPA和JSF。

AbstractFacade.java     private Class 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();
}

}

ProductController.java
@EJB
private ProductFacade productFacade;

private Product p= new Product();

public Product getP() {
    return p;
}

public void setP(Product p) {
    this.p = p;
}

public ProductController() {
}
public List<Product> findAll()
{
    return this.productFacade.findAll();
}
public String add()
{
    this.productFacade.create(this.p);
    this.p = new Product();
    return "index";
}

}

Product.java

public Product() {
}

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

public Product(Integer id, String name, int price, int quantity) {
    this.id = id;
    this.name = name;
    this.price = price;
    this.quantity = quantity;
}

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 int getPrice() {
    return price;
}

public void setPrice(int price) {
    this.price = price;
}

public int getQuantity() {
    return quantity;
}

public void setQuantity(int quantity) {
    this.quantity = quantity;
}

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

@Override
public boolean equals(Object object) {

    if (!(object instanceof Product)) {
        return false;
    }
    Product other = (Product) object;
    if ((this.id == null && other.id != null) || (this.id != null && !this.id.equals(other.id))) {
        return false;
    }
    return true;
}

@Override
public String toString() {
    return "entities.Product[ id=" + id + " ]";
}

}

add.xml

<?xml version='1.0' encoding='UTF-8' ?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"  "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
 <html xmlns="http://www.w3.org/1999/xhtml"
  xmlns:h="http://xmlns.jcp.org/jsf/html"
  xmlns:f="http://xmlns.jcp.org/jsf/core">
<h:head>
    <title>Facelet Title</title>
</h:head>
<h:body>
    <f:view>


        <h:form>
            <h1><h:outputText value="Create/Edit"/></h1>
            <h:panelGrid columns="2">

                <h:outputLabel value="Name:" for="name" />
                <h:inputText id="name" value="#{productController.p.name}" title="Name" 
                             required="true" requiredMessage="The Name field is required."/>
                <h:outputLabel value="Price:" for="price" />
                <h:inputText id="price" value="#{productController.p.price}" title="Price" 
                             required="true" requiredMessage="The Price  field is required."/>
                <h:outputLabel value="Quantity:" for="quantity" />
                <h:inputText id="quantity" value="#{productController.p.quantity}" title="Quantity" 
                             required="true" requiredMessage="The Quantity field is required."/>
                <h:commandButton value="Save" action="#{productController.add()}">
                </h:commandButton>
            </h:panelGrid>
        </h:form>
    </f:view>

</h:body>

错误堆栈跟踪 在模型。 EJB31_Generated__ProductFacade__Intf ____ Bean .create(未知来源)     在controller.ProductController.add(ProductController.java:47)

在model.AbstractFacade.create(AbstractFacade.java:25)

我该如何解决?

0 个答案:

没有答案