Happy to help!
We had a bit of a learning curve even to do basic CRUD in Java with Play, but once we got the patterns down, it's been a pleasure. Java 8 is also nice because lambda's make the code a lot more concise.
Here's an example DAO implementation, in case this helps. Note that we're using Google Guice for DI. Also, there's probably a better way to handle passing the database connection around. We stuff it in the ctx().args object, which is where you can put request-scoped items, and then our service class just looks to see if it's available, or otherwise throws an exception. This caused a couple problems when doing testing, but most of our DAO tests just connect directly to the database.
LocationRecord is auto-generated by jOOQ, as is the LOCATION reference to our "Location" table. The line
db.jooq.Utils.resetChangedOnNotNull( locationRecord ); is because of this issue, and you can use this fix in the meantime.
Note how we use our business rules to set some property likes lastModifiedBy, but otherwise, jOOQ handles all our properties automagically for us.
Also, check out how easy the find() query is. jOOQ gives you typesafe SQL statements written in Java so it's easy to customize these types of queries with JOINs or other various SQL concepts.
Josh
public class LocationDaoImpl implements LocationDao {
private Location location;
private final AragornDatabaseConnection aragornDatabaseConnection;
private final SessionSvc sessionSvc;
@Inject
public LocationDaoImpl( AragornDatabaseConnection aragornDatabaseConnection,
SessionSvc sessionSvc ) {
this.aragornDatabaseConnection = aragornDatabaseConnection;
this.sessionSvc = sessionSvc;
}
@Override
public void setLocation(Location location) {
this.location = location;
}
@Override
public Location insert() throws AragornDaoInvalidStateException, SQLException {
// Validate the service class state
if ( this.location == null ) {
throw new AragornDaoInvalidStateException( "ERROR: You called insert(), but location has not been set yet." );
}
// Get database connection
aragornDatabaseConnection.setHttpContext( ctx() );
DSLContext create = DSL.using( aragornDatabaseConnection.getConfiguration() );
this.location.setCreatedByUserId( sessionSvc.getCurrentUserId() );
this.location.setLastModifiedByUserId( sessionSvc.getCurrentUserId() );
LocationRecord locationRecord = create.newRecord( LOCATION, this.location );
db.jooq.Utils.resetChangedOnNotNull( locationRecord );
locationRecord.insert();
return locationRecord.into( Location.class );
}
@Override
public Location findLocationById(Integer locationId) throws SQLException, AragornDaoInvalidParameterException {
// Validation
if ( locationId == null ) {
throw new AragornDaoInvalidParameterException( "ERROR: You attempted to find a Location you but supplied a null locationId" );
}
// Get database connection
aragornDatabaseConnection.setHttpContext( ctx() );
DSLContext create = DSL.using( aragornDatabaseConnection.getConfiguration() );
Record result = create.select()
.from( LOCATION )
.where( LOCATION.LOCATION_ID.eq( locationId ) )
.fetchOne();
// Convert the results into POJOs
Location location = result.into(Location.class);
return location;
}