Search code examples
javahibernatejpauielementcollection

How delete data from CollectionTable | Hibernate (Jpa)


i have "A" entity with List<String> MyList:

@Entity(name = "A_table")
@Inheritance(strategy=InheritanceType.JOINED)
public class A implements Serializable {
    private static final long serialVersionUID = 1L;

    @Id
    @GeneratedValue(strategy = GenerationType.AUTO)
    private Long id;

    private String RepresentativeName;

    @ElementCollection
    @CollectionTable(name = "MyList_Table")
    private List<String> MyList;
}

After i set Values to this "A" entity, i want to delete all this data from the derby table.
But how i can delete the Strings from the CollectionTable (MyList_Table)?

I try to make it by HQL query, but i got error.

List<String> strList = em.createQuery("from MyList_Table").getResultList();
...
...   

the Error:

MyList_Table is not mapped [from MyList_Table]

How should I formulate the query correctly?
There may be another way to delete this CollectionTable data?

--------------------------------
Update:

when i used :
List<A> objectList = em.createQuery("from A_table").getResultList();
-> i got error :
A_Table is not mapped [from A_Table].

So I decided to leave this line as it was:
List<A> objectList = query.getResultList().
But then, if i try to delete it:

for (A a:objectList)
{
   if(....)
   {
     List<String> Mylist = a.getMyList();
      em.getTransaction().begin();
      em.remove(Mylist);
      em.getTransaction().commit();
   }
}

i get an error :

org.hibernate.MappingException: Unknown entity: org.hibernate.collection.PersistentBag

So i try to add "@IndexColumn" annotation :

    .....
    @ElementCollection
    @CollectionTable(name = "A_Table")
    @IndexColumn(name= "indx")
    private List<String> MyList;
    ...

and now i get this error:

org.hibernate.MappingException: Unknown entity: org.hibernate.collection.PersistentList

what i should do?

Thank you very much!!


Solution

  • First of all, you must recover elements from your parent table, i.e. A:

    List<A> objectList = em.createQuery("from A").getResultList(); 
    // It would be neccesary to cast the result list
    

    Once you have your result list, you can access now to your secondary table and do with these data whatever you want:

    objectList.getMyList().clear();
    ...
    

    If it doesn't work to you, you can use CriteriaQuery:

    CriteriaBuilder criteriaBuilder = em.getCriteriaBuilder();
    CriteriaQuery<A> criteriaQuery = criteriaBuilder.createQuery(A.class);
    criteriaQuery.from(A.class);
    Query query = em.createQuery(criteriaQuery);
    List<A> objectList = query.getResultList();
    

    EDIT: I have notice that you have a name attribute in your @Entity annotation, your mapping table is not named "A", but "A_table":

    List<A> objectList = em.createQuery("from A_table").getResultList();
    

    EDIT2: To delete elements, try deleting it one by one instead of deleting the entire list:

    List<String> Mylist = a.getMyList();
    em.getTransaction().begin();
    for (String element:Mylist){
        em.remove(element);
    }
    em.getTransaction().commit();
    

    ---------------------------
    The solution is here:

    How to delete data from org.hibernate.collection.PersistentBag? | Hibernate (Jpa)