JDO + RequestFactory - entity versioning - google-app-engine

I was trying to set-up a really trivial RequestFactory example, but I failed. I persist entities in to my datastore just find, however when trying to pull them out again, i get a
com.google.web.bindery.requestfactory.server.UnexpectedException: The persisted entity with id aglub19hcHBfaWRyCgsSBFVzZXIYBAw has a null version
So first of all this is my JPO annotated entity class. At the end you find to static function for RequestFactory to call, and a non-static member function which will become an InstanceRequest.
package com.test.server;
import java.util.List;
import javax.jdo.PersistenceManager;
import javax.jdo.annotations.Column;
import javax.jdo.annotations.Extension;
import javax.jdo.annotations.IdGeneratorStrategy;
import javax.jdo.annotations.IdentityType;
import javax.jdo.annotations.PersistenceCapable;
import javax.jdo.annotations.Persistent;
import javax.jdo.annotations.PrimaryKey;
import javax.jdo.annotations.Version;
import javax.jdo.annotations.VersionStrategy;
#PersistenceCapable(identityType = IdentityType.APPLICATION)
#Version(strategy = VersionStrategy.VERSION_NUMBER, column = "VERSION", extensions = { #Extension(vendorName = "datanucleus", key = "field-name", value = "version") })
public class User {
public User() {
}
public User(String name) {
this.name = name;
}
#PrimaryKey
#Persistent(valueStrategy = IdGeneratorStrategy.IDENTITY)
#Extension(vendorName = "datanucleus", key = "gae.encoded-pk", value = "true")
private String id;
#Persistent
#Column(name = "version")
private Integer version;
#Persistent
private String name;
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
public Integer getVersion() {
return version;
}
public void setVersion(Integer version) {
this.version = version;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public static final PersistenceManager persistenceManager() {
return PMF.get().getPersistenceManager();
}
#SuppressWarnings("unchecked")
public static List<User> findAllUsers() {
PersistenceManager pm = persistenceManager();
try {
String query = "SELECT FROM " + User.class.getName();
List<User> objects = (List<User>) pm.newQuery(query).execute();
objects.size(); // This is the workaround to retrieve all objects
return objects;
} finally {
pm.close();
}
}
public static User findUser(String id) {
PersistenceManager pm = persistenceManager();
try {
User u = pm.getObjectById(User.class, id);
return u;
} finally {
pm.close();
}
}
public void persist() {
PersistenceManager pm = persistenceManager();
try {
pm.makePersistent(this);
} finally {
pm.close();
}
}
}
The RequestFactory interface itself is really simple
package com.test.shared;
import com.google.web.bindery.requestfactory.shared.RequestFactory;
public interface UserOrderRequestFactory extends RequestFactory {
UserRequest userRequest();
}
so is the corresponding RequestContext
package com.test.shared;
import java.util.List;
import com.google.web.bindery.requestfactory.shared.InstanceRequest;
import com.google.web.bindery.requestfactory.shared.RequestContext;
import com.google.web.bindery.requestfactory.shared.Service;
import com.google.web.bindery.requestfactory.shared.Request;
import com.test.server.User;
#Service(User.class)
public interface UserRequest extends RequestContext {
Request<List<UserProxy>> findAllUsers();
InstanceRequest<UserProxy, Void> persist();
}
Here is the proxy of user for the client side
package com.test.shared;
import com.google.web.bindery.requestfactory.shared.EntityProxy;
import com.google.web.bindery.requestfactory.shared.EntityProxyId;
import com.google.web.bindery.requestfactory.shared.ProxyFor;
#ProxyFor(com.test.server.User.class)
public interface UserProxy extends EntityProxy {
EntityProxyId<UserProxy> stableId();
String getName();
void setName(String name);
}
and finally my onModuleLoad() which first persists a user and then gets a list of all users.
public void onModuleLoad() {
final EventBus eventBus = new SimpleEventBus();
requestFactory = GWT.create(UserOrderRequestFactory.class);
requestFactory.initialize(eventBus);
UserRequest userRequest = requestFactory.userRequest();
UserProxy user = userRequest.create(UserProxy.class);
user.setName("Luigi");
userRequest.persist().using(user).fire( new Receiver<Void>()
{
#Override
public void onSuccess(Void arg0)
{
GWT.log("User persisted.");
}
});
userRequest = requestFactory.userRequest();
Request<List<UserProxy>> findAllUsersRequest = userRequest.findAllUsers();
findAllUsersRequest.fire( new Receiver<List<UserProxy>>() {
#Override
public void onSuccess(List<UserProxy> list) {
for(UserProxy u: list) {
GWT.log(u.getName());
}
}
});
Any input is welcome. I would be happy to receive any advice on this.
Thank you in advance.

While JPA seems to do this automatically it seems to be my job to advance the version counter in JDO. I added the following code to my persist routine in User.java
// JPA #Version does this automatically, but JDO #Version is not working like that. Not sure why.
if (version == null) {
version = 0l;
}
version++;

I am not sure if it matters but the #Version column = "VERSION" does not match the #Column(name = "version").
However GAE does not really have 'columns' as such and you can just ignore them by removing the column = "" and the #Column.
See http://gae-java-persistence.blogspot.com.au/2009/10/optimistic-locking-with-version.html

Related

Hibernate relationship issues

I'm trying to store a couple of objects in a Oracle database using Hibernate. However, I can't seem to get the mapping relations right. As it stands I'm getting a NullPointerException.
In the end, every Client should contain an id, a first and last name, an e-mail and a set of reservations.
Every flight should contain a flight number, a starting time and a starting airport.
The bookings should have an id, client, flight, the date of booking and the amount of booked seats.
At the moment I'm just testing it with a single client as to not over-complicate it but I have gotten rather confused.
Here is what I have done till now:
Main
import java.sql.Date;
import java.sql.Time;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.persistence.EntityManager;
import javax.persistence.EntityManagerFactory;
import javax.persistence.Persistence;
public class JPAApplication {
private EntityManagerFactory entityManagerFactory;
public JPAApplication() {
Logger.getLogger("org.hibernate").setLevel(Level.ALL);
entityManagerFactory = Persistence.createEntityManagerFactory("DB1");
}
public void testFlights() {
EntityManager em = entityManagerFactory.createEntityManager();
em.getTransaction().begin();
Client firstClient = new Client("Kiro", "Betona", "kirobetona#gmail.com");
em.persist(firstClient);
}
public static void main(String[] args) {
JPAApplication app = new JPAApplication();
app.testFlights();
}
}
Client
import java.util.*;
import javax.persistence.*;
import javax.validation.constraints.NotNull;
#Entity
public class Client {
private int id;
private String firstName;
private String lastName;
private String email;
private Set<Booking> reservations;
Client(String firstName, String lastName, String email) {
this.firstName = firstName;
this.lastName = lastName;
this.email = email;
}
#Id
#GeneratedValue(strategy=GenerationType.IDENTITY)
public int getId() {
return id;
}
public String getFirstName() {
return firstName;
}
public void setFirstName(String firstName) {
this.firstName = firstName;
}
public String getLastName() {
return lastName;
}
public void setLastName(String lastName) {
this.lastName = lastName;
}
public String getEmail() {
return email;
}
public void setEmail(String email) {
this.email = email;
}
public void setId(int id) {
this.id = id;
}
#OneToMany(targetEntity = Booking.class, mappedBy="client", fetch=FetchType.EAGER)
public Set<Booking> getReservations() {
return reservations;
}
public void setReservations(Set<Booking> reservations) {
this.reservations = reservations;
}
}
Booking
import java.util.Date;
import javax.persistence.*;
import javax.persistence.Temporal;
import javax.persistence.TemporalType;
import javax.validation.constraints.NotNull;
#Entity
public class Booking {
#Id
#GeneratedValue(strategy=GenerationType.IDENTITY)
private int id;
private Client client;
private Flight flight;
private int bookedSeats = 1;
#Temporal(TemporalType.DATE)
private Date bookingDate;
Booking(Client client, Flight flight, Date bookingDate) {
this.client = client;
this.flight = flight;
this.bookingDate = bookingDate;
}
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
#ManyToOne
#JoinColumn(name="id")
public Client getClient() {
return client;
}
#OneToOne
#PrimaryKeyJoinColumn(name="flight")
public Flight getFlight() {
return flight;
}
public int getBookedSeats() {
return bookedSeats;
}
public void setBookedSeats(int bookedSeats) {
this.bookedSeats = bookedSeats;
}
public Date getBookingDate() {
return bookingDate;
}
public void setBookingDate(Date bookingDate) {
this.bookingDate = bookingDate;
}
public void setClient(Client client) {
this.client = client;
}
public void setFlight(Flight flight) {
this.flight = flight;
}
}
Flight
import java.sql.Time;
import javax.persistence.*;
import javax.validation.constraints.NotNull;
#Entity
public class Flight {
private String flightNumber;
private Time startingTime;
private String startingAirport;
Flight(String flightNumber, Time startingTime, String startingAirport) {
this.flightNumber = flightNumber;
this.startingTime = startingTime;
this.startingAirport = startingAirport;
}
#Id
#OneToOne(targetEntity = Booking.class, mappedBy="flight", fetch=FetchType.EAGER)
#PrimaryKeyJoinColumn(name="flight")
public String getFlightNumber() {
return flightNumber;
}
public Time getStartingTime() {
return startingTime;
}
public void setStartingTime(Time startingTime) {
this.startingTime = startingTime;
}
public String getStartingAirport() {
return startingAirport;
}
public void setStartingAirport(String startingAirport) {
this.startingAirport = startingAirport;
}
public void setFlightNumber(String flightNumber) {
this.flightNumber = flightNumber;
}
}
I see some confusion when you are using the #JoinColumn annotation in Booking: you are telling Booking to use a column named "id" as the Foreign Key to Client, however that conflicts with the Booking primary key.
You could instead make the column name separate, like this:
#JoinColumn(name="clientId")
Also, for Hibernate to instantiate the Entity objects, the entity should have a no-arg constructor. The default no-arg constructors have been overridden in the example code.

App Engine endpoint to accept POST data in request body

I have created a Google Endpoint in my App Engine Server as follows:
package com.xxxxx.gcmbackend;
import com.google.api.server.spi.config.Api;
import com.google.api.server.spi.config.ApiMethod;
import com.google.api.server.spi.config.ApiNamespace;
import com.google.api.server.spi.response.CollectionResponse;
import java.util.List;
import java.util.logging.Logger;
import javax.inject.Named;
import static com.xxxxxx.gcmbackend.OfyService.ofy;
#Api(
name = "register",
version = "v1",
namespace = #ApiNamespace(
ownerDomain = "gcmbackend.xxxxx.com",
ownerName = "gcmbackend.xxxxx.com",
packagePath=""
)
)
public class UserRegistrationEndpoint {
private static final Logger log = Logger.getLogger(RegistrationEndpoint.class.getName());
#ApiMethod(name = "register")
public void registerDevice(#Named("regId") String regId, #Named("username") String username, #Named("phone") String phone) {
if(findRecord(regId) != null) {
log.info("Device " + regId + " already registered, skipping register");
return;
}
RegistrationRecord record = new RegistrationRecord();
record.setRegId(regId);
record.setUsername(username);
record.setPhone(phone);
ofy().save().entity(record).now();
}
private RegistrationRecord findRecord(String regId) {
return ofy().load().type(RegistrationRecord.class).filter("regId", regId).first().now();
}
}
This works perfectly in creating new User records. The API is of the following format:
http://example.appspot.com/_ah/api/register/v1/registerDevice/<regId>/<username>/<phone>
However, I want the url to look like this:
http://example.appspot.com/_ah/api/register/v1/registerDevice/
and then send POST data as follows:
{
regId: "some_value",
username: "some_value",
phone: "some_value"
}
What do I need to change in my Endpoint in order to achieve this format?
You need to create a java bean with regId, username and phone attributes e.g. RegistrationInput.
public class RegistrationInput {
private String regId;
private String username;
private String phone;
public String getRegId() {
return regId;
}
public void setRegId(String regId) {
this.regId = regId;
}
public String getUsername() {
return username;
}
public void setUsername(String username) {
this.username = username;
}
public String getPhone() {
return phone;
}
public void setPhone(String phone) {
this.phone = phone;
}
}
Then add the above java bean RegistrationInput, as a parameter to the ApiMethod
#ApiMethod(name = "register")
public void registerDevice(RegistrationInput input) {
.....
}

Issues with setting up AppEngine Endpoint class from Objectify in Eclipse

I am very new to Objectify and I am trying to create endpoints for a simple object. The issue that I am running into is that the endpoint class that Eclipse auto creates has references to JPA. Do I have to manually create my own endpoint class instead of using the "Generate Endpoint Class" in Eclipse? Here are the steps I took to set up my project and the source code:
Created a Web Application Project in Eclipse (app engine project)
Added Objectify-5.0.3.jar and guava-17.0.jar files to web-inf/lib and project's class path
Created a simple class Car.java and added annotations
Registered the entity in servlet (ObjectifyService.register(Car.class);
Right clicked on the Car class and selected Google App Engine (WPT) > Generate Cloud End Point Class
Deployed to App Engine and from API Explorer tried to insert a record and I got this error in logs:
https://objectify-example-650.appspot.com/_ah/api/carendpoint/v1/car
Method: carendpoint.insertCar
Error Code: 400
Reason: badRequest
Message: java.lang.IllegalArgumentException: Type ("com.appengine.objectify.Car") is not that of an entity but needs to be for this operation
I also see this error/warning in app engine project log:
<pre>
org.datanucleus.metadata.MetaDataManager loadPersistenceUnit: Class com.appengine.objectify.CarEndpoint was specified in persistence-unit transactions-optional but not annotated, so ignoring
</pre>
Here is my source code:
import com.googlecode.objectify.annotation.Entity;
#Entity
public class Car {
#Id Long id;
String vin;
String color;
private Car() {}
public Long getId() {
return id;
}
public void setId(Long id) {
this.id = id;
}
public String getVin() {
return vin;
}
public void setVin(String vin) {
this.vin = vin;
}
public String getColor() {
return color;
}
public void setColor(String color) {
this.color = color;
}
}
The endpoint class:
#Api(name = "carendpoint", namespace = #ApiNamespace(ownerDomain = "appengine.com", ownerName = "appengine.com", packagePath = "objectify"))
public class CarEndpoint {
#SuppressWarnings({ "unchecked", "unused" })
#ApiMethod(name = "listCar")
public CollectionResponse<Car> listCar(
#Nullable #Named("cursor") String cursorString,
#Nullable #Named("limit") Integer limit) {
EntityManager mgr = null;
Cursor cursor = null;
List<Car> execute = null;
try {
mgr = getEntityManager();
Query query = mgr.createQuery("select from Car as Car");
if (cursorString != null && cursorString != "") {
cursor = Cursor.fromWebSafeString(cursorString);
query.setHint(JPACursorHelper.CURSOR_HINT, cursor);
}
if (limit != null) {
query.setFirstResult(0);
query.setMaxResults(limit);
}
execute = (List<Car>) query.getResultList();
cursor = JPACursorHelper.getCursor(execute);
if (cursor != null)
cursorString = cursor.toWebSafeString();
for (Car obj : execute)
;
} finally {
mgr.close();
}
return CollectionResponse.<Car> builder().setItems(execute)
.setNextPageToken(cursorString).build();
}
#ApiMethod(name = "getCar")
public Car getCar(#Named("id") Long id) {
EntityManager mgr = getEntityManager();
Car car = null;
try {
car = mgr.find(Car.class, id);
} finally {
mgr.close();
}
return car;
}
#ApiMethod(name = "insertCar")
public Car insertCar(Car car) {
EntityManager mgr = getEntityManager();
try {
if (containsCar(car)) {
throw new EntityExistsException("Object already exists");
}
mgr.persist(car);
} finally {
mgr.close();
}
return car;
}
#ApiMethod(name = "updateCar")
public Car updateCar(Car car) {
EntityManager mgr = getEntityManager();
try {
if (!containsCar(car)) {
throw new EntityNotFoundException("Object does not exist");
}
mgr.persist(car);
} finally {
mgr.close();
}
return car;
}
#ApiMethod(name = "removeCar")
public void removeCar(#Named("id") Long id) {
EntityManager mgr = getEntityManager();
try {
Car car = mgr.find(Car.class, id);
mgr.remove(car);
} finally {
mgr.close();
}
}
private boolean containsCar(Car car) {
EntityManager mgr = getEntityManager();
boolean contains = true;
try {
Car item = mgr.find(Car.class, car.getId());
if (item == null) {
contains = false;
}
} finally {
mgr.close();
}
return contains;
}
private static EntityManager getEntityManager() {
return EMF.get().createEntityManager();
}
}
EFM class created by eclipse when autogenerating the endpoint class:
import javax.persistence.EntityManagerFactory;
import javax.persistence.Persistence;
public final class EMF {
private static final EntityManagerFactory emfInstance = Persistence
.createEntityManagerFactory("transactions-optional");
private EMF() {
}
public static EntityManagerFactory get() {
return emfInstance;
}
}
The auto-gen cloud endpoint classes, are for JPA/ JDO only, its covered here: Objectify with Endpoints for android Hope that helps
Thanks Tim! I ended up writing the endpoint class. As you mentioned eclipse does not create endpoint class for classes annotated for Objectity.
package com.appengine.objectify;
import static com.appengine.objectify.OfyService.ofy;
import com.google.api.server.spi.config.Api;
import com.google.api.server.spi.config.ApiMethod;
import com.google.api.server.spi.config.ApiNamespace;
import java.util.ArrayList;
import java.util.List;
import javax.inject.Named;
#Api(name = "carendpoint", namespace = #ApiNamespace(ownerDomain = "appengine.com", ownerName = "appengine.com", packagePath = "objectify"))
public class CarEndpoint {
#ApiMethod(name = "listCar")
public List<Car> listCar() {
List<Car> result = new ArrayList<Car>();
result = ofy().load().type(Car.class).list();
return result;
}
#ApiMethod(name = "getCar")
public Car getCar(#Named Long id) {
Car Car = ofy().load().type(Car.class).id(id).now();
return Car;
}
#ApiMethod(name = "insertCar")
public Car insertCar(Car Car) {
ofy().save().entity(Car).now();
return Car;
}
#ApiMethod(name = "removeCar")
public void removeCar(#Named Long id) {
ofy().delete().type(Car.class).id(id).now();
}
}

Inconsistent cascading persistence-by-reachability behavior with JDO, App Engine, Data Nucleus, JUnit

I'm experimenting with App Engine, using JDO and DataNucleus for persistence. I have a simple domain that includes several unidirectional relationships. The question comes with nesting those relationships:
Civilization -(1-1)-> Clan
Civilization -(1-1)-> Land
Civilization -(1-1)-> Military -(1-N)-> Armies (this is inconsistent)
Civilization -(1-N)-> Settlement
According to the DataNucleus Documentation, persistence-by-reachability semantics should persist everything by cascading on a persist of a Civilization. I have a JUnit test to check the basic storage and retrieval of these objects, but its behavior is inconsistent. With no changes to the code, repeated runs of the test give nondeterministic results. Specifically, the armies only persist about 50% of the time. They are the only test that fails.
I could more easily understand a scenario where Armies never persist, but the irregular behavior has me at a loss. Everything else persists correctly and consistently. I've tried wrapping the factory method in a transaction and I've tried bidirectional relationships, and neither have changed the 50/50 pass/fail split in JUnit.
I am using Annotation-based configuration for DataNucleus, as described in the App Engine documentation (link not included because of anti-spam measures). I apologize for the large amount of code attached; I just don't know where I'm going wrong.
CivilizationCreateTest.java:
package com.moffett.grunzke.server;
import static org.junit.Assert.*;
import java.util.ArrayList;
import java.util.List;
import javax.jdo.PersistenceManager;
import javax.jdo.Query;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import com.google.appengine.api.users.User;
import com.google.appengine.api.users.UserService;
import com.google.appengine.api.users.UserServiceFactory;
import com.google.appengine.tools.development.testing.LocalServiceTestHelper;
import com.moffett.grunzke.generic.GenericHelperFactory;
import com.moffett.grunzke.server.civilization.Army;
import com.moffett.grunzke.server.civilization.Civilization;
import com.moffett.grunzke.server.civilization.Clan;
import com.moffett.grunzke.server.civilization.Land;
import com.moffett.grunzke.server.civilization.Military;
import com.moffett.grunzke.server.civilization.Settlement;
#SuppressWarnings("unchecked")
public class CivilizationCreationTest
{
private final LocalServiceTestHelper helper = new LocalServiceTestHelper(
new LocalDatastoreServiceTestConfig(),
new LocalUserServiceTestConfig())
.setEnvIsLoggedIn(true)
.setEnvEmail("generic.user#gmail.com")
.setEnvAuthDomain("google.com");
#Before
public void setUp()
{
helper.setUp();
}
#After
public void tearDown()
{
helper.tearDown();
}
#Test
public void testCivilizationCreation()
{
String clanName = "Test Clan";
String rulerName = "Test Ruler";
UserService userService = UserServiceFactory.getUserService();
User user = userService.getCurrentUser();
if (user == null)
{
fail("No user");
}
PersistenceManager pm = PMF.get().getPersistenceManager();
CivilizationFactory.newInstance(user, clanName, rulerName);
// We check to make sure that 1, and only 1 Civilization has been made.
Query q1 = pm.newQuery("SELECT FROM " + Civilization.class.getName());
List<Civilization> allCivilizations = (List<Civilization>) q1.execute();
assertTrue(allCivilizations.size() == 1);
// Now we move on to checking the other aspects.
Civilization persistentCiv = allCivilizations.get(0);
Clan persistentClan = persistentCiv.getClan();
Land persistentLand = persistentCiv.getLand();
Military persistentMilitary = persistentCiv.getMilitary();
ArrayList<Settlement> persistentSettlements = persistentCiv.getSettlements();
// Make sure Civ has pointers to all the necessary elements.
assertTrue(persistentClan != null);
assertTrue(persistentLand != null);
assertTrue(persistentMilitary != null);
assertTrue(persistentMilitary.getArmies() != null);
assertTrue(persistentSettlements != null);
// Lastly we want to make sure that there is only one entry in each of Clan,
// Land, Military, Army, Settlement.
Query q2 = pm.newQuery("SELECT FROM " + Clan.class.getName());
List<Clan> allClans = (List<Clan>) q2.execute();
assertTrue(allClans.size() == 1);
Query q3 = pm.newQuery("SELECT FROM " + Land.class.getName());
List<Land> allLand = (List<Land>) q3.execute();
assertTrue(allLand.size() == 1);
Query q4 = pm.newQuery("SELECT FROM " + Military.class.getName());
List<Military> allMilitary = (List<Military>) q4.execute();
assertTrue(allMilitary.size() == 1);
Query q5 = pm.newQuery("SELECT FROM " + Army.class.getName());
List<Army> allArmy = (List<Army>) q5.execute();
// *** THIS FAILS 50% OF THE TIME ***
assertTrue(allArmy.size() == 1);
Query q6 = pm.newQuery("SELECT FROM " + Settlement.class.getName());
List<Settlement> allSettlement = (List<Settlement>) q6.execute();
assertTrue(allSettlement.size() == 1);
}
}
CivilizationFactory.java:
package com.moffett.grunzke.server;
import java.util.ArrayList;
import com.google.appengine.api.users.User;
import com.moffett.grunzke.server.civilization.Army;
import com.moffett.grunzke.server.civilization.Civilization;
import com.moffett.grunzke.server.civilization.Clan;
import com.moffett.grunzke.server.civilization.Land;
import com.moffett.grunzke.server.civilization.Military;
import com.moffett.grunzke.server.civilization.Settlement;
public class CivilizationFactory
{
public static Civilization newInstance(User user, String clanName, String rulerName)
{
// First we make a new clan.
Clan clan = new Clan();
clan.setUser(user);
clan.setClanName(clanName);
clan.setRulerName(rulerName);
// Don't need land.save() because of persistence-by-reachability
// Now we need to make a new Land.
Land land = new Land();
land.setArableLand(100);
land.setPasturableLand(0);
land.setLandUsedBySettlements(0);
// Don't need land.save() because of persistence-by-reachability
// Now we need to make a new Military
Military military = new Military();
Army army = new Army();
army.setMeleeUnits(10);
army.setRangedUnits(10);
army.setMountedUnits(10);
military.addArmy(army);
// Don't need military.save() because of persistence-by-reachability
// Now we need to make a new Settlement
Settlement settlement = new Settlement();
// Don't need settlement.save() because of persistence-by-reachability
ArrayList<Settlement> settlements = new ArrayList<Settlement>();
settlements.add(settlement);
// Lastly join everything together in the civ
Civilization civ = new Civilization();
civ.setClan(clan);
civ.setLand(land);
civ.setMilitary(military);
civ.setSettlements(settlements);
civ.save();
// civ.save should casacde to cover all of the elements above
return civ;
}
}
Civilization.java:
package com.moffett.grunzke.server.civilization;
import java.util.ArrayList;
import javax.jdo.PersistenceManager;
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 com.moffett.grunzke.server.PMF;
#PersistenceCapable
public class Civilization
{
#PrimaryKey
#Persistent(valueStrategy = IdGeneratorStrategy.IDENTITY)
private Key key;
#Persistent
private Clan clan;
#Persistent
private Land land;
#Persistent
private Military military;
#Persistent
private ArrayList<Settlement> settlements = new ArrayList<Settlement>();
public void save()
{
PersistenceManager pm = PMF.get().getPersistenceManager();
try
{
pm.makePersistent(this);
}
finally
{
pm.close();
}
}
public ArrayList<Settlement> getSettlements()
{
return settlements;
}
public void setSettlements(ArrayList<Settlement> settlements)
{
this.settlements = settlements;
}
public Key getKey()
{
return key;
}
public void setKey(Key key)
{
this.key = key;
}
public Clan getClan()
{
return clan;
}
public void setClan(Clan clan)
{
this.clan = clan;
}
public Land getLand()
{
return land;
}
public void setLand(Land land)
{
this.land = land;
}
public void setMilitary(Military military)
{
this.military = military;
}
public Military getMilitary()
{
return military;
}
}
Military.java
package com.moffett.grunzke.server.civilization;
import java.util.ArrayList;
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;
#PersistenceCapable
public class Military
{
#PrimaryKey
#Persistent(valueStrategy = IdGeneratorStrategy.IDENTITY)
private Key key;
#Persistent
private ArrayList<Army> armies = new ArrayList<Army>();
public Key getKey()
{
return key;
}
public void setKey(Key key)
{
this.key = key;
}
public ArrayList<Army> getArmies()
{
return armies;
}
public void setArmies(ArrayList<Army> armies)
{
this.armies = armies;
}
public void addArmy(Army army)
{
this.armies.add(army);
}
}
Army.java
package com.moffett.grunzke.server.civilization;
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;
#PersistenceCapable
public class Army
{
#PrimaryKey
#Persistent(valueStrategy = IdGeneratorStrategy.IDENTITY)
private Key key;
#Persistent
private int meleeUnits;
#Persistent
private int rangedUnits;
#Persistent
private int mountedUnits;
public Key getKey()
{
return key;
}
public void setKey(Key key)
{
this.key = key;
}
public int getMeleeUnits()
{
return meleeUnits;
}
public void setMeleeUnits(int meleeUnits)
{
this.meleeUnits = meleeUnits;
}
public int getRangedUnits()
{
return rangedUnits;
}
public void setRangedUnits(int rangeUnits)
{
this.rangedUnits = rangeUnits;
}
public int getMountedUnits()
{
return mountedUnits;
}
public void setMountedUnits(int mountedUnits)
{
this.mountedUnits = mountedUnits;
}
}
My guess is the problem is with the way you are setting the setter methods that take a List are implemented. Remember that JDO will replace the ArrayList fields with persistance-aware versions, so you don't want to change the fields. Try this:
#PersistenceCapable
public class Military {
#PrimaryKey
#Persistent(valueStrategy = IdGeneratorStrategy.IDENTITY)
private Key key;
#Persistent
private final List <Army> armies = new ArrayList<Army>();
public void setArmies(List<Army> armies) {
this.armies.clear();
this.armies.addAll(armies);
}
This is a good idea for other reasons as well. You don't want someone doing this:
military.setArmies(armies);
armies.clear();
...or this:
military.getArmies().clear();
Personally, I would have the methods that change your entities expose only the operations you want:
public void addArmy(Army army) {
armies.add(army);
}
public List<Army> getArmies() {
return Collections.unmodifiableList(armies);
}

Save gwt entities to google application engine datastore with jdo, using rpc

Hello iam new to GWT framework. I want to persist my domain objects/entities to google application engine datastore using rpc. A simple implementation to test if i can make multiple rpc calls ( greetServer() , saveStudent() )
Student
import javax.jdo.annotations.Extension;
import javax.jdo.annotations.IdGeneratorStrategy;
import javax.jdo.annotations.PersistenceCapable;
import javax.jdo.annotations.Persistent;
import javax.jdo.annotations.PrimaryKey;
import com.google.gwt.user.client.rpc.IsSerializable;
#PersistenceCapable
public class Student implements IsSerializable {
private static final long serialVersionUID = 1L;
#PrimaryKey
#Persistent(valueStrategy = IdGeneratorStrategy.IDENTITY)
#Extension(vendorName = "datanucleus", key = "gae.encoded-pk", value = "true")
private int studentId;
#Persistent private String firstName;
#Persistent private String lastName;
public Student(){}
public Student(String firstName, String lastName){
this.firstName = firstName;
this.lastName = lastName;
}
public void setStudentId(int studentId) {
this.studentId = studentId;
}
public int getStudentId() {
return studentId;
}
public void setFirstName(String firstName) {
this.firstName = firstName;
}
public String getFirstName() {
return firstName;
}
public void setLastName(String lastName) {
this.lastName = lastName;
}
public String getLastName() {
return lastName;
}
}
GreetingService (default code generated by Eclipse IDE)
import com.google.gwt.user.client.rpc.RemoteService;
import com.google.gwt.user.client.rpc.RemoteServiceRelativePath;
#RemoteServiceRelativePath("greet")
public interface GreetingService extends RemoteService {
String greetServer(String name) throws IllegalArgumentException;
**String saveStudent(Student s) throws IllegalArgumentException;**
}
GreetingServiceAsync
import com.google.gwt.user.client.rpc.AsyncCallback;
public interface GreetingServiceAsync {
void greetServer(String input, AsyncCallback<String> callback)
throws IllegalArgumentException;
**void saveStudent(Student s, AsyncCallback<String> callback)
throws IllegalArgumentException;**
}
GreetingServiceImpl
import javax.jdo.PersistenceManager;
import com.d.client.GreetingService;
import com.d.client.Student;
import com.d.shared.FieldVerifier;
import com.google.gwt.user.server.rpc.RemoteServiceServlet;
#SuppressWarnings("serial")
public class GreetingServiceImpl extends RemoteServiceServlet implements
GreetingService {
public String greetServer(String input) throws IllegalArgumentException
...
String serverInfo = getServletContext().getServerInfo();
String userAgent = getThreadLocalRequest().getHeader("User-Agent");
...
}
#Override
public String saveStudent(Student s) throws IllegalArgumentException {
PersistenceManager pm = PMF.get().getPersistenceManager();
pm.makePersistent(s);
return "student save - ok";
}
}
PMF
import javax.jdo.JDOHelper;
import javax.jdo.PersistenceManagerFactory;
public final class PMF {
private static final PersistenceManagerFactory pmfInstance = JDOHelper
.getPersistenceManagerFactory("transactions-optional");
private PMF() {
}
public static PersistenceManagerFactory get() {
return pmfInstance;
}
}
EntryPoint
...
private final GreetingServiceAsync greetingService = GWT
.create(GreetingService.class);
greetingService.greetServer("greet",
new AsyncCallback<String>() {
public void onFailure(Throwable caught) {
// Show the RPC error message to the user
}
public void onSuccess(String result) {
//Show success message
}
});
greetingService.saveStudent(new Student("kostas","trichas"),
new AsyncCallback<String>() {
public void onFailure(Throwable caught) {
// Show the RPC error message to the user
}
public void onSuccess(String result) {
//Show success message
}
});
...
Is the above implementation correct? I deployed this sample application to gae and it did not persisted the object student (you can browse the entities at gae datastore viewer)
check it please:
http://gwtgaedatastore.appspot.com
Change your int studentID to Long id to get it working
This works with your original code (ie., Long id):
#Extension (vendorName="jpox", key="key-auto-increment" ,value="true")
Or, change id to String and your orig code works.
I could not get Long PK to work with datanucleus using gae.pk-id.

Resources