Skip to main content

Pojo extends HashMap

3 replies [Last post]
gabox01
Offline
Joined: 2007-04-22
Points: 0

Hi,

I would like my POJO entities to have dynamic properties. This way i could easily decorate my object graph with JSF specific information.

For example:

public class Category extends HashMap{

private String name;
private List subCategories = new ArrayList();

public Category(String name) {
this.name = name;
}

public List getSubCategories() {
return subCategories;
}

public String getName() {
return name;
}

public void setName(String name) {
this.name = name;
}

public void setSubCategories(List subCategories) {
this.subCategories = subCategories;
}

}

My managed bean:

@ManagedBean
@RequestScoped
public class TestBean {

private List categories = new ArrayList();

public List getCategories() {
return categories;
}

@PostConstruct
public void init() {
Category rootCat = new Category("rootCategory");
rootCat.getSubCategories().add(new Category("child1"));
rootCat.getSubCategories().add(new Category("child2"));
this.categories.add(rootCat);
}
}

My xhtml page:

#{root.name}

#{child.name}

The problem is if the Category pojo extends the Hasmap, the page does not show anything. If i remove the extension, than it is ok.

Why?
Thanks.

Reply viewing options

Select your preferred way to display the comments and click "Save settings" to activate your changes.
dafei1288
Offline
Joined: 2009-03-20
Points: 0

Maybe it is cause by HashMap!

Try to define a Interface ICategory;
And then define a type implements the interface.

HashMap

:)

gabox01
Offline
Joined: 2007-04-22
Points: 0

I don't see your point.

dafei1288
Offline
Joined: 2009-03-20
Points: 0

public interface ICategory {
public String getName();
public void setName(String name);
public List getSubCategories();
public void setSubCategories(List subCategories);
}

-----------------------------------------------------------------------------------------------------------

public class Category extends HashMap implements ICategory{
private static final long serialVersionUID = 1L;
private String name;
private List subCategories = new ArrayList();

public Category(String name) {
this.name = name;
}

public List getSubCategories() {
return subCategories;
}

public String getName() {
return name;
}

public void setName(String name) {
this.name = name;
}

public void setSubCategories(List subCategories) {
this.subCategories = subCategories;
}

}

----------------------------------------------------------------------------------------------------------

try this ;)