Introduction to using Java Persistence API in a web application

Discussions

News: Introduction to using Java Persistence API in a web application

  1. Sanjeeb Kumar has written up a walkthrough for using EJB3 persistence in a simple web application. While this has been done elsewhere (and referenced on TSS and other sites), this entry is interesting because of its simple and clear nature and references, which make it easy to follow.

    Other interesting facets: it uses the Glassfish EJB3 reference implementation (as opposed to Hibernate 3, although it'd be interesting to see if the use of Hibernate changed anything in the walkthrough), and also references Java2DB, a tool provided in Glassfish that can create a database schema from a set of entity beans.

    Threaded Messages (24)

  2. Generating tables from Beans[ Go to top ]

    First let me say that I have read the spec and built a couple very small sample applications myself. Based on my expirence so far I really like EJB and think that the people involved did a great job bringing it together.

    I'm also sure that once Rod and co. build a wrapper for EJB3 that the spring people will be fine with it too ;)

    My question is weather there are plans for javax.persistence to become part of Java SE? It seems to me that all of Java could benefit from this API not just application server based applications.
  3. Generating tables from Beans[ Go to top ]

    Sorry for the title I was orig going to ask a different question but changed my mind.
  4. First let me say that
    Stop - You have to. First, I will let you say whether you liked the 'simple and clear nature' and 'easy to follow feature' of this article. Then, you may proceed with your usual question(s).

    TSS has done and is doing a superb job of highlighting the very good work of 'Sanjeeb Kumar' (here) and of 'Binod P.G' (http://www.theserverside.com/news/thread.tss?thread_id=37863#192695).

    Nice!!! <= this is admiration for TSS for its job of coverage. Ok. And Thank you.
  5. First, I will let you say whether you liked the 'simple and clear nature' and 'easy to follow feature' of this article.

    ha ha ha Sorry but I can't say, I didn't bother reading it. I'm just taking advantage of the fact that this post relates to EJB3 ;)
  6. Generating tables from Beans[ Go to top ]

    Yes. If I recollect it right, Linda (JSR 220 EJB3 Spec Lead) mentioned in JavaPolis 2004 that this API is going to be part of JSE as well.

    So, you are right (and community believes) that all of Java could benifit from this API.

    --Anshuman
  7. JPA 1.0 and EJB 3.0 specs[ Go to top ]

    The Java Persistence API spec (JPA 1.0) is actually a separate and distinct spec from EJB 3.0, and EJB 3.0 refers to JPA for its POJO persistence model. One reason the two were separated into individual specs (though produced by the same expert group) was precisely so JPA could become part of JavaSE at some point. (There was a very conscientous effort to make sure that JPA, as defined in the spec, could be used in either SE or EE environments.) It also helps separate the concepts of the persistence model (JPA) from the component execution model (EJB3). This is good, since these two models being smashed together in EntityBeans was one of the factors EB's started to be viewed as 'heavyweight'.

    Randy Schnier
    IBM, member JSR 220 expert group
  8. JPA 1.0 and EJB 3.0 specs[ Go to top ]

    Hey, the name EJB3 has been overly sold to the developer community as the POJO persistence solution for so long, it is too late to correct the mistake. Even though the acronym POJO itself was coined by Martine Fowler et al to make using plain Java object instead of heavyweight EJB more sexy (thus more attractive to developers who want to be sexy).
  9. JPA 1.0 and EJB 3.0 specs[ Go to top ]

    Especially when this is not in the interest of some "EJB3 vendors" (to give the stuff a proper name).
  10. JPA 1.0 and EJB 3.0 specs[ Go to top ]

    Even though the acronym POJO itself was coined by Martine Fowler et al to make using plain Java object instead of heavyweight EJB more sexy (thus more attractive to developers who want to be sexy).

    Five years ago!
  11. JPA 1.0 and EJB 3.0 specs[ Go to top ]

    Even though the acronym POJO itself was coined by Martine Fowler et al to make using plain Java object instead of heavyweight EJB more sexy (thus more attractive to developers who want to be sexy).
    Five years ago!

    Here it is:

    http://www.martinfowler.com/bliki/POJO.html
  12. JPA 1.0 and EJB 3.0 specs[ Go to top ]

    Hey, the name EJB3 has been overly sold to the developer community as the POJO persistence solution for so long, it is too late to correct the mistake.
    OTOH POJO persistence has been oversold, too. After having to choose between 5+ MB of generated SQL queries or writing a separate finder for each access case in a proprietary language with shabby tool support, plain old SQL doesn't look that bad anymore.
  13. Using Persistence API in Java SE[ Go to top ]

    My question is weather there are plans for javax.persistence to become part of Java SE? It seems to me that all of Java could benefit from this API not just application server based applications.
    This might not be the direct answer to your question.
    It is possible to use the Persistence API from a JavaSE application even today. Look at https://glassfish.dev.java.net/javaee5/persistence/entity-persistence-support.html#Java_SE
    for a description.
  14. Article vs. Hibernate EJB3?[ Go to top ]

    I have implemented an EJB3 persistence unit, I've read most of the released documents regarding EJB3 persistence, from both Sun and JBoss, and I have these comments.

    1. Very nice, clear and helpful article. Thank you Sanjeeb.

    2. Hibernate-EJB3 docs reference a ".ejb3" file which encompasses the persistence unit. Sanjeeb implies this file can be in a simple .jar file. Let me say "massive thank you" to whoever is responsible, if in fact EJB3s can be captured in a simple JAR. The packaging issues for ".ejb3" were going to be pretty annoying.

    3. The em.find method was not well documented in Hibernate-EJB3 docs, so it was nice to see it used here.

    4. I didn't know that @Id was required, and that it was mentioned in the article saved me some time.

    5. Just a plug for the Hibernate-EJB3 folks: That microcontainer is way-nice for debugging these EJB3 entity beans.

    What can we do to encourage the release of this great persistence technology?
  15. Re: Article vs. Hibernate EJB3?[ Go to top ]

    For what it's worth, I meant ".par" not ".ejb3".

    QUESTION: Is it true that ".par" is no longer necessary to indicate a persistence archive? (Please.) The presence of a persistence.xml file should be enough to trigger examining annotations, and the extra deployment complexity of dealing with .par using Maven, etc. will hinder adoption.
  16. Re: Article vs. Hibernate EJB3?[ Go to top ]

    QUESTION: Is it true that ".par" is no longer necessary to indicate a persistence archive? (Please.) The presence of a persistence.xml file should be enough to trigger examining annotations, and the extra deployment complexity of dealing with .par using Maven, etc. will hinder adoption.

    This is correct. ".par" as a packaging identifier was proposed in one of the earlier versions of the spec, but in the latest spec version, any .jar file containing a persistence.xml file identifies to the EE runtime that the .jar should be examined for entity annotations and xml persistence attributes. (The persistence.xml file identifies which classes within the .jar belong to each Persistence Unit defined for that jar.) In an SE environment, the classes that define the persistence unit are identified to the EntityManager through config parms passed in when the EntityManager is created.

    Randy
  17. Re: Article vs. Hibernate EJB3?[ Go to top ]

    QUESTION: Is it true that ".par" is no longer necessary to indicate a persistence archive? (Please.) The presence of a persistence.xml file should be enough to trigger examining annotations, and the extra deployment complexity of dealing with .par using Maven, etc. will hinder adoption.
    This is correct. ".par" as a packaging identifier was proposed in one of the earlier versions of the spec, but in the latest spec version, any .jar file containing a persistence.xml file identifies to the EE runtime that the .jar should be examined for entity annotations and xml persistence attributes. (The persistence.xml file identifies which classes within the .jar belong to each Persistence Unit defined for that jar.) In an SE environment, the classes that define the persistence unit are identified to the EntityManager through config parms passed in when the EntityManager is created.Randy

    Is this also true for the .ejb3 suffix for Session Beans? Does JBoss support .jar suffix for EJB3 Session Beans? Let me continue this highly moral author trend and say that I declare my undying love to those responsible, if .jar is a fine suffix for EJB3 Session Beans!
  18. Is this also true for the .ejb3 suffix for Session Beans?

    Yes, it is true for EJB3 session beans as well. It is implemented in glassfish project.
    .ejb3 was always a JBoss specific thing, never got into the spec. .par was introduced in the EJB3 spec. But it was subsequently removed.

    --Sahoo
  19. Re: Article vs. Hibernate EJB3?[ Go to top ]

    Is this also true for the .ejb3 suffix for Session Beans? Does JBoss support .jar suffix for EJB3 Session Beans? Let me continue this highly moral author trend and say that I declare my undying love to those responsible, if .jar is a fine suffix for EJB3 Session Beans!

    Oracle supports this in its EJB3 preview since February 2005
    http://www.oracle.com/technology/tech/java/ejb30.html

    -Debu
    http://debupanda.com
  20. Re: Article vs. Hibernate EJB3?[ Go to top ]

    Have a look at why .par is not required. Comments welcome.
    -- Sahoo
  21. Article vs. Hibernate EJB3?[ Go to top ]

    Thank you, Dan. You possess great moral codes to approach authors. Not everyone possesses what you do in this direction.

    You illustrated a good method when publishing. I wish, we all illustrate the same (good method) (here on TSS) when posting questions.

    Good.
  22. Article vs. Hibernate EJB3?[ Go to top ]

    Thank you, Dan. You possess great moral codes to approach authors. Not everyone possesses what you do in this direction.You illustrated a good method when publishing. I wish, we all illustrate the same (good method) (here on TSS) when posting questions.Good.

    What are you the TTS etiquette cheer learder?
  23. Article vs. Hibernate EJB3?[ Go to top ]

    Some comments online
    2. Hibernate-EJB3 docs reference a ".ejb3" file which encompasses the persistence unit. Sanjeeb implies this file can be in a simple .jar file. Let me say "massive thank you" to whoever is responsible, if in fact EJB3s can be captured in a simple JAR. The packaging issues for ".ejb3" were going to be pretty annoying.

    This is already (and has always been) implemented in Hibernate Entity Manager (ie the simple jar stuff). I should make it clearer from the doc.
    3. The em.find method was not well documented in Hibernate-EJB3 docs, so it was nice to see it used here.
    I don't see much more than UserCredential credential = em.find(UserCredential.class, name); in Sanjeeb article which is more or less the same kind of information that in HEM docs? http://www.hibernate.org/hib_docs/entitymanager/reference/en/html_single/#d0e499

    What kind of information were you expecting? Any proposal welcome :-)
    4. I didn't know that @Id was required, and that it was mentioned in the article saved me some time.
    A property(ies) holding the id is required, not @Id (in the last public spec). You can have @Id / @EmbeddedId on a property or even @IdClass at the class level

    Emmanuel
  24. In my original posting, I mentioned that because of a bug, Java2DB (i.e. automatic table creation during deployment) feature does not work in glassfish for Derby database. Very soon, the bug is going to be fixed. It is right now only working for Oracle database. To enable it, either

    1) add a property in persistence.xml under <properties> tag as follows:
    <property name="ddl-generation" value="createtables"/>

    OR

    2) Invoke asadmin deploy command with an additional option -createtables=true. So the complete command is:

    $GLASSFISH_HOME/bin/asadmin deploy --user <username> --password <password> --createtables=true <path to jar/war/ear>

    Watch out for updates at Persistence RI project page to know when this feature is available for all databases.

    --Sahoo
  25. The bug that Sahoo had mentioned earlier has been resolved. Effective from todays build glassfish would have support for java2db for different databases.