JPA CRUD example

The following two tabs change content below.
Prasad Kharkar is a java enthusiast and always keen to explore and learn java technologies. He is SCJP,OCPWCD, OCEJPAD and aspires to be java architect.

Latest posts by Prasad Kharkar (see all)

Hello all, In previous article we have learned about the jpa architecture. This article will focus on creating a simple JPA CRUD example.

JPA CRUD example:

1. Create a JPA project.

Create a JPA project in eclipse with the name JPAExamples.

2. Create database.

We need some database to store data from jpa entities. Create a database named thejavageek In our example, We will create a table employee with columns idemployee, firstname, lastname and email.

3. Employee entity:

Create a class Employee in the package com.thejavageek.jpa.entities  and annotate with  @Entity  and @Idso that it becomes an entity that is managed by EntityManager .

4. persistence.xml:

As soon as you created jpa project in eclipse, a file called persistence.xml is created which stores the information about jpa. Update it as follows. Also, add the mysql driver jar file into your classpath of project.

5. jpa crud example operations:

Now that we have created Employee entity and updated persistence.xml for jpa configuration, we can perform operations on entities. Create a class Test in package com.thejavageek.jpa  as follows:

Your final project structure would be as follows:

projectStructure

Now simply run this program and you will see following output on the console

I hope this JPA CRUD example helped!!!

References for further study :

Share Button

Prasad Kharkar

Prasad Kharkar is a java enthusiast and always keen to explore and learn java technologies. He is SCJP,OCPWCD, OCEJPAD and aspires to be java architect.

19 thoughts on “JPA CRUD example

  • Pingback:Automatic ID creation using jpa table generator | theJavaGeek

  • April 3, 2015 at 8:10 pm
    Permalink

    As you know “Hibernate” does create a Table by using the following property,

    create

    But I don’t see a way getting it in JPA, Could you please guide ?

    Reply
  • April 3, 2015 at 8:13 pm
    Permalink

    I don’t know why its not accepting these syntax, I ‘m talking about property name=”hbm2ddl.auto set to create

    Reply
    • April 4, 2015 at 3:54 pm
      Permalink

      Hi Prateek, JPA by default does not provide any way to do this. It is implementation specific. For hibernate you need to set hbm2ddl.auto as true, for eclipseLink has property “eclipselink.ddl-generation”. All implementations have different properties.

      Reply
  • April 4, 2015 at 12:09 am
    Permalink

    Could you please provide maven version of this project ?

    Reply
    • April 4, 2015 at 3:54 pm
      Permalink

      Hi Prateek, Right now I do not have maven version. I will provide as soon as I create it.

      Reply
      • February 14, 2017 at 1:21 pm
        Permalink

        you are not aythorizied to tell lie to the developers..?

        Reply
        • February 14, 2017 at 4:35 pm
          Permalink

          Hi Bob, thank you for your comment. Could you please correct me where I am wrong?

          Reply
  • October 10, 2016 at 3:06 pm
    Permalink

    I just copied the code and created the project using Eclipse Mars as defined in the tutorial. I am using persistence-api-1.0.jar and Eclipselink2.5.x and the following error:
    Exception in thread “main” javax.persistence.PersistenceException: No Persistence provider for EntityManager named JPAExamples

    please help me …

    Reply
    • October 19, 2016 at 12:43 am
      Permalink

      add the following line after in the persistence.xml file:

      org.eclipse.persistence.jpa.PersistenceProvider

      Reply
      • October 19, 2016 at 12:46 am
        Permalink

        xml was removed from the previous reply. after persistence-unit add the property provider with the following value org.eclipse.persistence.jpa.PersistenceProvider

        That should take care of your error.

        Reply
  • September 1, 2017 at 5:24 am
    Permalink

    Hi,
    thanks a lot for this article!
    Is there a possibility to see the sql output?
    I am asking this thing because is not clear in my mind who is the class involved to insert data.

    Thanks
    Andrea

    Reply
    • September 4, 2017 at 2:38 pm
      Permalink

      Hi Andrea,

      As you might be aware that JPA is a specification and implementing frameworks do all the work. The class can be implementation specific. We can use log levels to check sql output.

      Reply
  • April 4, 2018 at 10:23 am
    Permalink

    Hi,
    Nice, short and useful article.
    I’m trying to run the standalone JPA application, but getting following error:
    : No Persistence provider for EntityManager named JsfJpaExample
    while creating the EntityManagerFactory.

    I tried by explicitly stating the JPA provider in persistence.xml file
    org.hibernate.jpa.HibernatePersistenceProvider
    Still getting same error.
    Any idea how can this error be resolved.
    Thanks,
    Mahesh

    Reply
      • April 8, 2018 at 4:49 pm
        Permalink

        Hi,
        As mentioned, creating a standalone JPA App. Its solved now, some hibernate jar files were missing in the class path. Thanks.

        Reply

Leave a Reply

Your email address will not be published. Required fields are marked *