@Documented @Target(value=METHOD) @Retention(value=RUNTIME) public @interface Select
Annotation used to decorate a method in a Query interface with a SQL statement
that can return a result set from the underlying data store. The
Select
annotation must not be used in-conjunction with a SQL statement
that returns an update count.
A Query interface may contain zero, one or more methods decorated with an Select
annotation.
The following provides an example of using the Select
annotation.
// Define DataSet type public class Mammal { public String name; public String description; public int age; } // Define the interface containing the methods representing SQL statements // that can be invoked. interface MyQueries extends BaseQuery {To invoke the@Select
("select name, description, age from mammal") DataSet<Mammal> getAllMammals();@
Update("delete from mammal") int deleteAllMammals(); }
getAllMammals()
method, an instance of
the MyQueries
interface must be created by invoking either the
Connnection.createQueryObject
or DataSource.createQueryObject
method.
MyQueries mq = con.createQueryObject(MyQueries.class); Dataset<Mammal> rows = mq.getAllMammals();
sql
annotation element allows developers to specify parameter markers
similar to PreparedStatements
. Parameter markers are defined as:
Select
annotation. If this occurs, a SQLRuntimeException
will be thrown.
sql
annotation element.
interface MyQueries extends BaseQuery {
@Select
(sql="select name, description from mammal where weight > ?1")
DataSet<Mammal> getBigMammals(int weight);
}
MyQueries mq = con.createQueryObject(MyQueries.class); DataSet<Mammal> rows = mq.getBigMammals(200);
When the getBigMammals()
method is invoked, the value specified
for the parameter weight will be used as the value for the parameter
marker.
Select
annotation returns a
DataSet
instance that is connected to the underlying data store.
The returned DataSet
is treated similar to a
ResultSet
object. Any modifications to the DataSet
are
propagated to the data source if the DataSet
is updatable.
If the connected
annotation element is set to false
, the returned
DataSet
instance is considered to be disconnected. The DataSet
is then treated similar to a
CachedRowSet
and any modifications to the DataSet
are not propogated to the data source until the DataSet.sync
method is called.
allColumnsMapped
annotation element is used to specify whether
all of the columns that are returned by a method decorated by a
Select
annotation must be present in the data class for a
DataSet
. The returned columns are mapped by matching their column names
to the field names in the data class for a DataSet
.
If the allColumnsMapped
annotation element is set to false
,
then any columns returned by a method decorated by a Select
annotation
that are not contained in the data class will be ignored.
Consider the following example:
public class Mammal { public String firstName; public String lastName; public String address; public String description; } public class MammalFail { public String name; public String description; public int age; } interface Queries {The Mammal table contains the following columns and data:@Select
(sql="select * from Mammal", allColumnsMapped=true) DataSet<Mammal> getAllMammals();@Select
(sql="select * from Mammal", allColumnsMapped=true) DataSet<MammalFail> getAllMammals2(); }
----------------------------------------------------------- |firstName | lastName | address | description | =========================================================== | homer | simpson | Springfield | Average Joe | ----------------------------------------------------------- | fred | flintsone | San Francisco, CA | stoneage human | ----------------------------------------------------------- | wombat | jones | Aspen, CO | wombat | ============================================================The call to the
getAllMammals
method will succeed because the data class
Mammal contains a field for every column in the result set that is returned by the
query specified in the sql
annotation element.
The call to the getAllMammals2
method will result in a SQLRuntimeException
being thrown because the data class MammalFail does not contain a field
for every column in the result set that is returned by the query specified in
the sql
annotation element. The fields firstName, lastName and address
would need to be added to the data class MammalFail in order for
the call to the getAllMammals2
method to succeed.
Optional Element Summary | |
---|---|
boolean |
allColumnsMapped
Determines whether all of the columns that are returned as part of a result set for the SQL command specified in the sql annotation
element, are required to be present in the data class for a DataSet . |
boolean |
connected
Determines whether the returned DataSet is connected to the
underlying data store. |
boolean |
readOnly
Determines if the returned DataSet is updatable. |
boolean |
scrollable
Determines whether a connected DataSet is scrollable. |
String |
sql
The SQL command to execute. |
String |
tableName
Specifies the name of the table from where this data is to be written back to. |
String |
value
The SQL command to execute. |
public abstract String sql
Note: If the sql
and the value
annotation
element are specified at the same time, a SQLRuntimeException
will
be thrown.
String
representation of the SQL command.public abstract boolean readOnly
DataSet
is updatable.
true
if the returned DataSet
cannot be modified;
false
otherwiseDataSet.isReadOnly()
public abstract boolean connected
DataSet
is connected to the
underlying data store.
true
if the returned DataSet
is connected
to the underlying data store; false
otherwise.DataSet.isConnected()
public abstract boolean allColumnsMapped
sql
annotation
element, are required to be present in the data class for a DataSet
.
false
if the data class for a DataSet
does not require a field for each column returned in result set; true
if there must be a field present in the data class for every
column returned in a result set.public abstract String value
Note: If the sql
and the value
annotation
element are specified at the same time, a SQLRuntimeException
will
be thrown.
String
representation of the SQL command.public abstract boolean scrollable
DataSet
is scrollable.
If the DataSet
is scrollable, it has a ResultSet
type of TYPE_SCROLL_INSENSITVE
. If the DataSet
is
not scrollable, the ResultSet
type is specified to be
TYPE_FORWARD_ONLY
.
If the DataSet
is disconnected, the scrollable
annotation element is ignored.
true
if the connected DataSet
is scrollable;
false
otherwise.DataSet.isScrollable()
public abstract String tableName
This annotation element is required when the DataSet
is in disconnected mode and the
contents of the DataSet
can be written back to the database.
Note: If the connected
annotation element is set
to false
, the readOnly
annotation element
is set to false
and the tableName
annotation
element is not specified, a SQLRuntimeException
will
be thrown.