/** * Create a Bean populating projection for the given type and bindings * * @param <T> type of projection * @param type type of the projection * @param bindings property bindings * @return factory expression */ public static <T> QBean<T> bean(Class<? extends T> type, Map<String, ? extends Expression<?>> bindings) { return new QBean<T>(type, bindings); }
@Test public void with_class_and_map_using_fields() { Map<String,Expression<?>> bindings = new LinkedHashMap<String,Expression<?>>(); bindings.put("name", name); bindings.put("age", age); bindings.put("married", married); QBean<Entity> beanProjection = new QBean<Entity>(Entity.class, true, bindings); Entity bean = beanProjection.newInstance("Fritz", 30, true); assertEquals("Fritz", bean.name); assertEquals(30, bean.age); assertEquals(true, bean.married); }
@Override public Beans newInstance(Object... args) { int offset = 0; Map<RelationalPath<?>, Object> beans = new HashMap<RelationalPath<?>, Object>(); for (Map.Entry<RelationalPath<?>, QBean<?>> entry : qBeans.entrySet()) { RelationalPath<?> path = entry.getKey(); QBean<?> qBean = entry.getValue(); int argsSize = qBean.getArgs().size(); Object[] subArgs = ArrayUtils.subarray(args, offset, offset + argsSize); beans.put(path, qBean.newInstance(subArgs)); offset += argsSize; } return new Beans(beans); }
@Test public void fieldsClassOfTExpressionOfQArray() { PathBuilder<Entity> entity = new PathBuilder<Entity>(Entity.class, "entity"); QBean<Entity> beanProjection = Projections.fields(Entity.class, entity.getNumber("cId", Integer.class), entity.getNumber("eId", Integer.class)); assertEquals(Entity.class, beanProjection.newInstance(1, 2).getClass()); }
@Test public void oneToOneToMany_projection_as_bean() { List<User> results = USERS_W_LATEST_POST_AND_COMMENTS.transform( groupBy(userName).list(Projections.bean(User.class, userName, Projections.bean(Post.class, postId, postName, set(qComment).as("comments")).as("latestPost")))); assertEquals(2, results.size()); User user = results.get(0); Post post = user.getLatestPost(); assertEquals(toInt(2), post.getId()); assertEquals("post 2", post.getName()); assertEquals(toSet(comment(4), comment(5)), post.getComments()); }
/** * Create a new QBean instance * * @param type type of bean * @param fieldAccess true, for field access and false, for property access * @param args fields or properties to be populated */ protected QBean(Class<? extends T> type, boolean fieldAccess, Expression<?>... args) { this(type, fieldAccess, createBindings(args)); }
@Test public void with_class_and_exprs_using_fields() { QBean<Entity> beanProjection = new QBean<Entity>(Entity.class, true, name, age, married); Entity bean = beanProjection.newInstance("Fritz", 30, true); assertEquals("Fritz", bean.name); assertEquals(30, bean.age); assertEquals(true, bean.married); }
public Iterable<T> iterable( Iterator<Map<String, Object>> result ) { List<T> entities = new ArrayList<T>(); while (result.hasNext()) { Map<String, Object> row = result.next(); Object[] args = new Object[row.size()]; int idx = 0; for ( Expression<?> expression : bean.getArgs() ) { args[idx++] = row.get( ((Path) expression).getMetadata().getElement().toString() ); } entities.add( bean.newInstance( args ) ); } return entities; } }
@Test public void beanClassOfTExpressionOfQArray() { PathBuilder<Entity> entity = new PathBuilder<Entity>(Entity.class, "entity"); QBean<Entity> beanProjection = Projections.bean(Entity.class, entity.getNumber("cId", Integer.class), entity.getNumber("eId", Integer.class)); assertEquals(Entity.class, beanProjection.newInstance(1, 2).getClass()); }
@Test public void oneToOneToMany_projection_as_bean() { Map<String, User> results = USERS_W_LATEST_POST_AND_COMMENTS.transform( groupBy(userName).as(Projections.bean(User.class, userName, Projections.bean(Post.class, postId, postName, set(qComment).as("comments")).as("latestPost")))); assertEquals(2, results.size()); User user = results.get("Jane"); Post post = user.getLatestPost(); assertEquals(toInt(2), post.getId()); assertEquals("post 2", post.getName()); assertEquals(toSet(comment(4), comment(5)), post.getComments()); }
/** * Create a new QBean instance * * @param type type of bean * @param fieldAccess true, for field access and false, for property access * @param args fields or properties to be populated */ protected QBean(Class<? extends T> type, boolean fieldAccess, Expression<?>... args) { this(type, fieldAccess, createBindings(args)); }
@Test public void with_class_and_map() { Map<String,Expression<?>> bindings = new LinkedHashMap<String,Expression<?>>(); bindings.put("name", name); bindings.put("age", age); bindings.put("married", married); QBean<Entity> beanProjection = new QBean<Entity>(Entity.class, bindings); Entity bean = beanProjection.newInstance("Fritz", 30, true); assertEquals("Fritz", bean.getName()); assertEquals(30, bean.getAge()); assertEquals(true, bean.isMarried()); }
@Test public void with_path_and_exprs_using_fields() { QBean<Entity> beanProjection = Projections.fields(entity, name, age, married); Entity bean = beanProjection.newInstance("Fritz", 30, true); assertEquals("Fritz", bean.name); assertEquals(30, bean.age); assertEquals(true, bean.married); }
/** * Create a field access based Bean populating projection for the given type and bindings * * @param <T> type of projection * @param type type of the projection * @param bindings field bindings * @return factory expression */ public static <T> QBean<T> fields(Class<? extends T> type, Map<String, ? extends Expression<?>> bindings) { return new QBean<T>(type, true, bindings); }