Skip to content

Instantly share code, notes, and snippets.

@landonreed
Created September 3, 2017 07:58
Show Gist options
  • Save landonreed/a19a39e149cefcc7c041eab0707acb4a to your computer and use it in GitHub Desktop.
Save landonreed/a19a39e149cefcc7c041eab0707acb4a to your computer and use it in GitHub Desktop.
Sample code testing ORMLite in datatools-server
package com.conveyal.datatools.manager.persistence;
import com.conveyal.datatools.manager.models.FeedSource;
import com.conveyal.datatools.manager.models.Note;
import com.conveyal.datatools.manager.models.Project;
import com.j256.ormlite.dao.Dao;
import com.j256.ormlite.dao.DaoManager;
import com.j256.ormlite.jdbc.DataSourceConnectionSource;
import com.j256.ormlite.table.TableUtils;
import java.sql.SQLException;
import java.util.UUID;
import static com.conveyal.datatools.manager.DataManager.GTFS_DATA_SOURCE;
import static com.conveyal.datatools.manager.DataManager.getConfigPropertyAsText;
/**
* Created by landon on 8/30/17.
*/
public class ORMLitePersistence {
private static DataSourceConnectionSource connectionSource;
public static Dao<FeedSource, UUID> feedSources;
public static Dao<Project, UUID> projects;
public static Dao<Note, UUID> notes;
public static Dao<FeedSource, UUID> feedSources;
public static Dao<FeedSource, UUID> feedSources;
public static Dao<FeedSource, UUID> feedSources;
public static void initialize() throws SQLException {
// create a connection source to our database
connectionSource = new DataSourceConnectionSource(GTFS_DATA_SOURCE, getConfigPropertyAsText("GTFS_DATABASE_URL"));;
// instantiate the dao
feedSources = DaoManager.createDao(connectionSource, FeedSource.class);
projects = DaoManager.createDao(connectionSource, Project.class);
notes = DaoManager.createDao(connectionSource, Note.class);
// if you need to create the 'feedSources' table make this call
TableUtils.createTableIfNotExists(connectionSource, FeedSource.class);
TableUtils.createTableIfNotExists(connectionSource, Project.class);
TableUtils.createTableIfNotExists(connectionSource, Note.class);
// create an instance of Account
FeedSource feedSource = new FeedSource();
// persist the feedSource object to the database
feedSources.create(feedSource);
// retrieve the feedSource from the database by its id field (name)
FeedSource feedSource2 = feedSources.queryForId(feedSource.id);
System.out.println("FeedSource: " + feedSource2.name);
// close the connection source
// connectionSource.close();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment