OrmLite - Lightweight Object Relational Mapping (ORM) Java Package

ORM Lite lightweight orm java package

Object Relational Mapping Lite (ORM Lite) provides some simple, lightweight functionality for persisting Java objects to SQL databases while avoiding the complexity and overhead of more standard ORM packages.

ORMLite is easy to use and provides the following features:

Use Annotations to Mark Classes to be Persisted

To use the package you add the @DatabaseTable annotation to the top of each class and a @DatabaseField annotation to each of the fields in the class that are to be persisted to the database. For example:

@DatabaseTable(tableName = "accounts") public class Account { @DatabaseField(id = true) private String name; @DatabaseField(canBeNull = false) private String password; ... Account() { // all persisted classes must define a no-arg constructor with at least package visibility } ... }

Classes can also be configured with javax.persistence annotations (JPA), Java calls, or Spring wiring. For more details, see the online documentation.

Sample Code Example

The following is a quick code example to give you a taste on how to use the package.

// this uses h2 but you can change it to match your database String databaseUrl = "jdbc:h2:mem:account"; // create a connection source to our database ConnectionSource connectionSource = new JdbcConnectionSource(databaseUrl); // instantiate the DAO to handle Account with String id Dao<Account,String> accountDao = DaoManager.createDao(connectionSource, Account.class); // if you need to create the 'accounts' table make this call TableUtils.createTable(connectionSource, Account.class); // create an instance of Account String name = "Jim Smith"; Account account = new Account(name, "_secret"); // persist the account object to the database accountDao.create(account); // retrieve the account Account account2 = accountDao.queryForId(name); // show its password System.out.println("Account: " + account2.getPassword()); // close the connection source connectionSource.close();

Free Spam Protection   Eggnog Recipe   Android ORM   Simple Java Magic   JMX using HTTP   OAuth 2.0 Simple Example   Great Eggnog Recipe