Dinis Vieira
Dinis Vieira

Reputation: 295

GWT + JDO + ArrayList

I'm getting a Null ArrayList in a program i'm developing. For testing purposes I created this really small example that still has the same problem. I already tried diferent Primary Keys, but the problem persists.

Any ideas or suggestions?

1-Employee class

@PersistenceCapable(identityType = IdentityType.APPLICATION)
public class Employee {

    @PrimaryKey
    @Persistent(valueStrategy = IdGeneratorStrategy.IDENTITY)
    @Extension(vendorName="datanucleus", key="gae.encoded-pk", value="true")
    private String key;

    @Persistent
    private ArrayList<String> nicks;

    public Employee(ArrayList<String> nicks) {
        this.setNicks(nicks);
    }

    public String getKey() {
        return key;
    }

    public void setNicks(ArrayList<String> nicks) {
        this.nicks = nicks;
    }

    public ArrayList<String> getNicks() {
        return nicks;
    } 
}

2-EmployeeService

public class BookServiceImpl extends RemoteServiceServlet implements
EmployeeService {

    public void addEmployee(){

        ArrayList<String> nicks = new ArrayList<String>();
        nicks.add("name1");
        nicks.add("name2");

        Employee employee = new Employee(nicks);

        PersistenceManager pm = PMF.get().getPersistenceManager();
        try {
            pm.makePersistent(employee);
        } finally {
            pm.close();
        }
    }

    /**
     * @return
     * @throws NotLoggedInException
     * @gwt.typeArgs <Employee>
     */
    public Collection<Employee> getEmployees() {

        PersistenceManager pm = getPersistenceManager();

        try {
            Query q = pm.newQuery("SELECT FROM " + Employee.class.getName());

            Collection<Employee> list =
                pm.detachCopyAll((Collection<Employee>)q.execute());

            return list;

        } finally {
            pm.close();
        }
    }
}

Upvotes: 0

Views: 2017

Answers (3)

nicktmro
nicktmro

Reputation: 2288

Your Employee class did not have detachable = "true".

You should change

@PersistenceCapable(identityType = IdentityType.APPLICATION)

to

@PersistenceCapable(identityType = IdentityType.APPLICATION, detachable = "true")

Upvotes: 1

Dinis Vieira
Dinis Vieira

Reputation: 295

I changed the code a bit, and everything is normal now, still I don't know what caused this problem.

I'm using Lists now instead of the collections**(1), I return everything as a simple array trough the RPC(2)** and I changed the way I did the query**(3)**.

(1) List results = (List) query.execute();

(2) return (Employee[]) employees.toArray(new Employee[0]);

(3) Query query = pm.newQuery(Employee.class);

Upvotes: 0

Ewan Todd
Ewan Todd

Reputation: 7312

Is it significant that in addEmployee, you obtain the persistenceManager like this:

PersistenceManager pm = PMF.get().getPersistenceManager();

but in getEmployees you call it like this

PersistenceManager pm = getPersistenceManager();

without using PMF.get().

Upvotes: 0

Related Questions