@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 Selectannotation.
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 { @Select("select name, description, age from mammal") DataSet<Mammal> getAllMammals(); @Update("delete from mammal") int deleteAllMammals(); }To invoke the 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();
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.
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
CachedRowSet
and any modifications to the DataSet are
not
propogated to the data source until the DataSet.sync method is called.
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 { @Select(sql="select * from Mammal", allColumnsMapped=true) DataSet<Mammal> getAllMammals(); @Select(sql="select * from Mammal", allColumnsMapped=true) DataSet<MammalFail> getAllMammals2(); }The Mammal table contains the following columns and data:
----------------------------------------------------------- |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.
public abstract boolean readOnly
public abstract boolean connected
public abstract boolean allColumnsMapped
public abstract String value
Note : If the sql and the value annotation element are specified at the same time, a SQLRuntimeException will be thrown.
public abstract boolean scrollable
If the DataSet is disconnected, the scrollable annotation element is ignored.
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.