How remove an element from a list?
bydcc Nov 19, 2007 6:30 PMHello,
I am trying to remove and element from a list but I don't get it.
I have a category and details for this category.
Relevant code for Category:
@Entity @Table(name = "category", catalog = "CartStore", uniqueConstraints = {}) public class Category implements java.io.Serializable { // Fields private long id; private String name; private String description; private String sortDescripcion; private Long creatorId; private Date creationDate; private Date modificationDate; private CategoryContext categoryContext = null; ... @Id @GeneratedValue @Column(name = "id", unique = true, nullable = false, insertable = true, updatable = false) public long getId() { return this.id; } public void setId(long id) { this.id = id; } .... @OneToMany(cascade = { CascadeType.ALL }, fetch = FetchType.LAZY, mappedBy = "category") public Set<CategoryDetails> getCategoryDetailses() { return this.categoryDetailses; } public void setCategoryDetailses(Set<CategoryDetails> categoryDetailses) { this.categoryDetailses = categoryDetailses; }
Relevant code for CategoryDetail:
@Entity @Table(name = "category_details", catalog = "CartStore", uniqueConstraints = {}) public class CategoryDetails implements java.io.Serializable { // Fields private long id; private Category category; private String name; private String value; private String type; .... @Id @GeneratedValue @Column(name = "id", unique = true, nullable = false, insertable = true, updatable = false) public long getId() { return this.id; } public void setId(long id) { this.id = id; } ..... @ManyToOne(cascade = {}, fetch = FetchType.LAZY) @JoinColumn(name = "category_id", unique = false, nullable = false, insertable = true, updatable = false) public Category getCategory() { return this.category; } public void setCategory(Category category) { this.category = category; }
The code I run:
tm.begin(); CategoryDetails categoryDetail = em.find(CategoryDetails.class, idCategoryDetail); categoryDetail.setName("To remove"); Category category = categoryDetail.getCategory(); Set categoryDetails = category.getCategoryDetailses(); for(Iterator it = categoryDetails.iterator(); it.hasNext();) { CategoryDetails cdx = (CategoryDetails)it.next(); cdx.getId(); // Lazy } categoryDetails.remove(categoryDetail); categoryDetail.setCategory(null); category.setCategoryDetailses(categoryDetails); em.merge(category); tm.commit();
The element is not removed and sentece sql that I see is:
2007-11-20 00:02:54,375 [main] DEBUG org.hibernate.SQL - update CartStore.category_details set name=?, value=?, type=? where id=? Hibernate: update CartStore.category_details set name=?, value=?, type=? where id=?
What i am doing bad?
Thank you.