public void testEncodedStringPk_NonKeyValue() throws EntityNotFoundException { HasEncodedStringPkJDO pojo = new HasEncodedStringPkJDO(); pojo.setId("yar"); beginTxn(); try { pm.makePersistent(pojo); fail("expected exception"); } catch (JDOFatalUserException e) { // good rollbackTxn(); } }
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(); }
public void testEncodedStringPk() throws EntityNotFoundException { HasEncodedStringPkJDO pojo = new HasEncodedStringPkJDO(); Key key = KeyFactory.createKey(HasEncodedStringPkJDO.class.getSimpleName(), "a name"); pojo.setId(KeyFactory.keyToString(key)); beginTxn(); pm.makePersistent(pojo); commitTxn(); assertEquals(KeyFactory.keyToString(key), pojo.getId()); Entity e = ds.get(KeyFactory.stringToKey(pojo.getId())); beginTxn(); pm.getObjectById(HasEncodedStringPkJDO.class, e.getKey().getName()); pm.getObjectById(HasEncodedStringPkJDO.class, e.getKey()); pm.getObjectById(HasEncodedStringPkJDO.class, KeyFactory.keyToString(e.getKey())); commitTxn(); }