如何从CollectionTable中删除数据休眠(Jpa) [英] How delete data from CollectionTable | Hibernate (Jpa)
问题描述
我有一个带有List<String> MyList
的"A"实体:
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;
}
在将值"设置为此"A"实体后,我想从derby表中删除所有这些数据.
但是,如何从CollectionTable(MyList_Table)中删除字符串?
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)?
我尝试通过HQL查询使其成功,但出现错误.
I try to make it by HQL query, but i got error.
List<String> strList = em.createQuery("from MyList_Table").getResultList();
...
...
错误:
MyList_Table is not mapped [from MyList_Table]
我应该如何正确地制定查询?
可能还有另一种删除此CollectionTable数据的方法吗?
How should I formulate the query correctly?
There may be another way to delete this CollectionTable data?
--------------------------------
更新:
当我使用时:
List<A> objectList = em.createQuery("from A_table").getResultList();
->我收到错误消息:
A_Table is not mapped [from A_Table]
.
when i used :
List<A> objectList = em.createQuery("from A_table").getResultList();
-> i got error :
A_Table is not mapped [from A_Table]
.
所以我决定保留原样:
List<A> objectList = query.getResultList()
.
但是,如果我尝试将其删除:
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();
}
}
我收到错误消息:
org.hibernate.MappingException: Unknown entity: org.hibernate.collection.PersistentBag
所以我尝试添加"@IndexColumn
注释:
.....
@ElementCollection
@CollectionTable(name = "A_Table")
@IndexColumn(name= "indx")
private List<String> MyList;
...
现在我收到此错误:
org.hibernate.MappingException: Unknown entity: org.hibernate.collection.PersistentList
我应该怎么做?
非常感谢您!
推荐答案
首先,您必须从父表中恢复元素,即A:
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();
...
如果它对您不起作用,则可以使用CriteriaQuery:
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();
我注意到您在@Entity批注中具有名称属性,您的映射表未命名为"A",而是"A_table":
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();
要删除元素,请尝试将其一一删除,而不要删除整个列表:
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();
---------------------------
解决方案在这里:
如何从组织中删除数据.hibernate.collection.PersistentBag? |休眠(Jpa)
---------------------------
The solution is here:
How to delete data from org.hibernate.collection.PersistentBag? | Hibernate (Jpa)
这篇关于如何从CollectionTable中删除数据休眠(Jpa)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!