Created
September 26, 2014 13:41
A concept for making JPA queries type-safe using code generation.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class TypeSafeJPA { | |
class MyBean { | |
private String foo; | |
private int bar; | |
public String getFoo() { | |
return foo; | |
} | |
public void setFoo(String foo) { | |
this.foo = foo; | |
} | |
public int getBar() { | |
return bar; | |
} | |
public void setBar(int bar) { | |
this.bar = bar; | |
} | |
} | |
@Test | |
public void testAPI() throws Exception { | |
Selection<MyBean> myBeanSelection = select(MyBean.class); | |
myBeanSelection.where().setFoo("bar"); | |
myBeanSelection.or().setFoo("bar2"); | |
myBeanSelection.where().setBar(23156); | |
//Selection<MyBean> copy = myBeanSelection.clone(); | |
MyBean databaseLookup = myBeanSelection.uniqueResult(); | |
} | |
static <T> Selection<T> select(Class<T> type) { | |
return new Selection<T>(type); | |
} | |
static class Selection<T> { | |
private final Class<T> type; | |
private final List<String> criteria = new ArrayList<String>(); | |
Selection(Class<T> type) { | |
this.type = type; | |
} | |
T where() { | |
try { | |
return new ByteBuddy() | |
.subclass(type) | |
.method(any()).intercept(MethodDelegation.to(this).filter(named("where"))) | |
.make() | |
.load(type.getClassLoader(), ClassLoadingStrategy.Default.WRAPPER) | |
.getLoaded() | |
.newInstance(); | |
} catch (Exception e) { | |
throw new RuntimeException(); | |
} | |
} | |
T or() { | |
try { | |
return new ByteBuddy() | |
.subclass(type) | |
.method(any()).intercept(MethodDelegation.to(this).filter(named("or"))) | |
.make() | |
.load(type.getClassLoader(), ClassLoadingStrategy.Default.WRAPPER) | |
.getLoaded() | |
.newInstance(); | |
} catch (Exception e) { | |
throw new RuntimeException(); | |
} | |
} | |
void where(@Argument(0) Object arg, @Origin Method m) { | |
criteria.add("Make sure that " + m.getName() + " is " + arg); | |
} | |
void or(@Argument(0) Object arg, @Origin Method m) { | |
criteria.add("or make sure that " + m.getName() + " is " + arg); | |
} | |
public T uniqueResult() { | |
for (String s : criteria) { | |
System.out.println(s); | |
} | |
return null; // Query JPA | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment