0
刪除從映射表中的數據我有2個實體中有一個是頭,另一種是項目無法休眠
頭有很多項目
頁眉代碼如下
@Entity
public class Headertable {
@Id
@GeneratedValue
private int headerId;
private String headerText;
@OneToMany(mappedBy="itemId.header",cascade=CascadeType.ALL,fetch=FetchType.EAGER)
private List<Items> items = new ArrayList<Items>();
...
getter and setters for these
和項目表如下
@Entity
public class Items {
@EmbeddedId
private ItemId itemId;
private String itemData;
.... getter and setters for these
商品表id類
@Embeddable
public class ItemId implements Serializable{
@ManyToOne
private Headertable header;
private int itemId;
...setter and getters
下面是測試代碼從郵件頭中刪除一個項如下
Session session = this.sessionFactory.openSession();
session.beginTransaction();
Headertable header = (Headertable) session.get(Headertable.class, 1);
Iterator<Items> iterator = header.getItems().iterator();
while(iterator.hasNext())
{
Items items = iterator.next();
if(items.getItemData().equals("chiru"))
{
iterator.remove();
}
}
session.saveOrUpdate(header);
Transaction transaction = session.getTransaction();
transaction.commit();
org.hibernate.MappingException:未知實體:org.hibernate.collection.internal.PersistentBag它引發此異常 – chiranjeevigk
不是集合,而是'Items'類型的變量'items'。 –