At Data Geekery (the company behind jOOQ), we love SQL. And we love Java. But one thing has always bothered us in the past. Java is not really a purely declarative language. A lot of Java language constructs are real anti patterns for the enlightened declarative programmer. For instance:
// This is bad
for (String string : strings)
System.out.println(string);
// This is even worse
try {
someSQLStatements();
}
catch (SQLException e) {
someRecovery();
}
The imperative style of the above code is hardly ever useful. Programmers need to tediously tell the Java compiler and the JVM what algorithm they meant to implement, down to the single statement, when in reality, using the JIT and other advanced optimisation techniques, they don’t really have to.
Luckily, there are annotations
Since Java 5, however, there have been farsighted people in expert groups who have added a powerful new concept to the Java language: Annotations (more info here). At first, experiments were made with only a handful of limited-use annotations, like:
@Path("/MonsterRest")
@Stateless
@WebServlet(urlPatterns = "/MonsterServlet")
@Entity
@Table(name = "MonsterEntity")
@XmlRootElement
@XmlAccessorType(XmlAccessType.FIELD)
@NamedQuery(name = "findAll", query = "SELECT c FROM Book c")
public class Book extends HttpServlet {
// ======================================
// = Attributes =
// ======================================
@Id
@GeneratedValue
private Long id;
private String isbn;
private Integer nbOfPage;
private Boolean illustrations;
private String contentLanguage;
@Column(nullable = false)
@Size(min = 5, max = 50)
@XmlElement(nillable = false)
private String title;
private Float price;
@Column(length = 2000)
@Size(max = 2000)
private String description;
@ElementCollection
@CollectionTable(name = "tags")
private List<String> tags = new ArrayList<>();
Look at this beauty. Credits to Antonio Goncalves
However, we still think that there is a lot of unnecessary object oriented bloat in the above. Luckily, recent innovations that make Java annotations turing complete (or even sentient?) will now finally allow us to improve upon this situation, specifically for jOOQ, which aims to model the declarative SQL language in Java. Finally, annotations are a perfect fit!
Those innovations are:
These innovations allow us to completely re-implement the entire jOOQ 4.0 API in order to allow for users writing SQL as follows:
@Select({
@Column("FIRST_NAME"),
@Column("LAST_NAME")
})
@From(
table = @Table("AUTHOR"),
join = @Join("BOOK"),
predicate = @On(
left = @Column("AUTHOR.ID"),
op = @Eq,
right = @Column("BOOK.AUTHOR_ID")
)
)
@Where(
predicate = @Predicate(
left = @Column("BOOK.TITLE"),
op = @Like,
right = @Value("%Annotations in a Nutshell%")
)
)
class SQLStatement {}
Just like JPA, this makes jOOQ now fully transparent and declarative, by using annotations. Developers will now be able to completely effortlessly translate their medium to highly complex SQL queries into the exact equivalent in jOOQ annotations.
Don’t worry, we’ll provide migration scripts to upgrade your legacy jOOQ 3.x application to 4.0. A working prototype is on the way and is expected to be released soon, early adopter feedback is very welcome, so stay tuned for more exciting SQL goodness!
You’re killing me man. Each and every year I fall to some april’s fool joke just because where I live the “joke day” is 28th december so I don’t have the mind frame to fight this. And it’s first day on a spring rainy day. I’ve read, like, 50% of the post until I realized…
@Comment(body=”hey now, annotations can be use for good in moderation”, sentiments={ @Sentiment(seriousness=4.0, sentimentValue=Sentiment.NOT_USED), @Sentiment(seriousness=2.0, sentimentValue=Sentiment.OK)})
@AnnotationsInCodeAreAtLeastBetterThanTomesOfXmlInSidecarFiles(true)
public class myComment() implements Serializable { }
You got me!
You’re killing me man. Each and every year I fall to some april’s fool joke just because where I live the “joke day” is 28th december so I don’t have the mind frame to fight this. And it’s first day on a spring rainy day. I’ve read, like, 50% of the post until I realized…
I just need some coffee. Well played!
;) See you again, next year!
“annotation type system theory”
I’d like to know more… Are jOOQ annotations monadic?
No. But sentient. We have plans for adding support for a single @Do annotation in jOOQ 4.1. jOOQ will figure out what you meant.
Can we have xdoclet support, too?
Of course. Just add
@XDoclet
+1 for implements Serializable. This is really important!