Hi, I need to add default items in an association one to many. Try as follows but do not save items
I wrote a code with two entities: product and cateory. I need to add all categories when a new product is created. But the data is not saved in product. Any idea thanks
Here is de code.
Entity product
@Table(name = "LIQUIDACION_PRODUCT")
@Entity(name = "liquidacion$Product")
public class Product extends StandardEntity {
private static final long serialVersionUID = 104464269097718362L;
@Column(name = "DESCRIPTION", nullable = false)
protected String description;
@OneToMany(mappedBy = "product")
protected Set<Category> catgories;
public void setCatgories(Set<Category> catgories) {
this.catgories = catgories;
}
public Set<Category> getCatgories() {
return catgories;
}
public void setDescription(String description) {
this.description = description;
}
public String getDescription() {
return description;
}
}
Entity Cateory
@Table(name = "LIQUIDACION_CATEGORY")
@Entity(name = "liquidacion$Category")
public class Category extends StandardEntity {
private static final long serialVersionUID = 5830517938240017460L;
@Column(name = "DESCRIPTION", nullable = false)
protected String description;
@ManyToOne(fetch = FetchType.LAZY)
@JoinColumn(name = "PRODUCT_ID")
protected Product product;
public void setProduct(Product product) {
this.product = product;
}
public Product getProduct() {
return product;
}
public void setDescription(String description) {
this.description = description;
}
public String getDescription() {
return description;
}
}
ProductEdit
public class ProductEdit extends AbstractEditor<Product> {
@Inject
private DataSupplier dataSupplier;
@Override
protected void initNewItem(Product item) {
super.initNewItem(item);
addDefaultCategories(item);
}
@Inject
private CollectionDatasource<Category, UUID> categoriesDs;
private void addDefaultCategories(Product item) {
LoadContext loadContext = LoadContext.create(Category.class)
.setQuery(LoadContext.createQuery("select p from liquidacion$Category p")
)
.setView("category-with-product-view");
List<Category> categories = dataSupplier.loadList(loadContext);
Set<Category> cats = new HashSet<Category>();
cats.addAll(categories);
for (Category cat : categories) {
cat.setProduct(item);
}
item.setCatgories(cats);
}
}