Skip to content

Query from table

Don Peter edited this page Oct 19, 2017 · 2 revisions

Find functions

The primary key in ONAM for all tables are maintained internally (auto generated, incrementing), you will be able to access them using getId() method, but not overwrite or alter its value.

Methods to fetch data from database. All methods are available in Entity class.

Find by ID

In-order to find a row by its id,

   User user = User.find(User.class, 1);

This will return null, if the row doesn't exist.

Check if a row exists/ Retrieve a single row.

   User user = User.findByUniqueProperty(User.class, "name", "John Doe");

This will return a single row if it exists with value "John Doe" in column "name". Custom column names may be specified for tables using @Column(name = "") annotation. See migration docs on how to implement the same.

Find by property

To find all rows with value "John Doe" in column "name" without any limit.

   List<User> users = User.findByProperty(User.class, "name", "John Doe");

or, Use page index and page size parameters. For example, to return results 11-60 (where result 1 is the first row), use:

   List<User> users = User.findByProperty(User.class, "name", "John Doe", 10, 50);

or, Use just limit parameter. For example, to return first 10 results, use:

   List<User> users = User.findByProperty(User.class, "name", "John Doe", 10);

Find All

To get all rows without any limit.

   List<User> users = User.findAll(User.class);

or, Use page index and page size parameters. For example, to return results 11-60 (where result 1 is the first row), use:

   List<User> users = User.findByProperty(User.class, 10, 50);

or, Use just limit parameter. For example, to return first 10 results, use:

   List<User> users = User.findByProperty(User.class, 10);

WHERE Clause

Incase, you need to specify a custom where clause.

Use where clause, use:

   String nameOfUser = "John Doe";
   String whereClause = "name == " + nameOfUser;
   List<User> users = User.findByProperty(User.class, whereClause);

or, Use page index and page size parameters. For example, to return results 11-60 (where result 1 is the first row), use:

   List<User> users = User.findByProperty(User.class, whereClause, 10, 50);

or, Use just limit parameter. For example, to return first 10 results, use:

   List<User> users = User.findByProperty(User.class, whereClause, 10);
Clone this wiki locally