03/09/2007

Coding java persistence: JPA vs. Hibernate

In my current projects, I deal with database persistence layer. I use Hibernate implementation of the JPA 1.0 API. But restrictions of JPA API limits my ability to use some cool features of Hibernate, including


  1. Full control over cascade behavior;

  2. Indexed collections;

  3. Collections of primitive values.


"Use JPA when possible, and Hibernate extensions where you can't do without" is a reasonable solution, but... It smells.... Anyway, the code becomes not portable enough when using Hibernate extensions. May be, it would be better to skip JPA and relay on Plain Old Hibernate (POH)?



XML Config vs. Annotations: Prons and Cons


JPA and Hibernate both supports XML mapping config and annotation config.


XML does not require to import annotations in entity classes. It makes entities more portable. They stay a simple POJOs, without any extra dependencies. It makes possible to package them in separate jar and include as a dependency in the client tier.


Annotations make mapping config to live together with property declarations in java file. It is more readable to the developer. It make sense when you don't plan to expose your entities outside the persistence module.

3 comments:

  1. Annotations are equally portable, when annotatios are not found in the classpath, they are simply ignored by Java. So you can deploy your domain model without the ejb3 annotations jar.

    ReplyDelete
  2. I faced the problem with javac compiler (Sun JDK 1.5.0_12) when dependent annotation classes were not in a classpath. So I had to add as JPA 1.0 dependency.

    ReplyDelete
  3. Error from java compiler when annotation class was not found in classpath:

    [INFO] Compilation failure
    Failure executing javac, but could not parse the error:
    An exception has occurred in the compiler (1.5.0_07). Please file a bug at the Java Developer Connection (http://java.su
    n.com/webapps/bugreport) after checking the Bug Parade for duplicates. Include your program and the following diagnosti
    c in your report. Thank you.
    com.sun.tools.javac.code.Symbol$CompletionFailure: file org\hibernate\annotations\ForeignKey.class not found

    ReplyDelete

redirect