Author: fdesbois Date: 2010-06-02 14:45:32 +0200 (Wed, 02 Jun 2010) New Revision: 1988 Url: http://nuiton.org/repositories/revision/topia/1988 Log: Add join support in TopiaQuery 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 10:30:27 UTC (rev 1987) +++ trunk/topia-persistence/src/main/java/org/nuiton/topia/framework/TopiaQuery.java 2010-06-02 12:45:32 UTC (rev 1988) @@ -234,10 +234,12 @@ private static final Log log = LogFactory.getLog(TopiaQuery.class); - public static final String FROM_SEPARATOR_DEFAULT = ", "; + public static final String FROM_SEPARATOR_DEFAULT = ","; - public static final String FROM_SEPARATOR_JOIN = " JOIN "; + public static final String FROM_SEPARATOR_JOIN = "JOIN"; + public static final String FROM_SEPARATOR_LEFT_JOIN = "LEFT JOIN"; + /** Params for HQL query * */ protected List<Object> params; @@ -408,40 +410,80 @@ * * @param str the element to add * @return the TopiaQuery - * @see #addFrom(String, String) - * @deprecated since 2.4 use {@link #addFrom(String, String)} + * @see #addFrom(Class, String) + * @see #addJoin(String, String) + * @see #addLeftJoin(String, String) + * @deprecated since 2.4 use correct addFrom or addJoin or addLeftJoin */ @Deprecated public TopiaQuery addFrom(String str) { - return addFrom(str, null); + return addFrom(FROM_SEPARATOR_DEFAULT, str, null); } /** * Add an element to the from in the query. Used to add some other data in * the query or for join as specific {@code separator}. * - * @param str the element to add + * @param property the property to add * @param separator The separator to use before adding the element (if null * the {@link #FROM_SEPARATOR_DEFAULT} will be used). * @return the TopiaQuery * @since 2.4 */ - public TopiaQuery addFrom(String str, String separator) { - if (separator == null) { - separator = FROM_SEPARATOR_DEFAULT; + protected TopiaQuery addFrom(String separator, String property, String alias) { + if (!separator.equals(FROM_SEPARATOR_DEFAULT)) { + from.append(' '); } - from.append(separator).append(str); + from.append(separator).append(' ').append(property); + + if (alias != null) { + from.append(' ').append(alias); + } return this; } /** + * 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 + * 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 + * @return the TopiaQuery + * @since 2.4 + */ + public TopiaQuery addJoin(String property, String alias) { + return addFrom(FROM_SEPARATOR_JOIN, property, alias); + } + + + /** + * 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 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 + * @return the TopiaQuery + * @since 2.4 + */ + public TopiaQuery addLeftJoin(String property, String alias) { + return addFrom(FROM_SEPARATOR_LEFT_JOIN, property, alias); + } + + /** * Add an other entity type to the from in the query. * * @param entityClass different from the mainEntity * @return the TopiaQuery */ public TopiaQuery addFrom(Class<? extends TopiaEntity> entityClass) { - return addFrom(entityClass.getName(), null); + return addFrom(entityClass, null); } /** @@ -453,7 +495,7 @@ */ public TopiaQuery addFrom(Class<? extends TopiaEntity> entityClass, String alias) { - return addFrom(entityClass.getName() + " " + alias, null); + return addFrom(FROM_SEPARATOR_DEFAULT, entityClass.getName(), alias); } /**