use of org.apache.cayenne.Persistent in project cayenne by apache.
the class DataContextPrefetchMultistepIT method testToManyToManyFirstStepUnresolved.
@Test
public void testToManyToManyFirstStepUnresolved() throws Exception {
createTwoArtistsWithExhibitsDataSet();
// since objects for the phantom prefetches are not retained explicitly, they may
// get garbage collected, and we won't be able to detect them
// so ensure ObjectStore uses a regular map just for this test
context.getObjectStore().objectMap = new HashMap<Object, Persistent>();
// Check the target ArtistExhibit objects do not exist yet
Map<String, Object> id1 = new HashMap<>();
id1.put("ARTIST_ID", 11);
id1.put("EXHIBIT_ID", 2);
ObjectId oid1 = new ObjectId("ArtistExhibit", id1);
Map<String, Object> id2 = new HashMap<>();
id2.put("ARTIST_ID", 101);
id2.put("EXHIBIT_ID", 2);
ObjectId oid2 = new ObjectId("ArtistExhibit", id2);
assertNull(context.getGraphManager().getNode(oid1));
assertNull(context.getGraphManager().getNode(oid2));
Expression e = ExpressionFactory.exp("galleryName = $name");
SelectQuery<Gallery> q = SelectQuery.query(Gallery.class, e.params(Collections.singletonMap("name", "gallery2")));
q.addPrefetch("exhibitArray.artistExhibitArray");
List<Gallery> galleries = context.select(q);
assertEquals(1, galleries.size());
Gallery g2 = galleries.get(0);
// this relationship wasn't explicitly prefetched....
Object list = g2.readPropertyDirectly("exhibitArray");
assertTrue(list instanceof Fault);
// however the target objects must be resolved
ArtistExhibit ae1 = (ArtistExhibit) context.getGraphManager().getNode(oid1);
ArtistExhibit ae2 = (ArtistExhibit) context.getGraphManager().getNode(oid2);
assertNotNull(ae1);
assertNotNull(ae2);
assertEquals(PersistenceState.COMMITTED, ae1.getPersistenceState());
assertEquals(PersistenceState.COMMITTED, ae2.getPersistenceState());
}
use of org.apache.cayenne.Persistent in project cayenne by apache.
the class BaseQueryMetadata method resolve.
boolean resolve(Object root, EntityResolver resolver) {
if (lastRoot != root || lastEntityResolver != resolver) {
this.classDescriptor = null;
this.dbEntity = null;
this.dataMap = null;
ObjEntity entity = null;
if (root != null) {
if (root instanceof Class<?>) {
entity = resolver.getObjEntity((Class<?>) root, true);
if (entity != null) {
this.dbEntity = entity.getDbEntity();
this.dataMap = entity.getDataMap();
}
} else if (root instanceof ObjEntity) {
entity = (ObjEntity) root;
this.dbEntity = entity.getDbEntity();
this.dataMap = entity.getDataMap();
} else if (root instanceof String) {
entity = resolver.getObjEntity((String) root);
if (entity != null) {
this.dbEntity = entity.getDbEntity();
this.dataMap = entity.getDataMap();
}
} else if (root instanceof DbEntity) {
this.dbEntity = (DbEntity) root;
this.dataMap = dbEntity.getDataMap();
} else if (root instanceof DataMap) {
this.dataMap = (DataMap) root;
} else if (root instanceof Persistent) {
entity = resolver.getObjEntity((Persistent) root);
if (entity != null) {
this.dbEntity = entity.getDbEntity();
this.dataMap = entity.getDataMap();
}
}
}
if (entity != null) {
this.classDescriptor = resolver.getClassDescriptor(entity.getName());
}
this.lastRoot = root;
this.lastEntityResolver = resolver;
return true;
}
return false;
}
use of org.apache.cayenne.Persistent in project cayenne by apache.
the class IdMapKeyAccessor method getValue.
public Object getValue(Object object) throws PropertyException {
if (object instanceof Persistent) {
ObjectId id = ((Persistent) object).getObjectId();
if (id.isTemporary()) {
return id;
}
Map<?, ?> map = id.getIdSnapshot();
if (map.size() == 1) {
Map.Entry<?, ?> pkEntry = map.entrySet().iterator().next();
return pkEntry.getValue();
}
return id;
} else {
throw new IllegalArgumentException("Object must be Persistent: " + object);
}
}
use of org.apache.cayenne.Persistent in project cayenne by apache.
the class SimpleAttributeProperty method resolveFault.
protected void resolveFault(Object object) throws PropertyException {
try {
Persistent persistent = (Persistent) object;
ObjectContext context = persistent.getObjectContext();
if (context != null) {
context.prepareForAccess(persistent, getName(), false);
}
} catch (ClassCastException e) {
throw new PropertyException("Object is not a Persistent: '" + object.getClass().getName() + "'", this, object, e);
}
}
use of org.apache.cayenne.Persistent in project cayenne by apache.
the class DataContextEJBQLIsNullIT method testIsNotNull.
@Test
public void testIsNotNull() throws Exception {
createTwoPaintings();
String ejbql1 = "SELECT p FROM Painting p WHERE p.estimatedPrice IS NOT NULL";
EJBQLQuery query1 = new EJBQLQuery(ejbql1);
List<?> results = context.performQuery(query1);
assertEquals(1, results.size());
assertEquals(33002, Cayenne.intPKForObject((Persistent) results.get(0)));
}
Aggregations