Amazon deals

Wednesday, February 11, 2009

The dreaded null comes back

The null pointer exceptions are dreadfully insane in Java. Take this one for example, the EntityManagerFactory returned null. You go on to a thousand forums on the net to find a solution to this problem and you find out one this, the JPA is relatively a newbie on the experienced programming world and the closest problem could be that you've not the given the correct name for the persistence unit. Well i'll try to list out a max of probable errors you need to correct to solve this problem...

1. Use the same name while defining the persistence unit in the persistence.xml file and while denoting the @Persistence

2. Add the META-inf folder that contains this xml fle in yor classpath if its not already in the classpath. The persistence.xml must be in the classpath for the entities to be able to recognise your back end database and connect to it.

3. Check the data type of entity parameters. Are they what you really expect? JPA has a weird habit of using non-conventional data types such as short for representing columns with numbers that are of range low enough to be represented by this datatype. But they'll mess you up when you try to connect to databases like oracle that don't support this. So you may need to look into the auto generated codes a little bit.

4. You do the above and still no gain? Yo might have foreign keys in your database. Drop those keys, the JPA doesn't always work well when you don't have well defined foreign keys around in your tables.

If you are still scratching your heads, then EJB's are not for you. Definitely you'll even find some books titled Why not to use EJB's. But this technology has certainly revolutionized database based applications especially with the way we perceive entities and query them.