Author: fdesbois Date: 2010-06-02 17:07:56 +0200 (Wed, 02 Jun 2010) New Revision: 1989 Url: http://nuiton.org/repositories/revision/topia/1989 Log: Add fetch managment (not fully tested) Modified: trunk/topia-persistence/src/main/java/org/nuiton/topia/framework/TopiaQuery.java Modified: trunk/topia-persistence/src/main/java/org/nuiton/topia/framework/TopiaQuery.java =================================================================== --- trunk/topia-persistence/src/main/java/org/nuiton/topia/framework/TopiaQuery.java 2010-06-02 12:45:32 UTC (rev 1988) +++ trunk/topia-persistence/src/main/java/org/nuiton/topia/framework/TopiaQuery.java 2010-06-02 15:07:56 UTC (rev 1989) @@ -411,8 +411,8 @@ * @param str the element to add * @return the TopiaQuery * @see #addFrom(Class, String) - * @see #addJoin(String, String) - * @see #addLeftJoin(String, String) + * @see #addJoin(String, String, boolean) + * @see #addLeftJoin(String, String, boolean) * @deprecated since 2.4 use correct addFrom or addJoin or addLeftJoin */ @Deprecated @@ -445,37 +445,50 @@ /** * Add a inner join {@code property} to the query with {@code alias}. * The join is done in From statement as : FROM Contact C JOIN C.boat B. The - * added part is 'JOIN C.boat B' using addJoin("C.boat", "B"). The order + * added part is 'JOIN C.boat B' using addJoin("C.boat", "B", false). The order * of calling {@link #addFrom(Class, String)} or this method is very important. * The first element in the FROM is always the main entity of the query. * * @param property Property name to use as a Join * @param alias Alias of the property in the query + * @param fetch Add FETCH keyword to load the property in result (avoid + * lazy initialization) * @return the TopiaQuery * @since 2.4 */ - public TopiaQuery addJoin(String property, String alias) { - return addFrom(FROM_SEPARATOR_JOIN, property, alias); + public TopiaQuery addJoin(String property, String alias, boolean fetch) { + return addFromJoin(FROM_SEPARATOR_JOIN, property, alias, fetch); } /** * Add a left join {@code property} to the query with {@code alias}. * The join is done in From statement as : FROM Contact C LEFT JOIN C.boat B. - * The added part is 'LEFT JOIN C.boat B' using addJoin("C.boat", "B"). + * The added part is 'LEFT JOIN C.boat B' using addJoin("C.boat", "B", false). * The order of calling {@link #addFrom(Class, String)} or this method is * very important. The first element in the FROM is always the main entity * of the query. * * @param property Property name to use as a Join * @param alias Alias of the property in the query + * @param fetch Add FETCH keyword to load the property in result (avoid + * lazy initialization) * @return the TopiaQuery * @since 2.4 */ - public TopiaQuery addLeftJoin(String property, String alias) { - return addFrom(FROM_SEPARATOR_LEFT_JOIN, property, alias); + public TopiaQuery addLeftJoin(String property, String alias, boolean fetch) { + return addFromJoin(FROM_SEPARATOR_LEFT_JOIN, property, alias, fetch); } + protected TopiaQuery addFromJoin(String separator, String property, + String alias, boolean fetch) { + String sep = separator; + if (fetch) { + sep = new StringBuilder(separator).append("FETCH ").toString(); + } + return addFrom(separator, property, alias); + } + /** * Add an other entity type to the from in the query. * @@ -599,6 +612,21 @@ return this; } + public TopiaQuery addFetch(String... properties) { + if (StringUtils.isEmpty(mainAlias)) { + mainAlias = RandomStringUtils.randomAlphabetic(4); + from.append(' ').append(mainAlias); + if (select == null) { + setSelect(mainAlias); + } + } + for (String property : properties) { + String propertyWithAlias = getProperty(mainAlias, property); + addLeftJoin(propertyWithAlias, null, true); + } + return this; + } + protected List<String> getPropertiesToLoad() { if (propertiesToLoad == null) { propertiesToLoad = new ArrayList<String>(); @@ -816,7 +844,10 @@ public TopiaQuery addNotNull(String paramName) { StringBuilder result = new StringBuilder(paramName).append(' ').append(Op.NOT_NULL); - return addWhere(result.toString()); + parentheses = false; + addWhere(result.toString()); + parentheses = true; + return this; } /**