GAE. Как сделать запрос из свойства childs, используя JDO? - PullRequest
1 голос
/ 22 января 2012

Основано на странице GAE JDO (http://code.google.com/appengine/docs/java/datastore/jdo/relationships.html), Я создаю объекты Employee и ContactInfo с отношением один ко многим.

public class Employee implements Serializable{
    @Persistent(mappedBy = "employee", defaultFetchGroup = "true")
    @Element(dependent = "true")
    private List<ContactInfo> contactInfoSets;
...

public class ContactInfo implements Serializable{
    @Persistent
    private String streetAddress;
...

Я пытаюсь выполнить приведенный ниже код, всегда приводящий к ошибке (java.lang.AssertionError:ожидается: <1>, но было: <0>) потому что, но возвращаемое значение равно 0.

String statement = "SELECT FROM " + Employee.class.getName() 
    + " WHERE contactInfoSets.contains(i) && i.streetAddress=='myaddress'"
    + " VARIABLES " + ContactInfo.class.getName() + " i ";
javax.jdo.Query query = pm.newQuery(statement);
List<ContactInfo> companyProfiles = (List<ContactInfo>) query.execute();

Спасибо ...

Здесь код

[Employee.java]

import java.io.Serializable;
import java.util.List;

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.Element;

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

@SuppressWarnings("serial")
@PersistenceCapable(identityType = IdentityType.APPLICATION, detachable = "true")
public class Employee implements Serializable{

    @PrimaryKey
    @Persistent(valueStrategy = IdGeneratorStrategy.IDENTITY)
    private Key key;

    @Persistent(mappedBy = "employee", defaultFetchGroup = "true")
    @Element(dependent = "true")
    private List<ContactInfo> contactInfoSets;

    public List<ContactInfo> getContactInfoSets() {
        return contactInfoSets;
    }
    public void setContactInfoSets(List<ContactInfo> contactInfoSets) {
        this.contactInfoSets = contactInfoSets;
    }
    public Key getKey() {
        return key;
    }
    public void setKey(Key key) {
        this.key = key;
    }
}

[ContactInfo.java]

import java.io.Serializable;

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 com.google.appengine.api.datastore.Key;

@SuppressWarnings("serial")
@PersistenceCapable(identityType = IdentityType.APPLICATION, detachable = "true")
public class ContactInfo implements Serializable{

    @PrimaryKey
    @Persistent(valueStrategy = IdGeneratorStrategy.IDENTITY)
    private Key key;


    @Persistent
    private String streetAddress;

    @Persistent
    private Employee employee;      

    public Key getKey() {
        return key;
    }

    public void setKey(Key key) {
        this.key = key;
    }   

    public String getStreetAddress() {
        return streetAddress;
    }

    public void setStreetAddress(String streetAddress) {
        this.streetAddress = streetAddress;
    }

    public Employee getEmployee() {
        return employee;
    }

    public void setEmployee(Employee employee) {
        this.employee = employee;
    }   
}

[TestJdoCollection.java]

import static com.google.appengine.api.datastore.FetchOptions.Builder.withLimit;
import static org.junit.Assert.assertEquals;

import java.util.ArrayList;
import java.util.List;

import javax.jdo.PersistenceManager;
import javax.jdo.PersistenceManagerFactory;

import org.junit.After;
import org.junit.Before;
import org.junit.Test;

import com.cea.common.service.CeaServiceFactory;
import com.cea.common.service.PersistenceService;
import com.google.appengine.api.datastore.DatastoreService;
import com.google.appengine.api.datastore.DatastoreServiceFactory;
import com.google.appengine.api.datastore.Query;
import com.google.appengine.tools.development.testing.LocalDatastoreServiceTestConfig;
import com.google.appengine.tools.development.testing.LocalServiceTestHelper;

public class TestJdoCollection {
private final LocalServiceTestHelper helper = new LocalServiceTestHelper(
        new LocalDatastoreServiceTestConfig());   

    private DatastoreService ds = DatastoreServiceFactory.getDatastoreService();

    @Before
    public void setUp() {
        helper.setUp();
    }

    @After
    public void tearDown() {
        helper.tearDown();
    }

    @Test   
    public void test(){
        CeaServiceFactory ceaServiceFactory = CeaServiceFactory.getInstance();
        PersistenceService servicePersistance = (PersistenceService) ceaServiceFactory.getCeaService(CeaServiceFactory.SERVICE_PERSISTANCE);        
        PersistenceManagerFactory pmf = servicePersistance.getPersistenceManagerFactory();
        PersistenceManager pm = pmf.getPersistenceManager();

        ContactInfo contactInfo = new ContactInfo();
        contactInfo.setStreetAddress("myaddress");

        Employee employee  = new Employee();
        List<ContactInfo> contactInfoSets = new ArrayList<ContactInfo>();
        contactInfoSets.add(contactInfo);
        employee.setContactInfoSets(contactInfoSets);       

        pm.makePersistent(employee);

        assertEquals(1, ds.prepare(new Query("Employee")).countEntities(withLimit(10)));
        assertEquals(1, ds.prepare(new Query("ContactInfo")).countEntities(withLimit(10)));

        String statement = "SELECT FROM " + Employee.class.getName() + " WHERE contactInfoSets.contains(i) && i.streetAddress=='myaddress'"
                + " VARIABLES " + ContactInfo.class.getName() + " i ";
        javax.jdo.Query query = pm.newQuery(statement);
        List<ContactInfo> companyProfiles = (List<ContactInfo>) query.execute();        
        assertEquals(1, companyProfiles.size());

    }
}
...