TE
TechEcho
Home24h TopNewestBestAskShowJobs
GitHubTwitter
Home

TechEcho

A tech news platform built with Next.js, providing global tech news and discussions.

GitHubTwitter

Home

HomeNewestBestAskShowJobs

Resources

HackerNews APIOriginal HackerNewsNext.js

© 2025 TechEcho. All rights reserved.

Typesafe database interaction with Java 8

45 pointsby benjiweberover 11 years ago

12 comments

falloutfarover 11 years ago
Hi Benji, there is a way to avoid CGlib proxy for what you want to do. It&#x27;s clearly a hack but I think it&#x27;s a better hack than using CGlib because it doesn&#x27;t require code generation.<p>By default, a method reference or a lambda doesn&#x27;t carry a reflective description of itself to be lightweight. This description is generated by the compiler, pass to the metafactory but once the method reference object is created the recipe of the method reference is lost. That is true but in one case, if the lambda or the method reference is Serializable, in that case, the recipe need to be kept in order that the runtime can deserialize the method reference. There are several ways to force a method reference to be serializable, the easiest way is to declare the functional interface that will receive the method reference as inheriting from Serializable. This means you can not use java.util.function.Function, you have to create your own interface that inherits from Serializable. Once you have done that, you just have to serialize the method reference to an array and deserialize it by hand to extract the recipe. So the only thing you need is a code that simulate de-serialization withtout using the classical ObjectInputStream of Java because it will not let you sneak at the recipe. The code here [1], do what you want.<p>cheers,<p>[1] <a href="https://github.com/forax/jayspec/blob/master/jayspec/src/com/github/forax/jayspec/SerializationDecoder.java" rel="nofollow">https:&#x2F;&#x2F;github.com&#x2F;forax&#x2F;jayspec&#x2F;blob&#x2F;master&#x2F;jayspec&#x2F;src&#x2F;com...</a>
mbellover 11 years ago
This looks really nice.<p>I wish Java would do something about getters and setters. It&#x27;s very awkward that they are not incorporated into the language but so many libraries assume they exist that it&#x27;s a defacto requirement to generate the same boilerplate constantly. I wish the language itself would provide default &#x27;simple&#x27; getters&#x2F;setters the way Groovy does and incorporate them throughout so I could use e.g.<p><pre><code> .set(Person::firstName) &#x2F;&#x2F;Reference a default setter method for the firstName member if one isn&#x27;t explicitly defined </code></pre> instead of<p><pre><code> .set(Person::setFirstName)</code></pre>
评论 #6976416 未加载
评论 #6976680 未加载
unclebucknastyover 11 years ago
Sorry, but this is ugly. Reminds me a bit of entity beans and all of the extra code hoops required in order to &quot;automate&quot; some functionality.<p>Here, we are really just using code declaratively, then doing extra work to map, etc. Then, folding in CGLib too? And, given that the queries are written in code, modifications to columns manipulated and so forth require re-compilation anyway.<p>Accessing the DB is not rocket science. I am not sure why we find new ways to complicate it so much. It is primarily just grunt work when done by hand. So, I do understand the desire to avoid that part of it.<p>This being the case, I will stick with my homegrown code generation tools for DB interfacing. Over the years, new tools have come and gone, which simply seem to move the problems around (Hibernate, anyone?) And, during that time, I haven&#x27;t found anything simpler or more straightforward than a tool I wrote over ten years ago to generate the (typesafe) base DAO layer from simple queries.
评论 #6977845 未加载
zastrowmover 11 years ago
As a .NET programmer, it&#x27;s great to see the changes that are coming to Java. Method references and lambdas should make for some really nice + typesafe fluent apis.<p>Although waiting for Android to support the new features will still be a pain (it just got support for 7 with the release of KitKat).
评论 #6976070 未加载
评论 #6976160 未加载
ZitchDogover 11 years ago
This ends up looking a lot like JOOQ - but JOOQ doesn&#x27;t require Java8<p><a href="http://www.jooq.org" rel="nofollow">http:&#x2F;&#x2F;www.jooq.org</a>
评论 #6976479 未加载
revetknover 11 years ago
How does it handle operations more complex than &quot;select * from user&quot;? For example, joins and subqueries. And wouldn&#x27;t it be nicer to have the database layer figure out properties for you instead of writing mapping code by hand?<p>For example (this uses SQL, I am not big on ORMs):<p><pre><code> User user = database.queryForObject(&quot;SELECT * FROM user WHERE user_id=?&quot;, User.class, userId);</code></pre>
评论 #6976894 未加载
lukasederover 11 years ago
Congrats to your tool. Using method references is quite clever, reminds me of a couple of tools that did something with instrumentation and advanced reflection (cglib, I guess). These come to my mind:<p>OhmDB (<a href="http://www.ohmdb.com" rel="nofollow">http:&#x2F;&#x2F;www.ohmdb.com</a>)<p>LambdaJ (<a href="https://code.google.com/p/lambdaj/" rel="nofollow">https:&#x2F;&#x2F;code.google.com&#x2F;p&#x2F;lambdaj&#x2F;</a>)<p>JaQu (<a href="http://www.h2database.com/html/jaqu.html" rel="nofollow">http:&#x2F;&#x2F;www.h2database.com&#x2F;html&#x2F;jaqu.html</a>)<p>JIRM, I think, also played around with similar ideas (<a href="https://github.com/agentgt/jirm" rel="nofollow">https:&#x2F;&#x2F;github.com&#x2F;agentgt&#x2F;jirm</a>)<p>Comparing this approach with jOOQ (<a href="http://www.jooq.org" rel="nofollow">http:&#x2F;&#x2F;www.jooq.org</a>), I think that method references might be somewhat limited in a SQL sense of thinking. How would you handle aliases? E.g. how would you perform a self-join, such as:<p><pre><code> FROM person p1 JOIN person p2 ON p1.parent_id = p2.id</code></pre>
batbombover 11 years ago
I wrote something in Java 7 I call zerorm that has some similar functionality. The type safety is mostly optional, but it can be enforced in a few ways, either through the compiler or at &quot;bind time&quot;. It&#x27;s similar to jOOq, korma, etc... but one nice benefit is it doesn&#x27;t try to do everything, it has no dependencies, and the core part of the code is around 3k LoC IIRC.<p><a href="http://github.com/zerorm/zerorm" rel="nofollow">http:&#x2F;&#x2F;github.com&#x2F;zerorm&#x2F;zerorm</a>
评论 #6976901 未加载
drdaemanover 11 years ago
Is it a language- or platform-side feature? I mean, is this something that needs additional support from JVM side or could be done on existing JVMs (i.e. a compiler for Java 8 could be written that would produce code that&#x27;d run on older JDK without need for upgrade)?
评论 #6976130 未加载
ww520over 11 years ago
I wrote something similar (Jsoda) for AWS&#x27;s databases a while back, <a href="https://github.com/williamw520/jsoda" rel="nofollow">https:&#x2F;&#x2F;github.com&#x2F;williamw520&#x2F;jsoda</a>. It just uses plain Java.
mpweiherover 11 years ago
Looks a lot like what was done in ROE or Gemstone, which both execute blocks&#x2F;lambdas against recording proxies and then translate the messages to DB queries. (I did something similar with Higher Order Messages).
bsaulover 11 years ago
I&#x27;m not a java expert, so i&#x27;m wondering : is the fact that this example still has to rely on cglib a downside ?
评论 #6976539 未加载