Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/jpa/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
持久化对象jpa时的java.lang.StackOverflower错误_Java_Jpa_Persistence_Ejb 3.0 - Fatal编程技术网

持久化对象jpa时的java.lang.StackOverflower错误

持久化对象jpa时的java.lang.StackOverflower错误,java,jpa,persistence,ejb-3.0,Java,Jpa,Persistence,Ejb 3.0,我正在使用JPA、JSF、EJB和Derby构建一个应用程序。此时,应用程序仍然很小。我在申请表中有一个添加新产品的表格。当向数据库添加数据时,在我重新启动应用程序或服务器之前,一切都会顺利进行。当我重新启动服务器或应用程序时,我得到了java.lang.StackOverflowerError,我仍然可以查询数据库中由产品数据库表示的数据,但无法创建产品。到目前为止,我在db中只有5个条目,但我担心这会发生得太早 这就是Ejb(为了简单起见删除了Getter、setter和构造函数): @无状

我正在使用JPA、JSF、EJB和Derby构建一个应用程序。此时,应用程序仍然很小。我在申请表中有一个添加新产品的表格。当向数据库添加数据时,在我重新启动应用程序或服务器之前,一切都会顺利进行。当我重新启动服务器或应用程序时,我得到了java.lang.StackOverflowerError,我仍然可以查询数据库中由产品数据库表示的数据,但无法创建产品。到目前为止,我在db中只有5个条目,但我担心这会发生得太早

这就是Ejb(为了简单起见删除了Getter、setter和构造函数):

@无状态
公共类ProductEJB{
@PersistenceContext(unitName=“luavippu”)
私人实体管理者;
公共列表findAllProducts()
{
TypedQuery query=em.createNamedQuery(“findAllProducts”,Product.class);
返回query.getResultList();
}
公共产品findProductById(int productId)
{
返回em.find(Product.class,productId);
}
公共产品createProduct(产品产品)
{
product.setDateAdded(productCreationDate());
em.persist(产品);
退货产品;
}    
公共无效更新产品(产品产品)
{
em.merge(产品);
}
公共产品(产品)
{
product=em.find(product.class,product.getProduct_id());
em.remove(em.merge(product));
}
这是ProductController(为了简单起见删除了Getter、setter和构造函数):

@Named
@请求范围
公共类产品控制器{
@EJB
私有产品ejb ProductEJB;
@EJB
私人类别jb categoryjb;
私人产品=新产品();
private List productList=new ArrayList();
私人类别;
private List categoryList=new ArrayList();
公共字符串doCreateProduct()
{
product=productEjb.createProduct(产品);
productList=productEjb.findAllProducts();
返回“listProduct?faces redirect=true”;
}
公共字符串dodeletProduct()
{
productEjb.deleteProduct(产品);
返回“deleteProduct?faces redirect=true”;
}
公共字符串cancelDeleteAction()
{
返回“listProduct?faces redirect=true”;
}
@施工后
公共void init()
{
categoryList=categoryEjb.findAllCategory();
productList=productEjb.findAllProducts();
}
类别实体(为了简单起见,删除了getter、setter、hash()和构造函数):

@实体
@命名查询({
@NamedQuery(name=“findAllCategory”,query=“从类别c中选择c”)
})
公共类类别实现可序列化
{
私有静态最终长serialVersionUID=1L;
@Id@GeneratedValue(策略=GenerationType.AUTO)
私有int类_id;
私有字符串名称;
私有字符串描述;
@OneToMany(mappedBy=“category_fk”)
私有列表产品;
//readObject()和writeObject()
私有void readObject(ObjectInputStream ois)抛出IOException、ClassNotFoundException
{
//默认反序列化程序
ois.defaultReadObject();
//阅读属性
category_id=ois.readInt();
name=(字符串)ois.readObject();
description=(字符串)ois.readObject();
}
私有void writeObject(ObjectOutputStream oos)抛出IOException、ClassNotFoundException
{
//默认序列化程序
oos.defaultWriteObject();
//写属性
oos.writeInt(类别标识);
oos.writeObject(名称);
oos.writeObject(描述);
}
@凌驾
公共布尔等于(对象obj){
if(obj==null){
返回false;
}
如果(getClass()!=obj.getClass()){
返回false;
}
最终类别其他=(类别)obj;
if(this.category\u id!=其他.category\u id){
返回false;
}
如果((this.name==null)?(other.name!=null):!this.name.equals(other.name)){
返回false;
}
如果((this.description==null)?(other.description!=null):!this.description.equals(other.description)){
返回false;
}
如果(this.product_fk!=other.product_fk&&(this.product_fk==null|||!this.product_fk.equals(other.product_fk))){
返回false;
}
返回true;
}
产品实体(为了简单起见,删除了getter、setter、hash()和构造函数):

@实体
@命名查询({
@NamedQuery(name=“findAllProducts”,query=“从产品p中选择p”)
})
公共类产品实现可序列化
{
私有静态最终长serialVersionUID=1L;
@Id@GeneratedValue(策略=GenerationType.AUTO)
私有int产品标识;
私有字符串名称;
私有字符串描述;
受保护的字节[]映像文件;
私人浮动价格;
@时态(TemporalType.TIMESTAMP)
已添加私人日期;
@许多酮
私人类别(fk),;
@许多酮
私人销售详情;
//readObject()和writeObject()方法
私有void readObject(ObjectInputStream ois)抛出IOException、ClassNotFoundException
{
//默认反序列化
ois.defaultReadObject();
//阅读属性
product_id=ois.readInt();
name=(字符串)ois.readObject();
description=(字符串)ois.readObject();

对于(int i=0;i问题似乎出在Category类中-equals做了一些事情,反过来调用equals,创建了一个无休止的循环

我怀疑循环依赖性是问题的根本原因。我认为您已经在
Category
SaleDetails
或两者中映射了
产品
s、 如果是,它将呼叫cir
@Stateless
public class ProductEJB{

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

    public List<Product> findAllProducts()
    {
        TypedQuery<Product> query = em.createNamedQuery("findAllProducts", Product.class);
        return query.getResultList();
    }

    public Product findProductById(int productId)
    {
        return em.find(Product.class, productId);
    }

    public Product createProduct(Product product)
    {
        product.setDateAdded(productCreationDate());
        em.persist(product);
        return product;        
    }    

    public void updateProduct(Product product)
    {
        em.merge(product);
    }

    public void deleteProduct(Product product)
    {
        product = em.find(Product.class, product.getProduct_id());
        em.remove(em.merge(product));
    }
    @Named
@RequestScoped
public class ProductController {

    @EJB
    private ProductEJB productEjb;
    @EJB
    private CategoryEJB categoryEjb;

    private Product product = new Product();
    private List<Product> productList = new ArrayList<Product>();

    private Category category;
    private List<Category> categoryList = new ArrayList<Category>();

    public String doCreateProduct()
    {
        product = productEjb.createProduct(product);
        productList = productEjb.findAllProducts();
        return "listProduct?faces-redirect=true";
    }

    public String doDeleteProduct()
    {
        productEjb.deleteProduct(product);
        return "deleteProduct?faces-redirect=true";
    }

    public String cancelDeleteAction()
    {
        return "listProduct?faces-redirect=true";
    }


    @PostConstruct
    public void init()
    {
        categoryList = categoryEjb.findAllCategory();
        productList = productEjb.findAllProducts();        
    }
@Entity
@NamedQueries({
    @NamedQuery(name= "findAllCategory", query="SELECT c FROM Category c")        
})
public class Category implements Serializable
{
    private static final long serialVersionUID = 1L;

    @Id @GeneratedValue(strategy= GenerationType.AUTO)
    private int category_id;
    private String name;
    private String description;
    @OneToMany(mappedBy = "category_fk")
        private List<Product> product_fk;

 // readObject() and writeObject() 

    private void readObject(ObjectInputStream ois) throws IOException, ClassNotFoundException
    {
        // default deserializer
        ois.defaultReadObject();

        // read the attributes
        category_id = ois.readInt();
        name = (String)ois.readObject();
        description = (String)ois.readObject();

    }

    private void writeObject(ObjectOutputStream oos) throws IOException, ClassNotFoundException
    {
        // default serializer
        oos.defaultWriteObject();

        // write the attributes
        oos.writeInt(category_id);
        oos.writeObject(name);
        oos.writeObject(description);


       }

 @Override
    public boolean equals(Object obj) {
        if (obj == null) {
            return false;
        }
        if (getClass() != obj.getClass()) {
            return false;
        }
        final Category other = (Category) obj;
        if (this.category_id != other.category_id) {
            return false;
        }
        if ((this.name == null) ? (other.name != null) : !this.name.equals(other.name)) {
            return false;
        }
        if ((this.description == null) ? (other.description != null) : !this.description.equals(other.description)) {
            return false;
        }
        if (this.product_fk != other.product_fk && (this.product_fk == null || !this.product_fk.equals(other.product_fk))) {
            return false;
        }
        return true;
    }
@Entity
@NamedQueries({
    @NamedQuery(name="findAllProducts", query = "SELECT p from Product p")

})
public class Product implements Serializable
{
    private static final long serialVersionUID = 1L;

    @Id @GeneratedValue(strategy= GenerationType.AUTO)
    private int product_id;
    private String name;
    private String description;
    protected byte[] imageFile;
    private Float price;
    @Temporal(TemporalType.TIMESTAMP)
    private Date dateAdded;        
    @ManyToOne
    private Category category_fk;
    @ManyToOne
    private SaleDetails saleDetails_fk;

    // readObject() and writeObject() methods

    private void readObject (ObjectInputStream ois)throws IOException, ClassNotFoundException
    {
        // default deserialization
        ois.defaultReadObject();

        // read the attributes
        product_id = ois.readInt();
        name = (String)ois.readObject();
        description = (String)ois.readObject();

        for(int i=0; i<imageFile.length; i++ )
        {
            imageFile[i]=ois.readByte();
        }

        price = ois.readFloat();
        dateAdded = (Date)ois.readObject();
        category_fk = (Category)ois.readObject();
        saleDetails_fk = (SaleDetails)ois.readObject();

    }

@Override
public boolean equals(Object obj) {
    if (obj == null) {
        return false;
    }
    if (getClass() != obj.getClass()) {
        return false;
    }
    final Product other = (Product) obj;
    if (this.product_id != other.product_id) {
        return false;
    }
    if ((this.name == null) ? (other.name != null) : !this.name.equals(other.name)) {
        return false;
    }
    if ((this.description == null) ? (other.description != null) : !this.description.equals(other.description)) {
        return false;
    }
    if (!Arrays.equals(this.imageFile, other.imageFile)) {
        return false;
    }
    if (this.price != other.price && (this.price == null || !this.price.equals(other.price))) {
        return false;
    }
    if (this.dateAdded != other.dateAdded && (this.dateAdded == null || !this.dateAdded.equals(other.dateAdded))) {
        return false;
    }
    if (this.category_fk != other.category_fk && (this.category_fk == null || !this.category_fk.equals(other.category_fk))) {
        return false;
    }
    if (this.saleDetails_fk != other.saleDetails_fk && (this.saleDetails_fk == null || !this.saleDetails_fk.equals(other.saleDetails_fk))) {
        return false;
    }
    return true;
}

    private void writeObject(ObjectOutputStream oos) throws IOException, ClassNotFoundException
    {
        // default serialization
        oos.defaultWriteObject();

        // write object attributes
        oos.writeInt(product_id);
        oos.writeObject(name);
        oos.writeObject(description);
        oos.write(imageFile);
        oos.writeFloat(price);
        oos.writeObject(dateAdded);
        oos.writeObject(category_fk);
        oos.writeObject(saleDetails_fk);

    }
    javax.faces.el.EvaluationException: java.lang.StackOverflowError
    at javax.faces.component.MethodBindingMethodExpressionAdapter.invoke(MethodBindingMethodExpressionAdapter.java:102)
    at com.sun.faces.application.ActionListenerImpl.processAction(ActionListenerImpl.java:102)
    at javax.faces.component.UICommand.broadcast(UICommand.java:315)
    at javax.faces.component.UIViewRoot.broadcastEvents(UIViewRoot.java:794)
    at javax.faces.component.UIViewRoot.processApplication(UIViewRoot.java:1259)
    at com.sun.faces.lifecycle.InvokeApplicationPhase.execute(InvokeApplicationPhase.java:81)
    at com.sun.faces.lifecycle.Phase.doPhase(Phase.java:101)
    at com.sun.faces.lifecycle.LifecycleImpl.execute(LifecycleImpl.java:118)
    at javax.faces.webapp.FacesServlet.service(FacesServlet.java:593)
    at org.apache.catalina.core.StandardWrapper.service(StandardWrapper.java:1550)
    at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:281)
    at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:175)
    at org.apache.catalina.core.StandardPipeline.doInvoke(StandardPipeline.java:655)
    at org.apache.catalina.core.StandardPipeline.invoke(StandardPipeline.java:595)
    at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:161)
    at org.apache.catalina.connector.CoyoteAdapter.doService(CoyoteAdapter.java:331)
    at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:231)
    at com.sun.enterprise.v3.services.impl.ContainerMapper$AdapterCallable.call(ContainerMapper.java:317)
    at com.sun.enterprise.v3.services.impl.ContainerMapper.service(ContainerMapper.java:195)
    at com.sun.grizzly.http.ProcessorTask.invokeAdapter(ProcessorTask.java:860)
    at com.sun.grizzly.http.ProcessorTask.doProcess(ProcessorTask.java:757)
    at com.sun.grizzly.http.ProcessorTask.process(ProcessorTask.java:1056)
    at com.sun.grizzly.http.DefaultProtocolFilter.execute(DefaultProtocolFilter.java:229)
    at com.sun.grizzly.DefaultProtocolChain.executeProtocolFilter(DefaultProtocolChain.java:137)
    at com.sun.grizzly.DefaultProtocolChain.execute(DefaultProtocolChain.java:104)
    at com.sun.grizzly.DefaultProtocolChain.execute(DefaultProtocolChain.java:90)
    at com.sun.grizzly.http.HttpProtocolChain.execute(HttpProtocolChain.java:79)
    at com.sun.grizzly.ProtocolChainContextTask.doCall(ProtocolChainContextTask.java:54)
    at com.sun.grizzly.SelectionKeyContextTask.call(SelectionKeyContextTask.java:59)
    at com.sun.grizzly.ContextTask.run(ContextTask.java:71)
    at com.sun.grizzly.util.AbstractThreadPool$Worker.doWork(AbstractThreadPool.java:532)
    at com.sun.grizzly.util.AbstractThreadPool$Worker.run(AbstractThreadPool.java:513)
    at java.lang.Thread.run(Thread.java:722)
Caused by: java.lang.StackOverflowError
    at java.util.Vector$Itr.<init>(Vector.java:1120)
    at java.util.Vector.iterator(Vector.java:1114)
    at java.util.AbstractList.hashCode(AbstractList.java:540)
    at java.util.Vector.hashCode(Vector.java:988)
    at org.eclipse.persistence.indirection.IndirectList.hashCode(IndirectList.java:460)
    at com.lv.Entity.Category.hashCode(Category.java:96)
    at com.lv.Entity.Product.hashCode(Product.java:148)
    at java.util.AbstractList.hashCode(AbstractList.java:541)
 private void writeObject(ObjectOutputStream oos) throws IOException {
    // default serialization 
    oos.defaultWriteObject();
    // write the object attributes
    oos.writeInt(product_id);
    oos.writeObject(name);
    oos.writeObject(description);
    oos.write(imageFile);
    oos.writeFloat(price);
    oos.writeObject(dateAdded);
    oos.writeObject(category_fk);
    oos.writeObject(saleDetails_fk);
  }

   private void readObject(ObjectInputStream ois) 
                                    throws ClassNotFoundException, IOException {
      // default deserialization
      ois.defaultReadObject();
      //read the attributes
      product_id = ois.readInt();
      name = (String)ois.readObject();
      description = (String)ois.readObject();
      imageFile = ois.read();
      price = ois.readFloat();
      dateAdded = (Date)ois.readObject();
      category_fk = (Category)ois.readObject();
      saleDetails_fk = (SaleDetails)ois.readObject();
    } 
if (this.product_fk != other.product_fk && (this.product_fk == null || !this.product_fk.equals(other.product_fk))) {
    return false;
}
if (this.category_fk != other.category_fk && (this.category_fk == null || !this.category_fk.equals(other.category_fk))) {
    return false;
}
    public class Category implements Serializable
{
   ...
 @Override
    public boolean equals(Object obj) {
        if (obj == null) {
            return false;
        }
        if (getClass() != obj.getClass()) {
            return false;
        }

        if ((this.name == null) ? (other.name != null) : !this.name.equals(other.name)) {
            return false;
        }
        if ((this.description == null) ? (other.description != null) : !this.description.equals(other.description)) {
            return false;
        }
        return true;
    }
}
        session.startTransaction();
    Category electronics = session.get(Category.class, 1234);
    Set<Product> products = electronics.getProducts();
    session.commit();

    Product camera = product.get(0);
    camera.setPrice("300");
    products.add(camera);