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.

Bug 158172 - Generate RESTful webservice on self reference one to many produces non-compiling code
Summary: Generate RESTful webservice on self reference one to many produces non-compil...
Status: RESOLVED DUPLICATE of bug 152170
Alias: None
Product: webservices
Classification: Unclassified
Component: REST (show other bugs)
Version: 6.x
Hardware: All All
: P1 blocker (vote)
Assignee: Ayub Khan
URL:
Keywords:
Depends on:
Blocks:
 
Reported: 2009-02-09 03:14 UTC by billrobertson42
Modified: 2009-02-12 21:04 UTC (History)
0 users

See Also:
Issue Type: DEFECT
Exception Reporter:


Attachments
Project with the bug (see table.sql in com.example package) (19.90 KB, application/x-gzip)
2009-02-09 03:22 UTC, billrobertson42
Details

Note You need to log in before you can comment on or make changes to this bug.
Description billrobertson42 2009-02-09 03:14:48 UTC
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
Comment 1 billrobertson42 2009-02-09 03:22:23 UTC
Created attachment 76725 [details]
Project with the bug (see table.sql in com.example package)
Comment 2 billrobertson42 2009-02-09 03:40:10 UTC
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?
Comment 3 Ayub Khan 2009-02-12 01:17:36 UTC
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;
    }
Comment 4 Ayub Khan 2009-02-12 21:04:07 UTC
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 ***