667481 members! Sign up to stay informed.

Sponsored Links


Resources

Enterprise Java
Research Library

Get Java white papers, product information, case studies and webcasts

News News News Messages: 24 Messages: 24 Messages: 24 Printer friendly Printer friendly Printer friendly Post reply Post reply Post reply XML XML XML

Introduction to using Java Persistence API in a web application

Posted by: Joseph Ottinger on December 05, 2005 DIGG
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 replies

·  Introduction to using Java Persistence API in a web application by Joseph Ottinger on Mon Dec 05 08:59:27 EST 2005
  ·  Generating tables from Beans by Matt Giacomini on Mon Dec 05 11:13:40 EST 2005
    ·  Generating tables from Beans by Matt Giacomini on Mon Dec 05 11:14:14 EST 2005
    ·  Matt, did you like the 'simple and clear nature' of this entry? by Sujan J20 on Mon Dec 05 11:59:15 EST 2005
      ·  Matt, did you like the 'simple and clear nature' of this entry? by Matt Giacomini on Mon Dec 05 12:38:54 EST 2005
    ·  Generating tables from Beans by Anshuman Purohit on Mon Dec 05 14:36:13 EST 2005
      ·  JPA 1.0 and EJB 3.0 specs by Randy Schnier on Mon Dec 05 15:08:41 EST 2005
        ·  JPA 1.0 and EJB 3.0 specs by George Jiang on Mon Dec 05 17:25:25 EST 2005
          ·  JPA 1.0 and EJB 3.0 specs by George Jiang on Mon Dec 05 17:31:58 EST 2005
          ·  JPA 1.0 and EJB 3.0 specs by George Jiang on Mon Dec 05 17:36:02 EST 2005
            ·  JPA 1.0 and EJB 3.0 specs by George Jiang on Tue Dec 06 17:35:52 EST 2005
          ·  JPA 1.0 and EJB 3.0 specs by Bostjan Dolenc on Tue Dec 06 05:47:11 EST 2005
    ·  Using Persistence API in Java SE by Mitesh Meswani on Mon Dec 05 15:23:00 EST 2005
    ·  Article vs. Hibernate EJB3? by Dan Greening on Mon Dec 05 15:43:27 EST 2005
      ·  Re: Article vs. Hibernate EJB3? by Dan Greening on Tue Dec 06 08:52:41 EST 2005
        ·  Re: Article vs. Hibernate EJB3? by Randy Schnier on Tue Dec 06 11:21:09 EST 2005
          ·  Re: Article vs. Hibernate EJB3? by Dan Greening on Tue Dec 06 16:30:33 EST 2005
            ·  Is .jar enough or do I need .par & .ejb3 etc? by Sahoo Sanjeeb on Wed Dec 07 00:11:37 EST 2005
            ·  Re: Article vs. Hibernate EJB3? by Debu Panda on Wed Dec 07 16:35:53 EST 2005
            ·  Re: Article vs. Hibernate EJB3? by Sahoo Sanjeeb on Wed Dec 07 21:06:19 EST 2005
      ·  Article vs. Hibernate EJB3? by Sujan J20 on Tue Dec 06 09:22:41 EST 2005
        ·  Article vs. Hibernate EJB3? by Matt Giacomini on Tue Dec 06 13:59:50 EST 2005
      ·  Article vs. Hibernate EJB3? by Emmanuel Bernard on Tue Dec 06 16:16:23 EST 2005
  ·  How to enable Java2DB feature in glassfish? by Sahoo Sanjeeb on Mon Dec 05 16:17:25 EST 2005
    ·  Java2DB feature now supported for all databases in glassfish by Pramod Gopinath on Tue Dec 06 20:48:47 EST 2005
  Message #192847 Post reply Post reply Post reply Go to top Go to top Go to top

Generating tables from Beans

Posted by: Matt Giacomini on December 05, 2005 in response to Message #192833
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.

  Message #192848 Post reply Post reply Post reply Go to top Go to top Go to top

Generating tables from Beans

Posted by: Matt Giacomini on December 05, 2005 in response to Message #192847
Sorry for the title I was orig going to ask a different question but changed my mind.

  Message #192858 Post reply Post reply Post reply Go to top Go to top Go to top

Matt, did you like the 'simple and clear nature' of this entry?

Posted by: Sujan J20 on December 05, 2005 in response to Message #192847
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.

  Message #192860 Post reply Post reply Post reply Go to top Go to top Go to top

Matt, did you like the 'simple and clear nature' of this entry?

Posted by: Matt Giacomini on December 05, 2005 in response to Message #192858
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 ;)

  Message #192869 Post reply Post reply Post reply Go to top Go to top Go to top

Generating tables from Beans

Posted by: Anshuman Purohit on December 05, 2005 in response to Message #192847
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

  Message #192875 Post reply Post reply Post reply Go to top Go to top Go to top

JPA 1.0 and EJB 3.0 specs

Posted by: Randy Schnier on December 05, 2005 in response to Message #192869
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

  Message #192876 Post reply Post reply Post reply Go to top Go to top Go to top

Using Persistence API in Java SE

Posted by: Mitesh Meswani on December 05, 2005 in response to Message #192847
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.

  Message #192878 Post reply Post reply Post reply Go to top Go to top Go to top

Article vs. Hibernate EJB3?

Posted by: Dan Greening on December 05, 2005 in response to Message #192847
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?

  Message #192879 Post reply Post reply Post reply Go to top Go to top Go to top

How to enable Java2DB feature in glassfish?

Posted by: Sahoo Sanjeeb on December 05, 2005 in response to Message #192833
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

  Message #192890 Post reply Post reply Post reply Go to top Go to top Go to top

JPA 1.0 and EJB 3.0 specs

Posted by: George Jiang on December 05, 2005 in response to Message #192875
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).

  Message #192891 Post reply Post reply Post reply Go to top Go to top Go to top

JPA 1.0 and EJB 3.0 specs

Posted by: George Jiang on December 05, 2005 in response to Message #192890
Especially when this is not in the interest of some "EJB3 vendors" (to give the stuff a proper name).

  Message #192892 Post reply Post reply Post reply Go to top Go to top Go to top

JPA 1.0 and EJB 3.0 specs

Posted by: George Jiang on December 05, 2005 in response to Message #192890
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!

  Message #192929 Post reply Post reply Post reply Go to top Go to top Go to top

JPA 1.0 and EJB 3.0 specs

Posted by: Bostjan Dolenc on December 06, 2005 in response to Message #192890
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.

  Message #192950 Post reply Post reply Post reply Go to top Go to top Go to top

Re: Article vs. Hibernate EJB3?

Posted by: Dan Greening on December 06, 2005 in response to Message #192878
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.

  Message #192957 Post reply Post reply Post reply Go to top Go to top Go to top

Article vs. Hibernate EJB3?

Posted by: Sujan J20 on December 06, 2005 in response to Message #192878
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.

  Message #192984 Post reply Post reply Post reply Go to top Go to top Go to top

Re: Article vs. Hibernate EJB3?

Posted by: Randy Schnier on December 06, 2005 in response to Message #192950
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

  Message #193014 Post reply Post reply Post reply Go to top Go to top Go to top

Article vs. Hibernate EJB3?

Posted by: Matt Giacomini on December 06, 2005 in response to Message #192957
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?

  Message #193030 Post reply Post reply Post reply Go to top Go to top Go to top

Article vs. Hibernate EJB3?

Posted by: Emmanuel Bernard on December 06, 2005 in response to Message #192878
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

  Message #193032 Post reply Post reply Post reply Go to top Go to top Go to top

Re: Article vs. Hibernate EJB3?

Posted by: Dan Greening on December 06, 2005 in response to Message #192984
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!

  Message #193044 Post reply Post reply Post reply Go to top Go to top Go to top

JPA 1.0 and EJB 3.0 specs

Posted by: George Jiang on December 06, 2005 in response to Message #192892
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

  Message #193049 Post reply Post reply Post reply Go to top Go to top Go to top

Java2DB feature now supported for all databases in glassfish

Posted by: Pramod Gopinath on December 06, 2005 in response to Message #192879
The bug that Sahoo had mentioned earlier has been resolved. Effective from todays build glassfish would have support for java2db for different databases.

  Message #193056 Post reply Post reply Post reply Go to top Go to top Go to top

Is .jar enough or do I need .par & .ejb3 etc?

Posted by: Sahoo Sanjeeb on December 07, 2005 in response to Message #193032
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

  Message #193165 Post reply Post reply Post reply Go to top Go to top Go to top

Re: Article vs. Hibernate EJB3?

Posted by: Debu Panda on December 07, 2005 in response to Message #193032
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

  Message #193185 Post reply Post reply Post reply Go to top Go to top Go to top

Re: Article vs. Hibernate EJB3?

Posted by: Sahoo Sanjeeb on December 07, 2005 in response to Message #193032
Have a look at why .par is not required. Comments welcome.
-- Sahoo

New content on TheServerSide.comNew content on TheServerSide.comNew content on TheServerSide.com

Dependency Injection in Java EE 6 - Part 1

Reza Rahman explores the features of the proposed JSR 299, Contexts and Dependency Injection for Java EE (CDI). When approved, it promises to be a key feature of Java EE 6. (November 2, Article)

SAML: It's Not just for Web services

SAML is an XML-based standard for exchanging authentication and authorization data between security domains. The single most important problem that SAML was created to solve is the Web browser Single Sign-On problem. Many organizations are debating whether to stay with version 1.1 or move to 2.0. This article makes observations about both options. (September 28, Article)

Programming is Also Teaching Your Team

Joe Ottinger takes a look at how people learn, and applies it to the practice of programming. He notes that understanding how people learn is an essential part of working in a programming team. (September 22, Article)

Can Java EE Deliver The Asynchronous Web?

Stephen Maryka gave us an article about the Asynchronous Web and posed a number of questions that get examined like an approach to delivering Asynchronous Web capabilities through extensions to existing Java EE technologies. (July 14, Article)

JSF Flex

JavaServer Faces Flex goal is to provide users capability in creating standard Flex components, part of flexSDK which is open sourced through MPL license, as normal JSF components. This article by Ji Hoon Kim will provide an overview of creating a simple multilingual JSF page consisting of JSF Flex tags. (June 29, Article)

The Rules of SOA - A Road to a Successful SOA Implementation

In this session Jeff explores the key characteristics of successful SOA projects. He covers some of the patterns, and anti-patterns, tool sets, and strategies that he himself learned the hard way. Last, he provides a strategy and blueprint for achieving a high likelihood of success in your SOA project. (June 23, Tech Talk)

Ari Zilka Talks About Terracotta 3.1

Ari Zilka, CTO of Terracotta, Inc., talks about the new features in Terracotta 3.1, announced during JavaOne and available now. (June 15, Tech Talk)

Enterprise Application Integration, and Spring

In this Tech Talk, Josh Long explores an integration challenge using Spring Integration and walks through the implementation, employing and expanding on the basic patterns of Enterprise Application Integration to tie together components into a function integration solution, and then demonstrates how Spring Integration helps address the integration requirements. (June 15, Tech Talk)

Google Web Toolkit: An Introduction

In this Tech Talk, David Geary teaches you: The basics of Google Web Toolkit; How to implement Ajax-enabled applications in Java; Internationalization; Hooking into the browser history mechanism; Remote procedure calls. (June 4, Tech Talk)

Just Enough Early Architecture to Guide Development

Jon Kern discusses the best architecture/technical solutions and ensure that they are repeated by all developers. By tackling the architecture up-front in a serial manner, subsequent parallel development will be much more manageable and predictable. (May 28, Tech Talk)

Productive Programmer: On the Lam from the Furniture Police

This keynote describes the frustrations of modern knowledge workers in their quest to actually get some work done, and solutions for how to guard yourself against all those distractions. Neal Ford talks about environments, coding, acceleration, automation, and avoiding repetition as ways to defeat the misguided attempts to sap your ability to produce good work. (May 26, Tech Talk)

Auto-Scaling Your Existing Web Application

Gil demonstrates how new, aggressive uses of already abundant compute capacity by common applications offer competitive value for application designers. (May 21, Tech Talk)

Automating Hibernate Mapping and Queries For Java Web Development

Chris Keene introduces WaveMaker as a new way to automate the ability to generate Hibernate classes in order to more quickly bring OR mapping into an application. (May 19, Article)

Auto-Scaling Your Existing Web Application

In this session Nati Shalom demonstrates how to take a standard Java EE web application and scale it out or down dynamically without changes to the application code. Seeing as most web applications are over-provisioned to meet infrequent peak loads, this is a dramatic change because it enables growing your application as needed, when needed, without paying for unutilized resources. (May 19, Tech Talk)

Free Book PDF Download: Mastering EJB Third Edition

Mastering EJB was one of the original and most influential EJB books in the industry. Mastering EJB III now returns with two new expert co-authors, updated for EJB 2.1 and 30% new chapters including security, integration, best practices, open source, and more.
(Book PDF Download)

Application Server Matrix

The Application Server Matrix is a detailed listing of J2EE vendors and their application server products, with information on latest version numbers, J2EE spec support and licensing, pricing, platform support, and links to product downloads and reviews.
(Application Server Comparison Matrix)

News | Blogs | Discussions | Tech talks | Patterns | Reviews | White Papers | Downloads | Articles | Media kit | About
Java Solutions
All Content Copyright ©2007 TheServerSide Privacy Policy
Site Map