I use to always put my SQL code in specific files, because I came from ugly Oracle SQL preprocessor where SQL and code where mixed like hell (JSP files are the same BTW). But, today I prefer Named Queries and/or JDBC 4. I use to declare my ORM mapping and my Depency Injection in a separate XML file :) Now I used JPA and JaCaDI (JSR-299 + JSR-330)
Having the SQL query above the method that will execute is a HUGE benefit: @SQL("select id from datastore where length > :threshold") Collection<Long> getIdsAbove(long threshold); Clear, easy to debug/modify/refactor and especially extremely clear usage of what the method is doing. It's the same for ORM and DI using Annotations vs. XML. It's hard to have a good balance when mixing languages in the same file, but when done right it's always a winner. Clean injection of DSL is the good way to go. On Thu, Sep 3, 2009 at 3:16 AM, Casper Bang <casper.b...@gmail.com> wrote: > > Annotations are the recommended way however, as it facilitates > validation. If you use em.createQuery(...) everything is late-bound > and even less type checked than using a namedQuery. For SQL though, > you are absolutely right but sadly SQL is no longer considered trendy. > > /Casper > > On 3 Sep., 01:53, Jess Holle <je...@ptc.com> wrote: > > Casper Bang wrote: > > > The issue goes a little deeper than your simple use case though. The > > > Java enterprise world, due to the lack of expression trees, has to > > > work with many thousands lines of embedded DSL (JPQL/HQL/SQL etc.). > > > This poses several problems: > > > > > - How do you format/indent it? > > > - How do you copy-paste between database tool and IDE? > > > - How do you get help with the syntax? > > > > > By my experience, that is very much a real problem : > > > > > " select new com.somecompany.somexp.backend.model.ManagerInfoRecord("+ > > > " c, b.itemNo, x )" + > > > " from SomeEntity x" + > > > " Join x.account a" + > > > " Join a.baseData b" + > > > " Join b.customer c "+ > > > " where x.state <> com.somecompany.somexp.backend.entities.SomeEntity > > > $ReadingState.InValid " + > > > " and ( " + > > > " (" + > > > " x.someState = com.somecompany.somexp.backend.entities.SomeEntity > > > $someState.Valid " + > > > " and b.someData <> 'Y' " + > > > " )" + > > > " or x.someOtherState = > > > com.somecompany.somexp.backend.entities.SomeEntity > > > $someOtherState.Valid " + > > > " )" + > > > " and b.relatedEntity =:relatedEntity " + > > > " and x.someForeignRelation <> > > > com.somecompany.somexp.backend.entities.SomeEntity$EntityStateEnum.E > > > " ), > > > > > And that's just one small annotation query (obfuscated to protect the > > > innocent). Fun eh? :) > > > > You don't embed this sort of thing. You use getResourceAsStream() and a > > separate file along with MessageFormat or such to perform substitutions > > of live data if need be. > > > > Plain and simple. > > > > This also allows the IDE to do easily appropriate things for a .sql file > > or the like. > > > > [In the XML sphere I'm all for sticking SVG, XSL FO, etc, directly > > inside an XHTML document -- but this whole approach seems to be dying on > > the vine there, even though the grammar is the same throughout all of > > these.] > > > > -- > > Jess Holle > > > -- JFrog Ltd 5 Habonim st., P.O.Box 8187 Netanya, Israel 42504. Tel: +972 9 8941444 Fax: +972 9 8659977 http://www.jfrog.org/ http://freddy33.blogspot.com/ http://nothingisinfinite.blogspot.com/ --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "The Java Posse" group. To post to this group, send email to javaposse@googlegroups.com To unsubscribe from this group, send email to javaposse+unsubscr...@googlegroups.com For more options, visit this group at http://groups.google.com/group/javaposse?hl=en -~----------~----~----~----~------~----~------~--~---