public void testUnencodedLongPk_BatchGet() throws EntityNotFoundException {
    switchDatasource(PersistenceManagerFactoryName.nontransactional);
    HasLongPkJDO pojo = new HasLongPkJDO();
    beginTxn();
    pojo.setId(1L);
    pm.makePersistent(pojo);
    commitTxn();

    HasLongPkJDO pojo2 = new HasLongPkJDO();
    beginTxn();
    pojo2.setId(2L);
    pm.makePersistent(pojo2);
    commitTxn();

    assertNotNull(pojo.getId());
    assertNotNull(pojo2.getId());

    beginTxn();
    Query q = pm.newQuery("select from " + HasLongPkJDO.class.getName() + " where id == :ids");
    List<HasLongPkJDO> pojos =
        (List<HasLongPkJDO>) q.execute(Utils.newArrayList(pojo.getId(), pojo2.getId()));
    assertEquals(2, pojos.size());
    // we should preserve order but right now we don't
    Set<Long> pks = Utils.newHashSet(pojos.get(0).getId(), pojos.get(1).getId());
    assertEquals(pks, Utils.newHashSet(1L, 2L));
    commitTxn();
  }
  public void testSerializeWithMultiValueProps() throws Exception {
    pm.setDetachAllOnCommit(true);
    beginTxn();
    DetachableWithMultiValuePropsJDO pojo = new DetachableWithMultiValuePropsJDO();
    pojo.setStrList(Utils.newArrayList("c", "d"));
    pm.makePersistent(pojo);
    commitTxn();
    assertEquals(ObjectState.DETACHED_CLEAN, JDOHelper.getObjectState(pojo));
    pm.close();
    assertEquals(ObjectState.DETACHED_CLEAN, JDOHelper.getObjectState(pojo));
    pm = pmf.getPersistenceManager();
    assertEquals(ObjectState.DETACHED_CLEAN, JDOHelper.getObjectState(pojo));

    pojo = toBytesAndBack(pojo);

    assertEquals(Utils.newArrayList("c", "d"), pojo.getStrList());
    assertEquals(ObjectState.DETACHED_CLEAN, JDOHelper.getObjectState(pojo));
    beginTxn();
    assertEquals(ObjectState.DETACHED_CLEAN, JDOHelper.getObjectState(pojo));
    // reattach to the pm - this turns our regular list field into a managed
    // list field
    pojo = pm.makePersistent(pojo);
    assertEquals(ObjectState.PERSISTENT_CLEAN, JDOHelper.getObjectState(pojo));
    pojo.getStrList().add("e");
    commitTxn();
    Entity e =
        ds.get(
            KeyFactory.createKey(
                DetachableWithMultiValuePropsJDO.class.getSimpleName(), pojo.getId()));
    assertEquals(3, ((List<String>) e.getProperty("strList")).size());
    assertEquals(Utils.newArrayList("c", "d", "e"), e.getProperty("strList"));
  }
 public void testNonDefaultStorageVersion() {
   em.close();
   emf.close();
   Map<String, String> props = Utils.newHashMap();
   props.put(
       StorageVersion.STORAGE_VERSION_PROPERTY,
       StorageVersion.PARENTS_DO_NOT_REFER_TO_CHILDREN.name());
   emf = Persistence.createEntityManagerFactory(getEntityManagerFactoryName().name(), props);
   em = emf.createEntityManager();
   DatastoreManager storeMgr = (DatastoreManager) getExecutionContext().getStoreManager();
   assertEquals(StorageVersion.PARENTS_DO_NOT_REFER_TO_CHILDREN, storeMgr.getStorageVersion());
 }
 public void testUnknownStorageVersion() {
   em.close();
   emf.close();
   Map<String, String> props = Utils.newHashMap();
   props.put(StorageVersion.STORAGE_VERSION_PROPERTY, "does not exist");
   try {
     emf = Persistence.createEntityManagerFactory(getEntityManagerFactoryName().name(), props);
   } catch (Exception e) {
     // not all Persistence impls wrap EMF creation exception into PersistenceException
     Throwable cause = (e instanceof PersistenceException) ? e.getCause() : e;
     // good
     assertTrue(cause.getMessage().startsWith("'does not exist'"));
   }
 }
  public void testEncodedStringPk_BatchGet() throws EntityNotFoundException {
    switchDatasource(PersistenceManagerFactoryName.nontransactional);
    HasEncodedStringPkJDO pojo = new HasEncodedStringPkJDO();
    beginTxn();
    String key1 =
        new KeyFactory.Builder("parent", 44)
            .addChild(HasEncodedStringPkJDO.class.getSimpleName(), "yar1")
            .getString();
    pojo.setId(key1);
    pm.makePersistent(pojo);
    commitTxn();

    HasEncodedStringPkJDO pojo2 = new HasEncodedStringPkJDO();
    beginTxn();
    String key2 =
        new KeyFactory.Builder("parent", 44)
            .addChild(HasEncodedStringPkJDO.class.getSimpleName(), "yar2")
            .getString();
    pojo2.setId(key2);
    pm.makePersistent(pojo2);
    commitTxn();

    assertNotNull(pojo.getId());
    assertNotNull(pojo2.getId());

    beginTxn();
    Query q =
        pm.newQuery("select from " + HasEncodedStringPkJDO.class.getName() + " where id == :ids");
    List<HasEncodedStringPkJDO> pojos =
        (List<HasEncodedStringPkJDO>) q.execute(Utils.newArrayList(pojo.getId(), pojo2.getId()));
    assertEquals(2, pojos.size());
    // we should preserve order but right now we don't
    Set<String> pks = Utils.newHashSet(pojos.get(0).getId(), pojos.get(1).getId());
    assertEquals(pks, Utils.newHashSet(key1, key2));
    commitTxn();
  }
/** @author Max Ross <*****@*****.**> */
@PersistenceCapable(detachable = "true")
public class HasOneToManyUnencodedStringPkSetJDO implements HasOneToManyUnencodedStringPkJDO {

  @PrimaryKey private String key;

  @Element(dependent = "true")
  private Set<Flight> flights = Utils.newHashSet();

  @Persistent(mappedBy = "parent")
  @Element(dependent = "true")
  private Set<BidirectionalChildUnencodedStringPkSetJDO> bidirChildren =
      new HashSet<BidirectionalChildUnencodedStringPkSetJDO>();

  public void addFlight(Flight flight) {
    flights.add(flight);
  }

  public Collection<Flight> getFlights() {
    return flights;
  }

  public String getId() {
    return key;
  }

  public void setId(String key) {
    this.key = key;
  }

  public void addBidirChild(BidirectionalChildUnencodedStringPkJDO child) {
    bidirChildren.add((BidirectionalChildUnencodedStringPkSetJDO) child);
  }

  public Collection<BidirectionalChildUnencodedStringPkJDO> getBidirChildren() {
    return (Set) bidirChildren;
  }

  public boolean removeFlights(Collection<Flight> flights) {
    return this.flights.removeAll(flights);
  }

  public boolean removeBidirChildren(
      Collection<BidirectionalChildUnencodedStringPkJDO> bidirChildren) {
    return this.bidirChildren.removeAll(bidirChildren);
  }
}
/** @author Max Ross <*****@*****.**> */
@PersistenceCapable(detachable = "true")
public class HasOneToManyKeyPkListJDO implements HasOneToManyKeyPkJDO {

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

  @Element(dependent = "true")
  @Order(column = "flights_INTEGER_IDX_keypk")
  private List<Flight> flights = Utils.newArrayList();

  public void addFlight(Flight flight) {
    flights.add(flight);
  }

  public Collection<Flight> getFlights() {
    return flights;
  }

  public Key getId() {
    return key;
  }
}