Based on GAE JDO page 
(http://code.google.com/appengine/docs/java/datastore/jdo/relationships.html), 
I create Employee and ContactInfo objects with relation one to many.

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

I try to exec below code always give me error (java.lang.AssertionError: 
expected:<1> but was:<0>) because but the return value is 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();

Thanks...  
        

Here the code

**[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());
            
        }
    }


-- 
You received this message because you are subscribed to the Google Groups 
"Google App Engine for Java" group.
To view this discussion on the web visit 
https://groups.google.com/d/msg/google-appengine-java/-/zfc6OWtei6UJ.
To post to this group, send email to [email protected].
To unsubscribe from this group, send email to 
[email protected].
For more options, visit this group at 
http://groups.google.com/group/google-appengine-java?hl=en.

Reply via email to