I wrote a C# implementation for NHibernate here: https://github.com/shaynevanasperen/NHibernate.Sessions.Operations.
It works by using an interface like this:
public interface IDatabases
{
ISessionManager SessionManager { get; }
T Query<T>(IDatabaseQuery<T> query);
T Query<T>(ICachedDatabaseQuery<T> query);
void Command(IDatabaseCommand command);
T Command<T>(IDatabaseCommand<T> command);
}
Given a POCO entity class like this:
class Database1Poco
{
public int Property1 { get; set; }
public string Property2 { get; set; }
}
You can build query objects like this:
class Database1PocoByProperty1 : DatabaseQuery<Database1Poco>
{
public override Database1Poco Execute(ISessionManager sessionManager)
{
return sessionManager.Session.Query<Database1Poco>().SingleOrDefault(x => x.Property1 == Property1);
}
public int Property1 { get; set; }
}
And then use them like this:
var database1Poco = _databases.Query(new Database1PocoByProperty1 { Property1 = 1 });
You could port that to Java if you like it.
Here are some other examples:
https://lostechies.com/jimmybogard/2012/10/08/favor-query-objects-over-repositories/
http://www.mrdustpan.com/command-query-objects-with-dapper#disqus_thread
http://crosscuttingconcerns.com/CommandQuery-Object-pattern