This Bugzilla instance is a read-only archive of historic NetBeans bug reports. To report a bug in NetBeans please follow the project's instructions for reporting issues.
A common example of this would be nested categories. To reproduce: 1. Create the following table (mysql) -- -------------------------------------- create table category ( id int not null, cat_name varchar(24) not null, parent_id int ) type = InnoDB; alter table category add constraint category_pk primary key (id); create index cat_parent_idx on category (parent_id); alter table category add constraint cat_parent_fk foreign key (parent_id) references category(id) on delete restrict on update restrict; -- -------------------------------------- 2. Start a new (basic) web project, create a data source to point to the database 3. New > Entity Classes from database..., select the data source, add the category table, accept all defaults. 4. Compile (necessary?) 5. New > Restful Web Services from Entity classes, add the Category entity, accept all defaults 6. Compile The compile produces the following errors. /home/bill/NetBeansProjects/WebApplication1/src/java/com/example/service/CategoriesResource.java:122: incompatible types found : java.util.Collection<com.example.Category> required: com.example.Category Category oldEntity = value.getCategoryCollection(); /home/bill/NetBeansProjects/WebApplication1/src/java/com/example/service/CategoriesResource.java:123: setCategoryCollection(java.util.Collection<com.example.Category>) in com.example.Category cannot be applied to (com.example.Category) value.setCategoryCollection(entity); /home/bill/NetBeansProjects/WebApplication1/src/java/com/example/service/CategoryResource.java:139: incompatible types found : java.util.Collection<com.example.Category> required: com.example.Category Category oldEntity = value.getCategoryCollection(); /home/bill/NetBeansProjects/WebApplication1/src/java/com/example/service/CategoryResource.java:140: setCategoryCollection(java.util.Collection<com.example.Category>) in com.example.Category cannot be applied to (com.example.Category) value.setCategoryCollection(entity); 4 errors
Created attachment 76725 [details] Project with the bug (see table.sql in com.example package)
I'm sure this will take time to fix etc... In the mean time, is it possible to add to the bug report what the broken methods should look like?
This is a bug in our model builder for generating RESTful webservices. We fill fix this issue. In the meanwhile here is the correct code snippet. CategoriesResource.java ... /** * Persist the given entity. * * @param entity the entity to persist */ protected void createEntity(Category entity) { EntityManager em = PersistenceService.getInstance().getEntityManager(); em.persist(entity); for (Category value : entity.getCategoryCollection()) { Category oldEntity = value.getParentId(); value.setParentId(entity); if (oldEntity != null) { oldEntity.getCategoryCollection().remove(entity); } } Category parentId = entity.getParentId(); if (parentId != null) { parentId.getCategoryCollection().add(entity); } } CategoryResource.java ... /** * Updates entity using data from newEntity. * * @param entity the entity to update * @param newEntity the entity containing the new data * @return the updated entity */ protected Category updateEntity(Category entity, Category newEntity) { EntityManager em = PersistenceService.getInstance().getEntityManager(); Collection<Category> categoryCollection = entity.getCategoryCollection(); Collection<Category> categoryCollectionNew = newEntity.getCategoryCollection(); Category parentId = entity.getParentId(); Category parentIdNew = newEntity.getParentId(); entity = em.merge(newEntity); for (Category value : categoryCollection) { if (!categoryCollectionNew.contains(value)) { throw new WebApplicationException(new Throwable("Cannot remove items from categoryCollection")); } } for (Category value : categoryCollectionNew) { if (!categoryCollection.contains(value)) { Category oldEntity = value.getParentId(); value.setParentId(entity); if (oldEntity != null && !oldEntity.equals(entity)) { oldEntity.getCategoryCollection().remove(value); } } } if (parentId != null && !parentId.equals(parentIdNew)) { parentId.getCategoryCollection().remove(entity); } if (parentIdNew != null && !parentIdNew.equals(parentId)) { parentIdNew.getCategoryCollection().add(entity); } return entity; }
This issue is duplicate of issue#152170, and is already fixed into 7.0 main branch. To test this you have to download dev build of NB7.0 from http://bits.netbeans.org/download/trunk/nightly/latest/ *** This issue has been marked as a duplicate of 152170 ***