Last active
November 17, 2018 13:07
-
-
Save alejandrogr/8606207 to your computer and use it in GitHub Desktop.
Deal with list of Refs in objectify
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package com.alejandrogr.gists.ofylistref; | |
import com.googlecode.objectify.Ref; | |
import com.googlecode.objectify.annotation.Entity; | |
import com.googlecode.objectify.annotation.Id; | |
import com.googlecode.objectify.annotation.Ignore; | |
import com.googlecode.objectify.annotation.Load; | |
import com.googlecode.objectify.annotation.OnLoad; | |
import java.util.ArrayList; | |
import java.util.List; | |
@Entity | |
class Car { | |
@Ignore private List<Person> passengers; | |
@Ignore private Person driver; | |
@Id private Long id; | |
@Load private transient Ref<Person> driverRef; // Person is an @Entity | |
@Load(Person.Passenger.class) private transient List<Ref<Person>> passengersRefs; | |
public Car(){ | |
passengersRefs = new ArrayList<Ref<Person>>(); | |
} | |
@OnLoad | |
public void deRef() { | |
if (passengersRefs != null) { | |
passengers = new ArrayList<Person>(); | |
for (Ref<Person> passengerLoaded : passengersRefs) { | |
if (passengerLoaded.isLoaded()) { | |
passengers.add(passengerLoaded.get()); | |
} | |
} | |
} | |
if( driverRef != null ){ | |
driver = driverRef.get(); | |
} | |
} | |
public void addPassenger(Person passenger) { | |
Ref<Person> ref = Ref.create(passenger.getKey()); | |
if (!passengersRefs.contains(ref)) { | |
passengersRefs.add(ref); | |
} | |
} | |
public Person getDriver() { return driverRef.get(); } | |
public void setDriver(Person p_person) { driverRef = Ref.create(p_person.getKey()); } | |
public List<Ref<Person>> getPassengersRefs() { return passengersRefs; } | |
public void setPassengersRefs(List<Ref<Person>> p_passengersRefs) { passengersRefs = p_passengersRefs; } | |
public List<Person> getPassengers() {return passengers;} | |
public void setPassengers(List<Person> p_passengers) {passengers = p_passengers;} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package com.alejandrogr.gists.ofylistref; | |
import junit.framework.TestCase; | |
import org.junit.After; | |
import org.junit.Before; | |
import org.junit.Test; | |
import com.google.appengine.tools.development.testing.LocalBlobstoreServiceTestConfig; | |
import com.google.appengine.tools.development.testing.LocalDatastoreServiceTestConfig; | |
import com.google.appengine.tools.development.testing.LocalServiceTestHelper; | |
import com.googlecode.objectify.Key; | |
import com.googlecode.objectify.ObjectifyService; | |
import static com.googlecode.objectify.ObjectifyService.ofy; | |
import static junit.framework.Assert.assertEquals; | |
import static junit.framework.Assert.assertNotNull; | |
public class OfyListRefTest extends TestCase{ | |
private final LocalServiceTestHelper helper = new LocalServiceTestHelper( | |
new LocalDatastoreServiceTestConfig().setDefaultHighRepJobPolicyUnappliedJobPercentage(1), | |
new LocalBlobstoreServiceTestConfig()); | |
@Override | |
@Before | |
public void setUp() { | |
this.helper.setUp(); | |
ObjectifyService.register(Person.class); | |
ObjectifyService.register(Car.class); | |
} | |
@Override | |
@After | |
public void tearDown() { | |
this.helper.tearDown(); | |
} | |
@Test | |
public void testCreateCar(){ | |
Person driver = new Person(); | |
driver.setEmail("[email protected]"); | |
driver.setName("Driver"); | |
Key<Person> driverKey = ofy().save().entity(driver).now(); | |
assertNotNull(driverKey); | |
Car car = new Car(); | |
car.setDriver(driver); | |
Key<Car> carKey = ofy().save().entity(car).now(); | |
assertNotNull(carKey); | |
} | |
@Test | |
public void testCreateCarWithPassengers(){ | |
Person driver = new Person(); | |
driver.setEmail("[email protected]"); | |
driver.setName("Driver"); | |
Key<Person> driverKey = ofy().save().entity(driver).now(); | |
Person passenger = new Person(); | |
passenger.setEmail("[email protected]"); | |
passenger.setName("Passenger"); | |
ofy().save().entity(passenger).now(); | |
Person passenger2 = new Person(); | |
passenger2.setEmail("[email protected]"); | |
passenger2.setName("Passenger 2"); | |
ofy().save().entity(passenger2).now(); | |
Person passenger3 = new Person(); | |
passenger3.setEmail("[email protected]"); | |
passenger3.setName("Passenger 3"); | |
ofy().save().entity(passenger3).now(); | |
Car car = new Car(); | |
car.setDriver(driver); | |
car.addPassenger(passenger); | |
car.addPassenger(passenger2); | |
car.addPassenger(passenger3); | |
Key<Car> carKey = ofy().save().entity(car).now(); | |
//clear ofy cache to ensure new objetc from DS | |
ofy().clear(); | |
//check load without group | |
Car carSaved = ofy().load().key( carKey ).now(); | |
assertNotNull(carSaved); | |
assertEquals("The car dosen't have 3 passengers", 3, carSaved.getPassengersRefs().size()); | |
assertEquals("The passengers are loaded", 0, carSaved.getPassengers().size()); | |
//clear ofy cache to ensure new objetc from DS | |
ofy().clear(); | |
//check load with group | |
carSaved = ofy().load().group(Person.Passenger.class).key( carKey ).now(); | |
assertNotNull(carSaved); | |
assertEquals("There are no passengers loaded", 3, carSaved.getPassengers().size()); | |
assertNotNull("Passenger email is empty", carSaved.getPassengers().get(1).getEmail()); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package com.alejandrogr.gists.ofylistref; | |
import com.googlecode.objectify.Key; | |
import com.googlecode.objectify.annotation.Entity; | |
import com.googlecode.objectify.annotation.Id; | |
@Entity | |
public class Person { | |
@Id private String email; | |
private String name; | |
public Key<Person> getKey() { | |
return Key.create(Person.class, email); | |
} | |
public String getEmail() { | |
return email; | |
} | |
public void setEmail(String email) { | |
this.email = email; | |
} | |
public String getName() { | |
return name; | |
} | |
public void setName(String name) { | |
this.name = name; | |
} | |
//empty class just for load group passenger | |
public class Passenger{} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
When I tried with these two methods:
public List<Ref> getPassengersRefs() { return passengersRefs; }
public void setPassengersRefs(List<Ref> p_passengersRefs) { passengersRefs = p_passengersRefs; }
I am getting this error:
Error:Execution failed for task ':backend:appengineEndpointsGetClientLibs'.
There was an error running endpoints command get-client-lib: Parameterized type com.googlecode.objectify.Ref'<'com.alejandrogr.gists.ofylistref.Person> not supported.
Any idea?