Skip to content

Instantly share code, notes, and snippets.

@abyrd
Created September 5, 2017 16:47
Show Gist options
  • Save abyrd/0e942f633939b55c7c09ee398cde6c81 to your computer and use it in GitHub Desktop.
Save abyrd/0e942f633939b55c7c09ee398cde6c81 to your computer and use it in GitHub Desktop.
An example of basic ORMLite persistence of Java objects with Postgres
package com.conveyal.datatools.manager.persistence;
import com.j256.ormlite.dao.Dao;
import com.j256.ormlite.dao.DaoManager;
import com.j256.ormlite.field.DatabaseField;
import com.j256.ormlite.jdbc.DataSourceConnectionSource;
import com.j256.ormlite.table.DatabaseTable;
import com.j256.ormlite.table.TableUtils;
import org.apache.commons.dbcp2.ConnectionFactory;
import org.apache.commons.dbcp2.DriverManagerConnectionFactory;
import org.apache.commons.dbcp2.PoolableConnectionFactory;
import org.apache.commons.dbcp2.PoolingDataSource;
import org.apache.commons.pool2.impl.GenericObjectPool;
import javax.sql.DataSource;
import java.io.Closeable;
import java.io.Serializable;
import java.sql.SQLException;
import java.util.UUID;
import static com.conveyal.datatools.manager.DataManager.LOG;
/**
* An example of basic ORMLite persistence in Postgres
*/
public class ORMLitePersistence {
public static final String DATABASE_URL = "jdbc:postgresql://localhost/ormlite_test";
public static void main (String[] args) {
Dao<TestThing, String> things;
// create a connection source to our database
DataSourceConnectionSource connectionSource = null;
try {
connectionSource = new DataSourceConnectionSource(createDataSource(), DATABASE_URL);
// Create a Data Access Object
things = DaoManager.createDao(connectionSource, TestThing.class);
// Create a corresponding database table as needed
TableUtils.createTableIfNotExists(connectionSource, TestThing.class);
for (int i = 0; i < 1024; i++) {
// Persist a fresh object to the database
TestThing testThing = new TestThing();
testThing.hex = Integer.toHexString(i);
testThing.octal = Integer.toOctalString(i);
testThing.embedded = new TestThing();
things.create(testThing);
// Retrieve the feedSource from the database by its id field (name)
TestThing retrievedThing = things.queryForId(testThing.id);
System.out.println("Retrieved: " + testThing.toString());
}
} catch (SQLException ex) {
LOG.error("Exception: {}", ex.toString());
} finally {
justCloseIt(connectionSource);
}
}
public static DataSource createDataSource () {
// ConnectionFactory can handle null username and password (for local host-based authentication)
ConnectionFactory connectionFactory = new DriverManagerConnectionFactory(DATABASE_URL, null, null);
PoolableConnectionFactory poolableConnectionFactory = new PoolableConnectionFactory(connectionFactory, null);
GenericObjectPool connectionPool = new GenericObjectPool(poolableConnectionFactory);
poolableConnectionFactory.setPool(connectionPool);
// Disabling auto-commit on the connection factory confuses ORMLite, so we leave it on.
// In any case ORMLite will create transactions for batch operations.
return new PoolingDataSource(connectionPool);
}
public static void justCloseIt (Closeable closeable) {
try {
if (closeable != null) closeable.close();
} catch (Exception ex) {
LOG.error("Error closing.");
}
}
@DatabaseTable
public static class TestThing implements Serializable {
@DatabaseField(id = true)
String id = UUID.randomUUID().toString();
@DatabaseField
String hex = "nothing";
@DatabaseField
String octal = "nothing";
@DatabaseField
TestThing embedded = null;
@Override
public String toString() {
return "TestThing{" +
"id='" + id + '\'' +
", hex='" + hex + '\'' +
", octal='" + octal + '\'' +
'}';
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment