Problems adding beans to BeanItemContainer

I have a problem where even though I am adding many different beans to my BeanItemContainer, the size of the container remains 1.

I am using the following code to populate BeanItemContainer with bean objects:

public BeanItemContainer<Incident> getAllIncidents() {
        BeanItemContainer<Incident> all = new BeanItemContainer<Incident>(Incident.class);
        
        DBCollection coll = mDb.getCollection("Accident");
        DBCursor cursor = coll.find();
        System.out.println("coll count: " + coll.count());
        try {
           while(cursor.hasNext()) {
               DBObject o = cursor.next();   
               Incident i = convertJSONToBean(o.toString());
               System.out.println("id:" + i.get_id());
               all.addBean(i);
               System.out.println("count:" + all.size());
           }
        } finally {
           cursor.close();
        }
        
        return all;
    }

The output is:


coll count: 1000
id:1919D562FE5EAF814825727A002863F3
count:1
id:69253597E8A376BC4825727A002865BA
count:1
id:512A4BD8C40F80904825727A002868A8
count:1

The beans are definitely different objects, so I am not sure why the count remains at 1. Can someone point out what I am doing wrong? Thank you.

What does Incident::equals() Incident::hashCode() methods look like ??

the BeanItemContainer uses the bean itself as the unique ItemID , so you should make sure equals() hashCode() is correctly defined.

Check the result of the addBean method it should return a BeanItem instance or null if it did not add the item…

Thank you for your help. My Incident class extended com.mongodb.BasicDBObject and I found that the BasicDBObject class does override equals and hashcode methods. The result of the addBean() method was null except for the first bean.

I changed to use the BeanContainer class and all is working now.