English 中文(简体)
closures mean fully type-safe criteria?
原标题:

combining closures (FCM) and generics, would it be possible to have fully type-safe criteria.

// The following works without a cast as Foo.id is a  long  field.
List<Long> ids = session.createCriteria(Foo.class)
                        .setProjection(Foo#id)
                        .list();

// The following is a compilation error, as Foo.bar is defined as an int, and not a string
session.createCriteria(Foo.class)
       .addRestriction(Restrictions.eq(Foo#bar,"blah"))
       .list();

I ve read the JPA 2.0 spec for type-safe criteria. But its still somewhat lacking.

Besides, I m just using criteria here as an example of improving the type-safety of code in general. I used the static typing of java heavily to let me code faster. But as a result I get bitten now and then by the parts of my code that ignore typing. For example HQL queries.

最佳回答

The code you re describing does not use closures but field literals (method literals). Like the good old class literal. These could help in a criteria API. The JPA 2 source code generation work-around for a type-safe query interface could be replaced with it. If it will be part of JDK7.

问题回答

As Thomas points out, this doesn t strictly require closures. It s all up in the air at the moment, given no-one knows quite what proposal is being looked at. It s not clear if FCM is actually the basis of the proposal, particularly given that Stephen Colebourne seemed to be as susprised as anyone about the announcement.

A lot of people are pointing at Neal Gafter s mysteriously-revised-more-or-less-right-as-the-Devoxx-presentation-announcing-closures-was-being-given spec as a hint as to what form closures might take. Mind you, the revised proposal looks (aesthetically) rather like FCM!

That spec does include the kind of references you refer to (under Method References in the above line), and of course FCM has the same. Yes, this would definitely make you suggest possible. My very first thought when reading about this was how it would affect JPA/Hibernate, and/or our own abstraction layers around same, in this regard. Typesafe, refactorable method references in your criteria? Hell yeah.





相关问题
Spring Properties File

Hi have this j2ee web application developed using spring framework. I have a problem with rendering mnessages in nihongo characters from the properties file. I tried converting the file to ascii using ...

Logging a global ID in multiple components

I have a system which contains multiple applications connected together using JMS and Spring Integration. Messages get sent along a chain of applications. [App A] -> [App B] -> [App C] We set a ...

Java Library Size

If I m given two Java Libraries in Jar format, 1 having no bells and whistles, and the other having lots of them that will mostly go unused.... my question is: How will the larger, mostly unused ...

How to get the Array Class for a given Class in Java?

I have a Class variable that holds a certain type and I need to get a variable that holds the corresponding array class. The best I could come up with is this: Class arrayOfFooClass = java.lang....

SQLite , Derby vs file system

I m working on a Java desktop application that reads and writes from/to different files. I think a better solution would be to replace the file system by a SQLite database. How hard is it to migrate ...

热门标签