Apache Empire-db
| Company type | Open source software project |
|---|---|
| Industry | Software development, Database management |
| Founded | January 2008 |
| Founder | ESTEAM Software |
| Headquarters | Germany |
| Owner | Apache Software Foundation |
| Website | empire-db |
Apache Empire-db is a Java-based library that provides a sophisticated object-oriented interface for interacting with relational database management systems (RDBMS) through JDBC. It is an open-source project licensed under the Apache License 2.0 and maintained by the Apache Software Foundation.
In contrast to Object-relational mapping (ORM) frameworks like Hibernate, iBATIS, or TopLink, Empire-db does not depend on XML configuration files or Java annotations to associate plain Java objects (POJOs) with database tables and columns. Instead, it employs a Java-based object model to represent the data schema and provides an API that primarily manipulates object references rather than relying on string literals.
The primary goal of Empire-db is to improve software robustness and maintainability by enhancing compile-time type safety and minimizing duplication of metadata. Additionally, it can offer performance advantages by allowing developers complete control over the construction and execution of SQL statements, distinguishing it from many traditional ORM solutions.
Major benefits
Empire-db’s key strength is its dynamic SQL generation interface for performing select, update, insert, and delete operations via Java methods that reference model objects. This design guarantees type safety while significantly reducing the reliance on string literals for identifiers or expressions. The framework supports DBMS independence through a modular driver architecture.
Using references to table and column objects enhances compile-time validation, which decreases the necessity for extensive testing. Moreover, this approach facilitates IDE code completion features to explore the data model, thereby increasing developer efficiency without requiring extra tools or plugins.
The object model also grants secure access to metadata such as data types, field lengths, mandatory constraints, and predefined value sets. This metadata can be extended beyond native DBMS properties, promoting the creation of reusable generic code and eliminating redundancy across different application layers.
Features at a glance
- The data model is represented entirely by Java objects, removing the need for XML schemas or annotations and allowing straightforward user customization.
- Cross-platform record management compatible with various databases including Oracle, Microsoft SQL Server, MySQL, Derby, H2, and HSQLDB (starting from version 2.0.5).
- Capability to generate DDL scripts for target DBMS directly from object definitions, applicable to whole databases or individual components like tables and views.
- A type-safe API that facilitates building dynamic SQL commands using method calls and object references instead of string literals, improving testability and maintainability.
- Reduced Java code footprint through dynamic beans that support runtime modifications of the data model (DDL) along with advanced interception of field and metadata access.
- Built-in tracking of record states and changes ("dirty checking") ensures that only modified fields are updated.
- Support for optimistic locking mechanisms via timestamp columns.
- Flexible query construction retrieves exactly required data, returning results as collections of POJOs with corresponding setters or constructors.
- A lightweight, passive library requiring no configuration overhead, enabling smooth integration into any system architecture or framework.
Example
Imagine a database containing two tables: Employees and Departments, from which you want to extract a list of employees that satisfy certain conditions.
The corresponding Oracle SQL query would be:
<syntaxhighlight lang="sql">
SELECT t1.EMPLOYEE_ID,
t1.LASTNAME || ', ' || t1.FIRSTNAME AS NAME,
t2.DEPARTMENT
FROM (EMPLOYEES t1
INNER JOIN DEPARTMENTS t2 ON t1.DEPARTMENT_ID = t2.DEPARTMENT_ID)
WHERE upper(t1.LASTNAME) LIKE upper('Foo%')
AND t1.RETIRED=0
ORDER BY t1.LASTNAME, t1.FIRSTNAME
</syntaxhighlight>
Using Empire-db's command API with references to the object model, the query can be constructed as follows:
<syntaxhighlight lang="java">
SampleDB db = getDatabase();
SampleDB.Employees EMP = db.EMPLOYEES;
SampleDB.Departments DEP = db.DEPARTMENTS;
DBCommand cmd = db.createCommand();
cmd.select(EMP.EMPLOYEE_ID);
cmd.select(EMP.LASTNAME.append(", ").append(EMP.FIRSTNAME).as("NAME"));
cmd.select(DEP.DEPARTMENT);
cmd.join(EMP.DEPARTMENT_ID, DEP.DEPARTMENT_ID);
cmd.where(EMP.LASTNAME.likeUpper("Foo%"));
cmd.where(EMP.RETIRED.is(false));
cmd.orderBy(EMP.LASTNAME);
cmd.orderBy(EMP.FIRSTNAME);
</syntaxhighlight>
To run the query and obtain a list of POJOs representing the results:
<syntaxhighlight lang="java">
public class EmployeeInfo {
private int employeeId;
private String name;
private String department;
// Include getters and setters or constructor here
}
DBReader reader = new DBReader();
try {
reader.open(cmd, getConnection());
List<EmployeeInfo> empList = reader.getBeanList(EmployeeInfo.class);
} finally {
reader.close();
}
</syntaxhighlight>
Additionally, Empire-db provides support for accessing fields through object references and can output query results formatted as XML.
History
Empire-db was originally created by ESTEAM Software, a German company that applied it to develop software solutions across multiple sectors.
In January 2008, Empire-db was released as open source and made available on SourceForge.net.
A proposal to bring Empire-db under the Apache Software Foundation's incubation program was submitted in June 2008. The ASF accepted it in July 2008, transferring all rights accordingly.
October 2008 saw the release of Empire-db version 2.0.4 as the first official Apache incubator version, adopting package names beginning with org.apache.empire.