1

I know removing from relational tables has always been an issue, So I searched along the internet to find a good answer but I tried most of them and they didnt work for me so I wish my question wont get duplicated.

So, I have two entities with OneToMany relation. each product_family can have many products so If Im not wrong, my product_family is like the parent;

ProductFamilyEntity

@Entity
@Table(name = "product_family")
public class ProductFamilyEntity {

    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    private long id;

    @Column
    private String name;

    @OneToMany(fetch = FetchType.LAZY, mappedBy = "productFamilyEntity", cascade = CascadeType.ALL, orphanRemoval = true)
    private List<ProductEntity> productEntityList = new ArrayList<ProductEntity>();

    public long getId() {
        return id;
    }

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

    public String getName() {
        return name;
    }

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

    public List<ProductEntity> getProductEntityList() {
        return productEntityList;
    }

    public void setProductEntityList(List<ProductEntity> productEntityList) {
        this.productEntityList = productEntityList;
    }
}

Most Answers on internet were suggesting to use CascadeType.all and orphanRemoval = true for parent entity class and I did the same.

And here is ProductEntity class

@Entity
@Table(name = "product")
public class ProductEntity {

  @Id
  @GeneratedValue(strategy = GenerationType.IDENTITY)
  private long id;

  @Column
  private String name;

  @Column
  private String code;

  @ManyToOne(fetch = FetchType.LAZY)
  @JoinColumn(name = "product_family_id" , nullable = false)
  ProductFamilyEntity productFamilyEntity;

  @OneToMany(fetch = FetchType.LAZY , mappedBy = "productEntity",)
//  cascade = {CascadeType.REMOVE, CascadeType.PERSIST})
//  @Cascade({org.hibernate.annotations.CascadeType.SAVE_UPDATE})
    List<ReleaseEntity> releaseEntitySet = new ArrayList<ReleaseEntity>();

  public long getId() {
    return id;
  }

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

  public String getName() {
    return name;
  }

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

  public String getCode() {
    return code;
  }

  public void setCode(String code) {
    this.code = code;
  }

  public ProductFamilyEntity getProductFamilyEntity() {
    return productFamilyEntity;
  }

  public void setProductFamilyEntity(ProductFamilyEntity productFamilyEntity) {
    this.productFamilyEntity = productFamilyEntity;
  }

  public List<ReleaseEntity> getReleaseEntitySet() {
    return releaseEntitySet;
  }

  public void setReleaseEntitySet(List<ReleaseEntity> releaseEntitySet) {
    this.releaseEntitySet = releaseEntitySet;
  }

}

When I insert product_family , I can easily delete it too unless its related to some products. here is the error I get then :

nested exception is javax.persistence.PersistenceException: org.hibernate.exception.GenericJDBCException: could not extract ResultSet

And this is last root caused :

org.postgresql.util.PSQLException: ERROR: current transaction is aborted, commands ignored until end of transaction block
    org.postgresql.core.v3.QueryExecutorImpl.receiveErrorResponse(QueryExecutorImpl.java:2102)
    org.postgresql.core.v3.QueryExecutorImpl.processResults(QueryExecutorImpl.java:1835)
    org.postgresql.core.v3.QueryExecutorImpl.execute(QueryExecutorImpl.java:257)
    org.postgresql.jdbc2.AbstractJdbc2Statement.execute(AbstractJdbc2Statement.java:500)
    org.postgresql.jdbc2.AbstractJdbc2Statement.executeWithFlags(AbstractJdbc2Statement.java:388)
    org.postgresql.jdbc2.AbstractJdbc2Statement.executeQuery(AbstractJdbc2Statement.java:273)
    org.hibernate.engine.jdbc.internal.ResultSetReturnImpl.extract(ResultSetReturnImpl.java:79)
    org.hibernate.loader.Loader.getResultSet(Loader.java:2062)
    org.hibernate.loader.Loader.executeQueryStatement(Loader.java:1859)
    org.hibernate.loader.Loader.executeQueryStatement(Loader.java:1838)
    org.hibernate.loader.Loader.doQuery(Loader.java:906)
    org.hibernate.loader.Loader.doQueryAndInitializeNonLazyCollections(Loader.java:348)
    org.hibernate.loader.Loader.doList(Loader.java:2550)
    org.hibernate.loader.Loader.doList(Loader.java:2536)
    org.hibernate.loader.Loader.listIgnoreQueryCache(Loader.java:2366)
    org.hibernate.loader.Loader.list(Loader.java:2361)
    org.hibernate.loader.hql.QueryLoader.list(QueryLoader.java:495)
    org.hibernate.hql.internal.ast.QueryTranslatorImpl.list(QueryTranslatorImpl.java:357)
    org.hibernate.engine.query.spi.HQLQueryPlan.performList(HQLQueryPlan.java:198)
    org.hibernate.internal.SessionImpl.list(SessionImpl.java:1194)
    org.hibernate.internal.QueryImpl.list(QueryImpl.java:101)
    org.hibernate.ejb.QueryImpl.getResultList(QueryImpl.java:268)
    ir.ntnaeem.automation.domain.abstractDao.PgsqlDao.selectAll(PgsqlDao.java:69)
    ir.ntnaeem.automation.controller.lm.ProductFamilyController.productFamilyList(ProductFamilyController.java:48)
    sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
    sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    java.lang.reflect.Method.invoke(Method.java:497)
    org.springframework.web.method.support.InvocableHandlerMethod.invoke(InvocableHandlerMethod.java:215)
    org.springframework.web.method.support.InvocableHandlerMethod.invokeForRequest(InvocableHandlerMethod.java:132)
    org.springframework.web.servlet.mvc.method.annotation.ServletInvocableHandlerMethod.invokeAndHandle(ServletInvocableHandlerMethod.java:104)
    org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.invokeHandleMethod(RequestMappingHandlerAdapter.java:781)
    org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.handleInternal(RequestMappingHandlerAdapter.java:721)
    org.springframework.web.servlet.mvc.method.AbstractHandlerMethodAdapter.handle(AbstractHandlerMethodAdapter.java:83)
    org.springframework.web.servlet.DispatcherServlet.doDispatch(DispatcherServlet.java:943)
    org.springframework.web.servlet.DispatcherServlet.doService(DispatcherServlet.java:877)
    org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:966)
    org.springframework.web.servlet.FrameworkServlet.doGet(FrameworkServlet.java:857)
    javax.servlet.http.HttpServlet.service(HttpServlet.java:624)
    org.springframework.web.servlet.FrameworkServlet.service(FrameworkServlet.java:842)
    javax.servlet.http.HttpServlet.service(HttpServlet.java:731)
    org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:52)
    org.springframework.web.filter.CharacterEncodingFilter.doFilterInternal(CharacterEncodingFilter.java:88)
    org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:107

can anyone help me out here?

My dependencies versions are as below :

        <dependency>
            <groupId>og.hibernate</groupId>
            <artifactId>hibernate-entitymanager</artifactId>
            <version>4.2.16.Final</version>
        </dependency>
        <dependency>
            <groupId>org.hibernate.javax.persistence</groupId>
            <artifactId>hibernate-jpa-2.0-api</artifactId>
            <version>1.0.0.Final</version>
        </dependency>

And I'm using postgresql V9.1.

Also here is my DAO remove method :

public void remove(long id) {
        EntityTransaction tx = em.getTransaction();
        tx.begin();
        String query = String.format("DELETE FROM %s table WHERE table.id= :id", entityClassName);
        Query jpaQuery = em.createQuery(query);
        jpaQuery.setParameter("id", id).executeUpdate();
        tx.commit();
    }

Thank You;

tharindu_DG
  • 8,900
  • 6
  • 52
  • 64
Sepehr GH
  • 1,297
  • 1
  • 17
  • 39

1 Answers1

0

I finally found my answer after 10 hours, I post it so it might help someone else.

The problem was my DAO remove method. I used to do below action to delete productFamilyEntity :

productFamilyDao.remove(id);

where id was id of my product family entity. the point is as you can see my remove method in my question, that might not be able to do remove all related products too. I'm not really good at database, I'm not sure thats the right thing it is doing or not, but the solution for me was :

ProductFamilyEntity productFamilyEntity = productFamilyDao.find(id);
productFamilyDao.remove(productFamilyEntity);

this way, you have all related products list in your productFamilyEntity, so now you can easily remove them. and here is my method for that :

public void remove(E entity) {

        EntityTransaction tx = em.getTransaction();
        tx.begin();
        em.remove(entity);
        tx.commit();
    }

Good Luck;

NOTE

Special thanks to @Neo for his answer here which was not the exact answer but a clue to the solution.

Community
  • 1
  • 1
Sepehr GH
  • 1,297
  • 1
  • 17
  • 39