Thursday, January 3, 2013

Spring MVC and NetBeans Rest service from database tutorial: many to many relationship


I was doing the Spring MVC and NetBeans Rest service from database tutorial

http://netbeans.org/kb/docs/websvc/rest.html#rest-spring

But now I applyied the tutorial to do the REST services to this database:


after finishing all the tutorial applied to the previous database, I test the URL

http://localhost:8080/WebApplication1/webresources/entities.incidents/1

and I got

{"datetime":"2012-12-28T00:00:00-05:00","description":"jIMAExx","id":"1","user":{"id":"2","identification":"1010123223","name":"pedros"}}

But now I wanted to get the categories corresponding to the many to many relationship, so I changed the incidents.java file from:
...
...
...
@XmlTransient
    public Collection<Categories> getCategoriesCollection() {
        return categoriesCollection;
    }

    public void setCategoriesCollection(Collection<Categories> categoriesCollection) {
        this.categoriesCollection = categoriesCollection;
    }
...
...
...
to
...
...
...
// @XmlTransient // Here, I just commented this line
    public Collection<Categories> getCategoriesCollection() {
        return categoriesCollection;
    }

    public void setCategoriesCollection(Collection<Categories> categoriesCollection) {
        this.categoriesCollection = categoriesCollection;
    }
...
...
...
Note that the only thing I did was commenting the @XmlTransient line. Then if I test  the url again I get:

{"categoriesCollection":[{"id":"1","name":"Categoria 1 Jaime"},{"id":"1","name":"Categoria 1 Jaime"},{"id":"1","name":"Categoria 1 Jaime"},{"id":"1","name":"Categoria 1 Jaime"},{"id":"1","name":"Categoria 1 Jaime"}],"datetime":"2012-12-28T00:00:00-05:00","description":"jIMAExx","id":"1","user":{"id":"2","identification":"1010123223","name":"pedros"}}

No comments: