Java

Ooo JPA, JPA

I normally have nothing good to say about ORM so I had a pleasant surprise today when I was able to generate four One to Many object relationships in about four or five hours.

I’m currently using TopLink although I try to stick to space inside the standard and avoid getting dragged into vendor extensions. I actually prefer OpenJPA from a testing point of view as TopLink won’t seem to play ball unless there is a persistence.xml in a directory called META-INF at the root of your classpath.

The basic syntax is pretty simple, you create a field that is a Collection of the Entity that represents the other side of the relationship, I used a List but then wondered why I had done that. You then annotate that field with @OneToMany and then specify the Join mechanism. Now this is where it usually gets messy and I have to say there is a certain amount of gnarl to this one. I had a relatively simple Join Table on the go but thankfully the annotation @JoinTable needs to know only three things.

Firstly the name of the table (as per the @Column annotation) and then the JoinColumns and InverseJoinColumns parameters. These names are correct but probably seem fiercer than they need to be. Both parameters take an array of @JoinColumn annotations (nested annotations! a first for me). Even if you just have one, wrap it in curly braces. The @JoinColumn syntax is the same as that for @OneToOne and specifies which column to resolve the @Id lookup with.

Join Columns identify the Id of the entity that has the Collection field; this class in other words. The InverseJoinColumns identify the Ids of the other entities that exist in the relationship.

So if Foo has a Collection of Bars then JoinColumns identifies the database columns that contain the Ids of Foo and the InverseJoinColumns identify the Ids of the Bars.

To be honest TopLink has decent enough logging that you can usually muddle along coredumping the SQL that the annotations are building up. My worst mistake was mixing up the @Table annotation with that that of @JoinTable and trying to select from my join table joined with, my join table.

The nice thing is that I was able to very quickly build up four foreign key relationships, all using join tables (some of which had been badly denormalised) and all by annotating the POJOs that also serve a domain purpose. I had to add some Setters for testing purposes but apart from that if I wasn’t using the data in the application I didn’t have to describe it in the configuration.

Good work JPA!

Standard
Java, London, Programming

London Scala User Group Meeting

Had a good meeting (I thought anyway) at the TW offices that featured a talk from Jamie Webb introducing Scala and then a quick dojo. Scala has a much terser syntax than Java but I managed to do my classic thing of mixing my languages and putting my constructs in the wrong place. The other thing that doing rather discussing taught me is that Scala’s constructor syntax is much better defined than Java with the default constructor going into the declaration of the class. That’s the kind of improvement on Java that I really like in Java. It seems to distill all these lessons you’ve been learning if you write good Java code.

The pub after the event also saw a lot of discussion, there was quite a bit of discussion about testing and dynamic versus static typing. Personally I have decided to follow Neal Ford and reject the issue. To me both Ruby and Scala are low ceremony, high essence languages that choose different approaches to address the same issue. As such I don’t think there is any contradiction in liking and wanting to learn both languages.

Similarly I love compile time checking but I’m not giving up on testing. It’s a synergistic practice and I want to get the benefits from TDD/BDD/Tests as Specification. To me there is some irony that if I do have a test suite then actually I have more flexibility about my choice of languages. Not testing means you get much higher value from strong compile checking and therefore something like Scala is going to deliver much more benefit quickly. At the end of the day though I don’t want to have treat my tests as the gamekeeper of my wild code. I do expect the language and the compiler to give some structure and help to the development process.

Despite (because of?) my mistakes  want to do some more and I think Aaron (the founder of the group) suggested converting an existing Java program to Scala. That seems pretty sensible and I have an old Java program that I use a lot hanging around that needs some TLC.

If you are interested in Scala and in London I would recommend coming along to this meetings because you have the chance to meet and talk to everyone who is really engaged in Scala in the city. And to the guy who came all the way from Belgium; I salute you, you are a real hero!

Standard
Groovy, Java, Programming, Scripting, Web Applications

Towards Groovier Projects

My latest project has witnessed an influx of Groovy. The project buildfile is run by Gant, there are Groovlets providing lightweight pages but it is in the test folders that the Groovy has made its most insidious advances.

Markup builder makes fragments of HTML to test, Groovy’s built-in Sql is beginning to setup and check the results of data operations at a functional level. Soon I plan to implement a Groovy Struts 2 result and then it will begin to replace Freemarker.

Standard
Java, Programming, Web Applications

Notes on NetBeans 6.1

I am working on an exciting web prototype project at the moment and I am using NetBeans 6.1 rather than my usual Eclipse environment. Overall I am very happy with the experience. The good points are:

  • NetBeans seamlessly integrated with my existing Tomcat instance, configured it correctly and makes it easy to build and deploy my application making my build-compile-deploy cycle as quick as it has ever been
  • The Javascript support is excellent and allowed to discover the JQuery API with no problems
  • It has been easy to define libraries and have them brought in to the project
  • The built-in XSLT validation and one-click transform of a test file is really powerful and very simple to use
  • Associating a web project with different deployment servers is easy
  • The GUIs for writing Java Web App configuration files is great
  • Managing NetBean Modules is easy and the interface is good
  • NetBeans 6.1 is fast, really noticably faster than 6.0.1

That is a whole lot of positive, so what hasn’t been so good?

  • Code templates complete on a different keystroke to code completion. The seperation doesn’t make sense to me. Both things are about completing what I have typed.
  • The window of opportunity for completing a code template is too narrow. If you don’t complete while typing the template stub then it doesn’t work.
  • When entering the parameters of a generated method you have to hit return too much. If the auto-generated code has aready correctly assigned variables to the parameter position then I’m done. At the moment though you have to hit return for each parameter otherwise the code generator gets in a tizzy and often opens brackets on the line below the current method.
  • Really gnarly HTML can take a long time to parse for the navigator windows and the slowdown is noticeable across the whole IDE. XML blazes by comparision.
  • There’s a lot of magic going on to make the project work automagically and there isn’t a good “raw” representation of the project that would make it easy to work from CI and the command-line.

Essentially most of these problems are to do with the interface being too fussy and in some cases too specialised. However I do thing I am getting the benefit of the integrated development environment and while it may not be as fast as Rails it is some of the fastest Java web development I have ever done.

Standard
Java, London, Programming, ThoughtWorks

Geek Nights are go!

So it’s taken a lot of work but finally we have Geek Nights! Yeah!

The events are effectively sponsered by ThoughtWorks as they are provided the food, drink and venue (the ThoughtWorks London office). The first one is going to be on mocking and Steve Freeman and Nat Pryce of JMock are going to give a talk.

The Geek Nights are open to anyone who is interested in the topic, you can sign up via the link on the Wiki.

Standard
Groovy, Java, Programming, Scripting

A Groovy Dice Roller DSL Solution

I’ve been looking at Ruby Quiz 61 for a while and I decided to take a look at solving it in Groovy as well.

Here’s a rough draft of what a solution might look like. So what did I learn about doing DSL in Groovy?

Well overall I think the Ruby solution is probably more elegant but Groovy’s Categories make meta-programming easy for meta village idiots like myself. I really struggled at first though because the documentation is so poor. I had to refer to Groovy in Action to sort myself out.

The key issue was that a category must consist of static methods which have at least one parameter, the first parameter should be of the type you want to “attach” the method to (so in this example I want to add the method d to Integer). You then effectively form a closure on the block of code to be executed with the method use which takes a list of categories you want to have active in the block.

The Eval object acts as a kind of synonym for GroovyShell but is meant to remove the boilerplate. Eval.me invokes the block with no pass-in parameters.

I think that Groovy does a decent job of providing solutions to problems like this but if you had a choice to make would you choose JRuby or Groovy? It ain’t easy. What I am convinced off is that both should be part of the library mix in any Java shop that is looking to the future.

Standard
Java, Software

Jude, the Java Documentation Browser

I recently bought a license to Dave Flanagan’s Jude. I have enjoyed Dave’s books on Javascript, Ruby and of course the Java in a Nutshell series. I was a little disappointed that the Nutshell Java book didn’t get an update to reflect Java 6 and looking for more information I took a trip to Dave’s site and saw an updated version of Jude. I had tried it before but preferred the paper version of the book (it’s initial sections on Java features are still models in the genre).

However with no new edition in the offing I decided to buy Jude and process my Java 6 JDK. Jude basically reads the Javadoc and code in the target JDK and then serves that information out via an in-application HTTP server which you point your browser at.

Recently I have been switching between several “post Java” languages: JRuby, Groovy, Scala and Jython. In all of them though I have needed to know what exactly the details of the Java Library API are so I can access them via the host language. The other day I was filing a bug report and needed to know the details of the Charset object, as I was flicking between between Jude and the report I wondered, “When did this app become indispensible?”.

It might be argued that all you need is Google and the Sun Javadoc but Jude has a few features that make it much more useful. Obviously it useable offline, that’s not something to be sniffed at. Secondly its search and browsing features are intuitive and “right” for the domain. I find it a lot easier to browse through hierachies and leap between classes and packages using Jude’s dedicated tools than via Google. It has also replaced the not inconsiderable heft of Java in a Nutshell from my work bag.

If I could make one change to make it even more useful I would make the default search much more liberal than it is now. The search accepts Java RE which is great for power using but you shouldn’t have to enter /.*http.*/i to find every instance of a class with any variation of HTTP in it, you want to be able to just type “http” Google-style.

Standard
Java, Python, Scripting

Sun recruits Jython leads

I know this isn’t exactly breaking news but it is further evidence that Sun is aiming to turn the JVM in a language agnostic platform. It’s also good news for the Jython project which has suffered a long period of hibernation and which has fallen far behind CPython in terms of its compatibility.

I like Python as a language and its clarity is great for one time scripts (which never really are). I would really like it to be a full member of the JVM-compatibles.

Standard
Java, Programming

Depressing times for Java programmers

Joshua Bloch of Effective Java and Collections fame has given a powerful and in many ways depressing talk. He has probably nailed the coffin shut on BGGA with this almost clinical dissection of the flaws of grafting Closures into Java. However he has also indicated how Java is effectively at the end of the line. Sun, for perfectly good reasons, wants to maintain backwards compatibility and not throw the kitchen sink into the language. The JCP does not have to stick with that approach but this talk does effectively say that there is a limit to what can be done while keeping the language recognisably Java.

The mention of Scala is significant because a lot of the things that people want in Java are available there and other features are in JRuby. Trying to create one language with universal appeal is going to be impossible. The reason I feel depressed about all of this is because I have working with Java for years now. I really had to fight to switch to 100% Java and now its time to move on again!

It would be easier to do that if there was something that was obviously better but currently all of the candidates for a potential successor have lacked that Eureka factor where you see something that is going to make programming easier  and better and your working life a whole lot more fun. So far only Scala has really come close.

Standard