Java and databases hand in hand

Modern systems fail not because of lack of technology, but because of lack of shared understanding. SQLComments is an open approach that allows Java developers and database administrators to communicate intent, context, and ownership directly inside SQL—where it matters most.

Code Generation

Generic create, read, update and delete operations are ready out of the box. The 'domain' Maven plugin is used to generate domain classes and CRUD statements.

Multi-DB support

By using SQL comments, your application can support multiple databases. Furthermore, it is straightforward to configure different SQL statements for specific database engines and take advantage of their non-standard functions.

Native SQL Statements

SQL statements are used in their native form. There is no need to rewrite an SQL statement in a Java-specific domain language. You can copy and paste them into your favourite database editor and tune the statement as required.

Type safety

The exact result and configuration POJO classes, which contain properties with the appropriate Java types, are generated from the SQL statements. The statement is checked for Java compliance and against the database schema.

A good example is worth a thousand words.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
public List<CompanyInfo> findCompanies(CompanyFilter filter) {
    /*-
      @SQLComment(name="findCompanies", resultClass="example.CompanyInfo", configClass="example.FindCompaniesConfig")
      SELECT comp.id, comp.name, country.name 
      FROM companies comp JOIN countries country ON comp.countryId = country.id
           JOIN employees emp ON comp.id = emp.companyId  -- Add join only if //@ ceo != null
      WHERE 1 = 1
           AND comp.id = :companyId                       -- Rows containing placeholders will only be used
           AND comp.name LIKE :name                       -- in a query if all placeholders are not null.
           AND emp.name LIKE :ceo
      ORDER BY
           country.name                                   -- Only if //@ order == 'country'
           comp.name ASC, country.name DESC               -- Only if //@ order == 'company'
     */
 
    // Set statement parameters     FindCompaniesConfig config = new FindCompaniesConfig();     config.setCompanyId(filter.getCompanyId());  
    // Configure paging if needed
        config.limit(20);
        config.offset(0);

        // Load data     List<CompanyInfo> companies = list(config, CompanyInfoMapper.INSTANCE);
    return companies; }

SQL statements in their native form, as used in database tools, can be placed in separate files or inserted directly into Java code as comments. The SQL Comments Maven plugin parses Java code and generates input and output classes, including transformers from JDBC ResultSet.

The generation of SQL statements at runtime can be controlled by JavaScript expressions at the end of a line, which are treated as SQL comments. There are two simple conditions that must be met for a row to be added to the statement:/p>

  1. all placeholders must have a non-null value (unless configured otherwise) and
  2. the JavaScript expression must return the boolean value 'true'.

You can find out more in the Quick Start Guide, or view the full Documentation and Tutorials.