![]() |
| |||||||||||||||||||||||||||||||||
Find this tutorial in: /opt/resin/webapps/resin-doc/amber/tutorial/find
Try the Tutorial Amber extends SQL to support relations, and queries the database uses the JDBC ResultSet API.
Most applications need to query the database to find entities with special properties. Amber's query language currently follows Hibernate's query language (this may change in the future depending on JDO 2.0.). The query language resembles SQL resembles SQL with a few restrictions and extensions appropriate to handling entities beans. For example, the QL extends SQL with direct support for relations. In the following example, the school's headmaster needs to know which courses are being taught and needs to find the course taught by a named teacher.
The example uses the same database table as the previous basic example.
The findAll query finds all the courses in the database.
The entity's class name replaces the usual SQL table. The mapping of entities to tables is saved in the configuration files (the Course.hbm.xml), to simply portability. The usual JDBC parameter syntax, '?', is allowed as well.
The example servlet uses two AmberConnection methods, query and prepareQuery. query returns a result set. prepareQuery prepares a query like the JDBC PreparedStatement.
findByInstructor returns the course taught by an instructor. Single-valued finder methods expect to return exactly one bean. If there are no matching courses, findByInstructor throws an ObjectNotFoundException. If more than one courses are taught by the instructor, it will throw a FinderException.
| ||||||||||||||||||||||||||||||||||