Hello everyone.

I've an issue : I can't update a pojo object.

I have two classes : A user and a location.
A user HAS A location.

My servlet do this :
1) create a user without a location, make it persist
2) retrieve a user, set the location (for the first time), make it persist
3) retrieve a user, set a new location, make it persist
4) retrieve a user, show if the position has been updated

My issue :
On step 4), I see the location initialized the first time. My new position 
is never saved.

More strange, I can see pojo location objects in the DataStore Viewer (
http://localhost:8888/_ah/admin/datastore)

If someone know how to fix it, I'll be grateful.

~Yoann~

Complete source code are in the attached files.
A more simple source :

Pojos:

@PersistenceCapable
public class POJOUser {

    /** Primary key */
    @PrimaryKey
    @Persistent
    private String key = null;

    /** Username */
    @Persistent
    private String username = null;

    /** Password */
    @Persistent
    private String password = null;
    
    /** The last location of the user */
    @Persistent
    private POJOLocation lastLocation = null;
    ....
}

@PersistenceCapable
public class POJOLocation implements Location {
    
    /** Primary key */
    @PrimaryKey
    @Persistent(valueStrategy = IdGeneratorStrategy.IDENTITY)
    private Key key = null;

    /** X */
    @Persistent
    private Double x = null;

    /** Y */
    @Persistent
    private Double y = null;
    ....
}

PMF :

public class DataAccessLayerForGoogleAppEngine implements DataAccessLayer {

    /** Persistence Manager */
    private PersistenceManagerFactory PMF = JDOHelper
    .getPersistenceManagerFactory("transactions-optional");

@Override
    public void updateUserLocation(String username, Location newLocation) {
 PersistenceManager pm = PMF.getPersistenceManager();
try {

    // Get the user
    POJOUser user = pm.getObjectById(POJOUser.class, username);
    if (user != null) {
// Update the location
user.setLastLocation(new POJOLocation(newLocation));
    }
    
} catch (JDOObjectNotFoundException ex) {

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

-- 
You received this message because you are subscribed to the Google Groups 
"Google App Engine for Java" group.
To post to this group, send email to google-appengine-j...@googlegroups.com.
To unsubscribe from this group, send email to 
google-appengine-java+unsubscr...@googlegroups.com.
For more options, visit this group at 
http://groups.google.com/group/google-appengine-java?hl=en.

package lan.polichouz.tracer.server.impl;

import java.util.List;

import javax.jdo.JDOHelper;
import javax.jdo.JDOObjectNotFoundException;
import javax.jdo.PersistenceManager;
import javax.jdo.PersistenceManagerFactory;
import javax.jdo.Query;

import lan.polichouz.tracer.server.DataAccessLayer;
import lan.polichouz.tracer.server.Location;
import lan.polichouz.tracer.server.impl.pojo.POJOLocation;
import lan.polichouz.tracer.server.impl.pojo.POJOUser;

/**
 * Implementation of a Data Access Layer using Google AppEngine framework.
 * 
 * @author yoyo
 */
public class DataAccessLayerForGoogleAppEngine implements DataAccessLayer {

    // TODO user and admin name MUST be unique (primary key).

    /** Persistence Manager */
    private PersistenceManagerFactory PMF = JDOHelper
	    .getPersistenceManagerFactory("transactions-optional");

    @Override
    public String getUsernamePassword(String username) {

	String realPassword = null;

	PersistenceManager pm = PMF.getPersistenceManager();
	try {

	    // Get the user
	    POJOUser user = pm.getObjectById(POJOUser.class, username);
	    if (user != null) {
		realPassword = user.getPassword();
	    }
	    
	} catch (JDOObjectNotFoundException ex) {
	    ex.printStackTrace();

	} finally {
	    pm.close();
	}

	return realPassword;
    }

    @Override
    public String getAdminPassword(String adminName) {

	String realPassword = null;

	PersistenceManager pm = PMF.getPersistenceManager();
	try {

	    // Get the user
	    POJOUser user = pm.getObjectById(POJOUser.class, adminName);
	    if (user != null) {
		// Get the password
		realPassword = user.getPassword();
	    }
	    
	} catch (JDOObjectNotFoundException ex) {

	} finally {
	    pm.close();
	}

	return realPassword;
    }

    @Override
    public void updateUserLocation(String username, Location newLocation) {
	
	PersistenceManager pm = PMF.getPersistenceManager();
	try {

	    // Get the user
	    POJOUser user = pm.getObjectById(POJOUser.class, username);
	    if (user != null) {
		// Update the location
		user.setLastLocation(new POJOLocation(newLocation)); //XXX not saved
	    }
	    
	} catch (JDOObjectNotFoundException ex) {

	} finally {
	    pm.close();
	}
    }

    @Override
    public Location getUserLocation(String username) {

	Location location = null;

	PersistenceManager pm = PMF.getPersistenceManager();
	try {

	    // Get the user
	    POJOUser user = pm.getObjectById(POJOUser.class, username);
	    if (user != null) {
		// Get the location
		location = user.getLastLocation();
	    }
	    
	} catch (JDOObjectNotFoundException ex) {

	} finally {
	    pm.close();
	}

	return location;
    }

    @SuppressWarnings("unchecked")
    @Override
    public void clear() {

	List<POJOUser> users = null;

	PersistenceManager pm = PMF.getPersistenceManager();
	try {

	    Query query = pm.newQuery(POJOUser.class);
	    try {

		// Get all users
		System.out.println("Getting all users...");
		users = (List<POJOUser>) query.execute();
		System.out.println("Getting all users OK");

	    } finally {
		query.closeAll();
	    }

	    // Remove all users
	    if (users != null) {
		System.out.println("Deleting all users...");
		pm.deletePersistentAll(users);
		System.out.println("Deleting all users OK");
	    }

	} finally {
	    pm.close();
	}
    }

    @Override
    public void initialize() {

	PersistenceManager pm = PMF.getPersistenceManager();
	try {

	    // Create an admin
	    POJOUser admin = new POJOUser("admin1", "pass");
	    pm.makePersistent(admin);

	    // Create a user
	    POJOUser user = new POJOUser("user1", "pass");
	    pm.makePersistent(user);

	} finally {
	    pm.close();
	}
    }
}
package lan.polichouz.tracer.server.impl.pojo;

import javax.jdo.annotations.IdGeneratorStrategy;
import javax.jdo.annotations.PersistenceCapable;
import javax.jdo.annotations.Persistent;
import javax.jdo.annotations.PrimaryKey;

import com.google.appengine.api.datastore.Key;

import lan.polichouz.tracer.server.Location;

/**
 * A simple X-Y location.
 * 
 * @author yoyo
 */
@PersistenceCapable
public class POJOLocation implements Location {
    
    /** Primary key */
    @PrimaryKey
    @Persistent(valueStrategy = IdGeneratorStrategy.IDENTITY)
    private Key key = null;

    /** X */
    @Persistent
    private Double x = null;

    /** Y */
    @Persistent
    private Double y = null;

    /**
     * Initialize a new Pojo location.
     * 
     * @param x
     * @param y
     */
    public POJOLocation(Double x, Double y) {

	if (x == null) {
	    throw new IllegalArgumentException("x is invalid");
	}

	if (y == null) {
	    throw new IllegalArgumentException("y is invalid");
	}

	this.x = x;
	this.y = y;
    }
    
    /**
     * Initialize a new Pojo Location
     * @param newLocation
     */
    public POJOLocation(Location location) {
	
	if (location == null) {
	    throw new IllegalArgumentException("location is invalid");
	}
	
	if (location.getX() == null) {
	    throw new IllegalArgumentException("location (X) is invalid");
	}
	
	if (location.getY() == null) {
	    throw new IllegalArgumentException("location (Y) is invalid");
	}
	
	x = location.getX();
	y = location.getY();
    }

    /**
     * Get the primary key.
     * @return
     */
    public Key getKey() {
	return key;
    }

    @Override
    public Double getX() {
	return x;
    }
    
    @Override
    public Double getY() {
	return y;
    }

    @Override
    public int hashCode() {
	return toString().hashCode();
    }

    @Override
    public boolean equals(Object obj) {
	return obj instanceof POJOLocation
		&& ((Location) obj).getX().equals(getX())
		&& ((Location) obj).getY().equals(getY());
    }

    @Override
    public String toString() {
	StringBuilder builder = new StringBuilder();
	builder.append("POJOLocation [x=");
	builder.append(x);
	builder.append(", y=");
	builder.append(y);
	builder.append("]");
	return builder.toString();
    }
}
package lan.polichouz.tracer.server.impl.pojo;

import javax.jdo.annotations.PersistenceCapable;
import javax.jdo.annotations.Persistent;
import javax.jdo.annotations.PrimaryKey;

import lan.polichouz.tracer.server.Location;

/**
 * A user, with a username and a password.
 * 
 * @author yoyo
 */
@PersistenceCapable
public class POJOUser {

    /** Primary key */
    @PrimaryKey
    @Persistent
    private String key = null;

    /** Username */
    @Persistent
    private String username = null;

    /** Password */
    @Persistent
    private String password = null;
    
    /** The last location of the user */
    @Persistent
    private POJOLocation lastLocation = null;

    /**
     * Initialize a new user.
     * 
     * @param username
     *            The user name
     * @param password
     *            The user password.
     */
    public POJOUser(String username, String password) {

	// Check parameters
	if (username == null || username.isEmpty()) {
	    throw new IllegalArgumentException("username is invalid");
	}

	if (password == null || password.isEmpty()) {
	    throw new IllegalArgumentException("password is invalid");
	}

	this.username = username;
	this.password = password;
	
	key = username;
    }

    /**
     * Get the primary key.
     * 
     * @return
     */
    public String getKey() {
	return key;
    }

    /**
     * Get the user name.
     * 
     * @return
     */
    public String getUsername() {
	return username;
    }

    /**
     * Get the password.
     * 
     * @return
     */
    public String getPassword() {
	return password;
    }
    
    /**
     * Get the last location of the user.
     * 
     * @return The last location of the user, or null if there are no location.
     */
    public Location getLastLocation() {
	return lastLocation;
    }
    
    /**
     * Set the last location of the user.
     * 
     * @param newLocation The new location of the user.
     */
    public void setLastLocation(POJOLocation newLocation) {
	lastLocation = newLocation;
    }

    @Override
    public int hashCode() {
	return toString().hashCode();
    }

    @Override
    public boolean equals(Object obj) {
	return obj instanceof POJOUser
		&& ((POJOUser) obj).getUsername().equals(getUsername())
		&& ((POJOUser) obj).getPassword().equals(getPassword());
    }

    @Override
    public String toString() {
	StringBuilder builder = new StringBuilder();
	builder.append("User [username=");
	builder.append(username);
	builder.append(", password=");
	builder.append(password);
	builder.append("]");
	return builder.toString();
    }
}

Reply via email to