I want to avoid code duplication in next situation: I have two classes NewTx and GetTx, first represents new transaction, second represent a transaction, that joins current. The goal is to make CRUD code as compact as possible, so classes are usually used as:
List<Users> users = NewTx.loadList("select u from User u"); Student s = GetTx.find(Student.class, '02d7c3fe-e9cf-11e4-8ceb-c7b1b9baf140'); These classes actually differ only in a way they obtain a transaction, but all their methods are static, so it seems impossible to move logic to parent class.
Now I have
public final class NewTx extends Tx { public static <T> List<T> loadList(String query) { return tx().call(Tx.<T>getLoadListCallable(query)); } static Transaction tx() { return DataSource.createTransaction(); } As I said before, only tx() method is different for NewTx and GetTx classes, other methods just obtain transaction and than delegate job to parent Tx class.
So the goal is to move all CRUD methods like loadList to parent class Tx.
Restriction: method calls must look like before: NewTx.load(..., not NewTx.get().load(..
Any ideas?