sf logo

sf.qof
Class QueryObjectFactory

java.lang.Object
  extended by sf.qof.QueryObjectFactory

public final class QueryObjectFactory
extends Object

Used to create query object implementations from definition interfaces or classes. It only provides static methods and can not be instantiated.

Definition interfaces or classes define abstract methods that are decorated with annotations to specify the SQL query and mappings to primitive types or Java beans.

     public class Person {
         int id;
         String name;
         
         public int getId() {
             return id;
         }
         public void setId(int id) {
             this.id = id;
         }
         ...
     }
     
     public interface PersonQueries extends BaseQuery {
         @Query(sql = "select id {%%.id}, name {%%.name} from person where id = {%1}")
         Person getPerson(int id);
         ...
     }
     
     Connection connection = ... // get the database connection from somewhere
     PersonQueries personQueries = QueryObjectFactory.createQueryObject(PersonQueries.class);
     personQueries.setConnection(connection);
     Person person = personQueries.getPerson(123);
 

Generated query object classes implement BaseQuery to set the connection etc.

Generated query object classes are cached for each ClassLoader. The generation process is thread safe i.e. if two threads are trying to create the same query object implementation one will wait till the generation of the class completes and then just instantiate an object while the other thread is generating the class.

The generation process can be customized by using a Customizer

See Also:
BaseQuery, Query, Insert, Update, Delete, Call

Method Summary
static
<T> T
createQueryObject(Class<T> queryDefinitionClass)
          Creates a query object class defined by a query definition and returns a new instance.
static
<T> T
createQueryObject(Class<T> queryDefinitionClass, Object... parameters)
          Creates a query object class defined by a query definition and returns a new instance.
static
<T,S> T
createQueryObjectFromSuperClass(Class<T> queryDefinitionClass, Class<S> superClass)
          Creates a query object class defined by a query definition and a super class and returns a new instance.
static
<T,S> T
createQueryObjectFromSuperClass(Class<T> queryDefinitionClass, Class<S> superClass, Object... parameters)
          Creates a query object class defined by a query definition and a super class and returns a new instance.
static void registerMapper(String type, MappingAdapter adapter)
          Register a custom mapping adapter in the mapping registry.
static void setCustomizer(Customizer cust)
          Sets the Customizer for the code generation.
static void setDefaultCustomizer()
          Resets the Customizer for the code generation to the default customizer.
static void setSQLDialect(SQLDialect dialect)
          Sets the SQL dialect.
static void unregisterMapper(String type)
          Unregister a custom mapping adapter from the mapping registry.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Method Detail

createQueryObject

public static <T> T createQueryObject(Class<T> queryDefinitionClass)
Creates a query object class defined by a query definition and returns a new instance.

If the query definition is a class then the generated query object class will be a subclass of this class. The query definition class can be abstract or concrete but is not allowed to be final. All constructors of the query definition class will be implemented by the query object class but only the default constructor will be used to instantiate the query object.

If the query definition is an interface then the superclass will be Object.

Parameters:
queryDefinitionClass - query definition class or interface
Returns:
an instance that implements the abstract definitions defined in queryDefinitionClass
Since:
1.0
See Also:
createQueryObject(Class, Object...), createQueryObjectFromSuperClass(Class, Class), createQueryObjectFromSuperClass(Class, Class, Object...)

createQueryObject

public static <T> T createQueryObject(Class<T> queryDefinitionClass,
                                      Object... parameters)
Creates a query object class defined by a query definition and returns a new instance.

The generated query object class is a subclass of query definition class. The query definition class can be abstract or concrete but is not allowed to be final. All constructors of the query definition class will be implemented by the query object class and parameters will be used to find a matching constructor to instantiate the query object with these parameters. The matching process works on the type of the parameters and will match the first constructor that matches all parameter type.

A single null parameter should be passed to a constructor as: new Class[] {null}.

Parameters:
queryDefinitionClass - query definition class
parameters - parameters to be used in the constructor
Returns:
an instance that implements the abstract definitions defined in queryDefinitionClass
Since:
1.0
See Also:
createQueryObject(Class), createQueryObjectFromSuperClass(Class, Class), createQueryObjectFromSuperClass(Class, Class, Object...)

createQueryObjectFromSuperClass

public static <T,S> T createQueryObjectFromSuperClass(Class<T> queryDefinitionClass,
                                                      Class<S> superClass)
Creates a query object class defined by a query definition and a super class and returns a new instance.

The generated query object class will be a subclass of superClass which can be abstract or concrete but is not allowed to be final. All constructors of superClass will be implemented by the query object class but only the default constructor will be used to instantiate the query object.

queryDefinitionClass must be an interface.

Parameters:
queryDefinitionClass - query definition interface
superClass - the class the query object class will inherit from
Returns:
an instance that implements the abstract definitions defined in queryDefinitionClass
Since:
1.0
See Also:
createQueryObject(Class), createQueryObject(Class, Object...), createQueryObjectFromSuperClass(Class, Class, Object...)

createQueryObjectFromSuperClass

public static <T,S> T createQueryObjectFromSuperClass(Class<T> queryDefinitionClass,
                                                      Class<S> superClass,
                                                      Object... parameters)
Creates a query object class defined by a query definition and a super class and returns a new instance.

The generated query object class will be a subclass of superClass which can be abstract or concrete but is not allowed to be final. All constructors of superClass will be implemented by the query object class and parameters will be used to find a matching constructor to instantiate the query object with these parameters. The matching process works on the type of the parameters and will match the first constructor that matches all parameter type.

A single null parameter should be passed to a constructor as: new Class[] {null}.

queryDefinitionClass must be an interface.

Parameters:
queryDefinitionClass - query definition interface
superClass - the class the query object class will inherit from
parameters - parameters to be used in the constructor
Returns:
an instance that implements the abstract definitions defined in queryDefinitionClass
Since:
1.0
See Also:
createQueryObject(Class), createQueryObject(Class, Object...), createQueryObjectFromSuperClass(Class, Class)

setCustomizer

public static void setCustomizer(Customizer cust)
Sets the Customizer for the code generation.

Parameters:
cust - a customizer
See Also:
Customizer

setDefaultCustomizer

public static void setDefaultCustomizer()
Resets the Customizer for the code generation to the default customizer.

See Also:
Customizer, DefaultCustomizer

registerMapper

public static void registerMapper(String type,
                                  MappingAdapter adapter)
Register a custom mapping adapter in the mapping registry.

This method can be used to register custom mapping adapters.

Parameters:
type - mapping type name
adapter - mapping adapter
See Also:
GeneratorMappingAdapter, DynamicMappingAdapter, MappingAdapter

unregisterMapper

public static void unregisterMapper(String type)
Unregister a custom mapping adapter from the mapping registry.

Parameters:
type - mapping type name

setSQLDialect

public static void setSQLDialect(SQLDialect dialect)
Sets the SQL dialect.

Parameters:
dialect - SQL dialect
See Also:
SQLDialect

sf logo

Copyright © 2007 brunella ltd. All Rights Reserved.