Wednesday, December 13, 2006

J2ME connectivity with a server app... Ooops I did it again

Recently I was looking at connecting my MIDlet with a backend application running in Sun AS 9, of course developed in NetBeans as well and since I am not really sure whether or not WS are supported by the phones the MIDlet will run on, I generated, well I had NB generate the web connectivity. Really cool stuff.
Then I wanted it all to work, ie send data from the phone to the backend and have the data stored in a database, Java DB included by NB. To get some grips on the whole Java EE 5 thing, I had NB generate a CRUD frontend for the entity classes and I looked at the generated code. This is actually rather clean code and very comprehensive indeed. But from the NB generated servlet unusable. It took me some digging in the Java EE 5 tutorial to find out how to get it all up an running with PersistenceUnits, Resources, annotations and transactions, but I got it all running in the end. I added the annotation of the Persistence Unit to the generated servlet and changed the EndToEndSupport class (I'm not really sure that it is called exactly that way, but it was generated by NB during the generation of the backend connectivity). The change included providing the EntityManager obtained in the servlet to the actual code that is to do the persistency. This was about 2 minutes of work and I was thrilled to see it all working.

Now the "Ooops I did it again" comes into play. I changed the backend functionality a bit, or rather I moved some classes and added some features which wasn't a problem until I had NB generate the code again so I could use the new backend functionality from the MIDlet. All code I had changed to get the persistence working was gone. Fortunately I remembered what I had done, and it all was still in SVN, so not too much harm done.

And this is when I started wondering about where that checkbox is that asks me whether or not I want to use the JPA in the backend I am connecting to from my MIDlet and when checked it will generate all the code for actually persist data send across.... or maybe I am not looking in the right direction to read all about it. Anyway, the tutorial on mobility assumes a working webapp, the web tutorial assumes JSF to be used and it doesn't all come together in one tutorial... maybe I'll document my efforts more detailed, until then: Drop me an email if you get stuck on this.


No comments: