Skip Headers

# Expressions Compared to SQL

Previous

Next

Expressions offer the following advantages over SQL when you access a database:

- Expressions are easier to maintain because the database is abstracted.

- Changes to descriptors or database tables do not affect the querying structures in the application.

- Expressions enhance readability by standardizing the `Query` interface so that it looks similar to traditional Java calling conventions. For example, the Java code required to get the street name from the `Address` object of the `Employee` class looks like this:

      emp.getAddress().getStreet().equals("Meadowlands");

  The expression to get the same information is similar:

      emp.get("address").get("street").equal("Meadowlands");

- Expressions allow read queries to transparently query between two classes that share a relationship. If these classes are stored in multiple tables in the database, TopLink automatically generates the appropriate join statements to return information from both tables.

- Expressions simplify complex operations. For example, the following Java code retrieves all employees that live on "Meadowlands" whose salary is greater than 10,000:

      ExpressionBuilder emp = new ExpressionBuilder();
      Expression exp = emp.get("address").get("street").equal("Meadowlands");
      Vector employees = session.readAllObjects(Employee.class,
        exp.and(emp.get("salary").greaterThan(10000)));

  TopLink automatically generates the appropriate SQL from that code:

      SELECT t0.VERSION, t0.ADDR_ID, t0.F_NAME, t0.EMP_ID, t0.L_NAME, t0.MANAGER_ID, t0.END_DATE, t0.START_DATE, t0.GENDER, t0.START_TIME, t0.END_TIME,t0.SALARY FROM EMPLOYEE t0, ADDRESS t1 WHERE (((t1.STREET = 'Meadowlands')AND (t0.SALARY > 10000)) AND (t1.ADDRESS_ID = t0.ADDR_ID))