Database access - Data Access
By default, database queries in RapidClipse are performed in the Data Access layer (Project Management > Data Access). This separates database queries from UI, entities, and business logic. In the Data Access layer, a corresponding Data Access Object (DAO) is provided for each entity (by default when creating and importing entities), which forms the interface to the database. The database query itself is done using a user-defined method that is called on the DAO, submits the database query, and then returns the query result. The DAO already provides some standard methods.
Standard query methods
-
Query all records related to the using DAO object - .findAll();.
List<Customer> allCustomers = new CustomerDAO().findAll(); for (Customer customer : allCustomers) { ... }
-
Query a specific object using the primary key - .find(Object primaryKey).
Customer specificCustomer = new CustomerDAO().find("SHG"); String contactname = specificCustomer.getContactname();
-
Returns a list of objects matching the given example - .findByExample(Class entity, Object example)
CAUTION: Also default values like 0 or "" (empty string) are included in the check, so all values should be preset with *zeroCustomer exampleCustomer = new Customer(); exampleCustomer.setCity("Musterhausen"); exampleCustomer.setContacttitle("Herr"); List<Customer> findByExample = new CustomerDAO().findByExample(Customer.class, exampleCustomer);
-
Manually abort the transaction with subsequent RollBack - .rollback().
new CustomerDAO().rollback();
-
Save created entities - .save(Object entity)
Customer exampleCustomer = new Customer(); exampleCustomer.setCity("Musterhausen"); exampleCustomer.setContacttitle("Herr"); Customer savedCustomer = new CustomerDAO().save(exampleCustomer);
or
Customer exampleCustomer2 = new Customer(); exampleCustomer2.setCity("Musterhausen2"); Customer exampleCustomer3 = new Customer(); exampleCustomer3.setCity("Musterhausen3"); Customer exampleCustomer4 = new Customer(); exampleCustomer4.setCity("Musterhausen4"); Customer savedCustomer = new CustomerDAO().save(exampleCustomer2, exampleCustomer3, exampleCustomer4);