use of org.apache.cayenne.map.ObjAttribute in project cayenne by apache.
the class SingleClassGenerationTest method testContainsPropertyImport.
@Test
public void testContainsPropertyImport() throws Exception {
ObjEntity objEntity = new ObjEntity("TEST1");
ObjAttribute attr = new ObjAttribute("attr");
ObjRelationship rel = new ObjRelationship("rel");
objEntity.addAttribute(attr);
objEntity.addRelationship(rel);
VelocityContext context = new VelocityContext();
context.put(Artifact.OBJECT_KEY, objEntity);
String res = renderTemplate(ClassGenerationAction.SINGLE_CLASS_TEMPLATE, context);
assertTrue(res.contains("org.apache.cayenne.exp.Property"));
}
use of org.apache.cayenne.map.ObjAttribute in project cayenne by apache.
the class SuperClassGenerationTest method testContainsPropertyImportForAttributes.
@Test
public void testContainsPropertyImportForAttributes() throws Exception {
ObjEntity objEntity = new ObjEntity("TEST1");
ObjAttribute attr = new ObjAttribute("attr");
objEntity.addAttribute(attr);
VelocityContext context = new VelocityContext();
context.put(Artifact.OBJECT_KEY, objEntity);
String res = renderTemplate(ClassGenerationAction.SUPERCLASS_TEMPLATE, context);
assertTrue(res.contains("org.apache.cayenne.exp.Property"));
}
use of org.apache.cayenne.map.ObjAttribute in project cayenne by apache.
the class JointPrefetchIT method testJointPrefetchDataTypes.
/**
* Tests that joined entities can have non-standard type mappings.
*/
@Test
public void testJointPrefetchDataTypes() {
// prepare... can't load from XML, as it doesn't yet support dates..
SQLTemplate artistSQL = new SQLTemplate(Artist.class, "insert into ARTIST (ARTIST_ID, ARTIST_NAME, DATE_OF_BIRTH) " + "values (33001, 'a1', #bind($date 'DATE'))");
artistSQL.setParams(Collections.singletonMap("date", new Date(System.currentTimeMillis())));
SQLTemplate paintingSQL = new SQLTemplate(Painting.class, "INSERT INTO PAINTING (PAINTING_ID, PAINTING_TITLE, ARTIST_ID, ESTIMATED_PRICE) " + "VALUES (33001, 'p1', 33001, 1000)");
context.performNonSelectingQuery(artistSQL);
context.performNonSelectingQuery(paintingSQL);
// test
SelectQuery<Painting> q = new SelectQuery<>(Painting.class);
q.addPrefetch(Painting.TO_ARTIST.joint());
ObjEntity artistE = context.getEntityResolver().getObjEntity("Artist");
ObjAttribute dateOfBirth = artistE.getAttribute("dateOfBirth");
assertEquals("java.util.Date", dateOfBirth.getType());
dateOfBirth.setType("java.sql.Date");
try {
final List<Painting> objects = q.select(context);
queryInterceptor.runWithQueriesBlocked(() -> {
assertEquals(1, objects.size());
for (Painting p : objects) {
Artist a = p.getToArtist();
assertNotNull(a);
assertNotNull(a.getDateOfBirth());
assertTrue(a.getDateOfBirth().getClass().getName(), Date.class.isAssignableFrom(a.getDateOfBirth().getClass()));
}
});
} finally {
dateOfBirth.setType("java.util.Date");
}
}
use of org.apache.cayenne.map.ObjAttribute in project cayenne by apache.
the class Compiler method compileEntityResultWithPrefetch.
private EntityResult compileEntityResultWithPrefetch(EntityResult compiledResult, EJBQLExpression prefetchExpression) {
final EntityResult result = compiledResult;
String id = prefetchExpression.getText().toLowerCase();
ClassDescriptor descriptor = descriptorsById.get(id);
if (descriptor == null) {
descriptor = descriptorsById.get(prefetchExpression.getText());
}
final String prefix = prefetchExpression.getText().substring(prefetchExpression.getText().indexOf(".") + 1);
final Set<String> visited = new HashSet<String>();
PropertyVisitor visitor = new PropertyVisitor() {
public boolean visitAttribute(AttributeProperty property) {
ObjAttribute oa = property.getAttribute();
if (visited.add(oa.getDbAttributePath())) {
result.addObjectField(oa.getEntity().getName(), "fetch." + prefix + "." + oa.getName(), prefix + "." + oa.getDbAttributeName());
}
return true;
}
public boolean visitToMany(ToManyProperty property) {
return true;
}
public boolean visitToOne(ToOneProperty property) {
ObjRelationship rel = property.getRelationship();
DbRelationship dbRel = rel.getDbRelationships().get(0);
for (DbJoin join : dbRel.getJoins()) {
DbAttribute src = join.getSource();
if (src.isForeignKey() && visited.add(src.getName())) {
result.addDbField("fetch." + prefix + "." + src.getName(), prefix + "." + src.getName());
}
}
return true;
}
};
descriptor.visitAllProperties(visitor);
// append id columns ... (some may have been appended already via relationships)
for (String pkName : descriptor.getEntity().getPrimaryKeyNames()) {
if (visited.add(pkName)) {
result.addDbField("fetch." + prefix + "." + pkName, prefix + "." + pkName);
}
}
// append inheritance discriminator columns...
for (ObjAttribute column : descriptor.getDiscriminatorColumns()) {
if (visited.add(column.getName())) {
result.addDbField("fetch." + prefix + "." + column.getDbAttributePath(), prefix + "." + column.getDbAttributePath());
}
}
return result;
}
use of org.apache.cayenne.map.ObjAttribute in project cayenne by apache.
the class Compiler method compileEntityResult.
private EntityResult compileEntityResult(EJBQLExpression expression, int position) {
String id = expression.getText().toLowerCase();
ClassDescriptor descriptor = descriptorsById.get(id);
if (descriptor == null) {
descriptor = descriptorsById.get(expression.getText());
}
if (descriptor == null) {
throw new EJBQLException("the entity variable '" + id + "' does not refer to any entity in the FROM clause");
}
final EntityResult entityResult = new EntityResult(descriptor.getObjectClass());
final String prefix = "ec" + position + "_";
final int[] index = { 0 };
final Set<String> visited = new HashSet<String>();
PropertyVisitor visitor = new PropertyVisitor() {
public boolean visitAttribute(AttributeProperty property) {
ObjAttribute oa = property.getAttribute();
if (visited.add(oa.getDbAttributePath())) {
entityResult.addObjectField(oa.getEntity().getName(), oa.getName(), prefix + index[0]++);
}
return true;
}
public boolean visitToMany(ToManyProperty property) {
return true;
}
public boolean visitToOne(ToOneProperty property) {
ObjRelationship rel = property.getRelationship();
DbRelationship dbRel = rel.getDbRelationships().get(0);
for (DbJoin join : dbRel.getJoins()) {
DbAttribute src = join.getSource();
if (src.isForeignKey() && visited.add(src.getName())) {
entityResult.addDbField(src.getName(), prefix + index[0]++);
}
}
return true;
}
};
descriptor.visitAllProperties(visitor);
// append id columns ... (some may have been appended already via relationships)
for (String pkName : descriptor.getEntity().getPrimaryKeyNames()) {
if (visited.add(pkName)) {
entityResult.addDbField(pkName, prefix + index[0]++);
}
}
// append inheritance discriminator columns...
for (ObjAttribute column : descriptor.getDiscriminatorColumns()) {
if (visited.add(column.getName())) {
entityResult.addDbField(column.getDbAttributePath(), prefix + index[0]++);
}
}
return entityResult;
}
Aggregations