{"id":29255,"date":"2014-08-27T19:00:06","date_gmt":"2014-08-27T16:00:06","guid":{"rendered":"http:\/\/www.javacodegeeks.com\/?p=29255"},"modified":"2015-08-28T10:58:21","modified_gmt":"2015-08-28T07:58:21","slug":"jpa-tutorial-setting-up-jpa-in-a-java-se-environment","status":"publish","type":"post","link":"https:\/\/www.javacodegeeks.com\/2014\/08\/jpa-tutorial-setting-up-jpa-in-a-java-se-environment.html","title":{"rendered":"JPA Tutorial \u2013 Setting Up JPA in a Java SE Environment"},"content":{"rendered":"<p>JPA stands for Java Persistence API, which basically is a specification that\u00a0describes a way to persist\u00a0data into a persistent storage, usually a database. We\u00a0can think of it as something similar to\u00a0<a href=\"http:\/\/en.wikipedia.org\/wiki\/Object-relational_mapping\">ORM<\/a> tools like <a href=\"http:\/\/hibernate.org\/\">Hibernate<\/a>, except that it is an official part of the Java EE specification (and it\u2019s also supported on Java SE). There are many reasons to learn an ORM tool like JPA. I will not go into the details of this because there are already many posts on the web which perfectly answer this question, like <a href=\"http:\/\/stackoverflow.com\/questions\/4406310\/why-use-jpa-instead-of-directly-writing-sql-query-on-java-file-i-e-directly-to\" target=\"_blank\">this one<\/a>,\u00a0or <a href=\"http:\/\/en.wikibooks.org\/wiki\/Java_Persistence\/Why_use_JPA_or_ORM%3F\">this one<\/a>. However, we should also keep in mind that <a href=\"http:\/\/stackoverflow.com\/a\/494853\/245679\"> this is not a single magic bullet<\/a> which will solve our\u00a0every problem.<br \/>\nWhen I first started out with JPA, I had real difficulties to set it up because most of the articles on the web are written for Java EE environment only, whereas I was trying to use it in a Java SE environment. I hope that this article will be helpful for those who wish to do the same in the future.<br \/>\nIn this example we will use Maven to set up our required dependencies. Since JPA is only a specification, we will also need an implementation. There are many good implementations of JPA available freely (like EclipseLink, Hibernate etc.). For this article I have chosen to\u00a0use Hibernate. As for the database, I\u00a0will use MySQL. Let us first create a simple maven project. I have created mine\u00a0using the quick start archetype from the command line. If you do not know how to do that, you can follow <a href=\"http:\/\/www.mkyong.com\/maven\/how-to-create-a-java-project-with-maven\/\" target=\"_blank\">this tutorial<\/a>.<br \/>\nOK, so let us get the dependencies for the JPA next. Include the following lines in your pom.xml:<\/p>\n<pre class=\" brush:xml\">&lt;dependency&gt;\r\n  &lt;groupId&gt;javax.persistence&lt;\/groupId&gt;\r\n  &lt;artifactId&gt;persistence-api&lt;\/artifactId&gt;\r\n  &lt;version&gt;1.0.2&lt;\/version&gt;\r\n&lt;\/dependency&gt;\r\n&lt;dependency&gt;\r\n  &lt;groupId&gt;org.hibernate&lt;\/groupId&gt;\r\n  &lt;artifactId&gt;hibernate-entitymanager&lt;\/artifactId&gt;\r\n  &lt;version&gt;4.3.6.Final&lt;\/version&gt;\r\n  &lt;exclusions&gt;\r\n    &lt;exclusion&gt;\r\n      &lt;groupId&gt;org.hibernate.javax.persistence&lt;\/groupId&gt;\r\n      &lt;artifactId&gt;hibernate-jpa-2.1-api&lt;\/artifactId&gt;\r\n    &lt;\/exclusion&gt;\r\n  &lt;\/exclusions&gt;\r\n&lt;\/dependency&gt;<\/pre>\n<p>The first dependency specifies the standard JPA interface, and the second one specifies the implementation. Including JPA dependencies this way is desirable because it gives us the freedom to switch vendor-specific implementation in the future without much problem (<a href=\"http:\/\/stackoverflow.com\/a\/21523840\/245679\" target=\"_blank\">see details here<\/a>). However we will not be able to use the latest version of the API this way because the API version 1.0.2 is the last version that is\u00a0released as an independent JAR. At the time of writing this article, the latest version of the JPA specification is 2.1 which is not available independently (there are <a href=\"https:\/\/java.net\/jira\/browse\/JPA_SPEC-19\" target=\"_blank\">lots<\/a> of <a href=\"https:\/\/java.net\/jira\/browse\/JPA_SPEC-60\" target=\"_blank\">requests<\/a>\u00a0for it though). If we want to use that one now then our only options are to choose from either a vendor-specific JAR or use an application server which provides the API along with its implementation. I have decided to use the API specification provided by Hibernate. In that case including only the following dependency will suffice:<\/p>\n<pre class=\" brush:xml\">&lt;dependency&gt;\r\n  &lt;groupId&gt;org.hibernate&lt;\/groupId&gt;\r\n  &lt;artifactId&gt;hibernate-entitymanager&lt;\/artifactId&gt;\r\n  &lt;version&gt;4.3.6.Final&lt;\/version&gt;\r\n&lt;\/dependency&gt;<\/pre>\n<p>Next step is to include the dependency for MySQL. Include the following lines in your pom.xml:<\/p>\n<pre class=\" brush:xml\">&lt;dependency&gt;\r\n  &lt;groupId&gt;mysql&lt;\/groupId&gt;\r\n  &lt;artifactId&gt;mysql-connector-java&lt;\/artifactId&gt;\r\n  &lt;version&gt;5.1.31&lt;\/version&gt;\r\n&lt;\/dependency&gt;<\/pre>\n<p>After including the rest of the dependencies (i.e., jUnit, Hamcrest etc.) the full pom.xml looks like below:<\/p>\n<pre class=\" brush:xml;wrap-lines:false\">&lt;project xmlns=\"http:\/\/maven.apache.org\/POM\/4.0.0\" xmlns:xsi=\"http:\/\/www.w3.org\/2001\/XMLSchema-instance\"\r\n  xsi:schemaLocation=\"http:\/\/maven.apache.org\/POM\/4.0.0 http:\/\/maven.apache.org\/xsd\/maven-4.0.0.xsd\"&gt;\r\n\r\n  &lt;modelVersion&gt;4.0.0&lt;\/modelVersion&gt;\r\n\r\n  &lt;groupId&gt;com.keertimaan.javasamples&lt;\/groupId&gt;\r\n  &lt;artifactId&gt;jpa-example&lt;\/artifactId&gt;\r\n  &lt;version&gt;0.0.1-SNAPSHOT&lt;\/version&gt;\r\n  &lt;packaging&gt;jar&lt;\/packaging&gt;\r\n\r\n  &lt;name&gt;jpa-example&lt;\/name&gt;\r\n  &lt;url&gt;http:\/\/www.codesod.com&lt;\/url&gt;\r\n\r\n  &lt;properties&gt;\r\n    &lt;java.version&gt;1.8&lt;\/java.version&gt;\r\n    &lt;hibernate.version&gt;4.3.6.Final&lt;\/hibernate.version&gt;\r\n    &lt;project.build.sourceEncoding&gt;UTF-8&lt;\/project.build.sourceEncoding&gt;\r\n  &lt;\/properties&gt;\r\n\r\n  &lt;dependencies&gt;\r\n    &lt;!-- JPA --&gt;\r\n    &lt;dependency&gt;\r\n      &lt;groupId&gt;org.hibernate&lt;\/groupId&gt;\r\n      &lt;artifactId&gt;hibernate-entitymanager&lt;\/artifactId&gt;\r\n      &lt;version&gt;${hibernate.version}&lt;\/version&gt;\r\n    &lt;\/dependency&gt;\r\n\r\n    &lt;!-- For connection pooling --&gt;\r\n    &lt;dependency&gt;\r\n      &lt;groupId&gt;org.hibernate&lt;\/groupId&gt;\r\n      &lt;artifactId&gt;hibernate-c3p0&lt;\/artifactId&gt;\r\n      &lt;version&gt;${hibernate.version}&lt;\/version&gt;\r\n    &lt;\/dependency&gt;\r\n\r\n    &lt;!-- Database --&gt;\r\n    &lt;dependency&gt;\r\n      &lt;groupId&gt;mysql&lt;\/groupId&gt;\r\n      &lt;artifactId&gt;mysql-connector-java&lt;\/artifactId&gt;\r\n      &lt;version&gt;5.1.31&lt;\/version&gt;\r\n    &lt;\/dependency&gt;\r\n\r\n    &lt;!-- Test --&gt;\r\n    &lt;dependency&gt;\r\n      &lt;groupId&gt;junit&lt;\/groupId&gt;\r\n      &lt;artifactId&gt;junit&lt;\/artifactId&gt;\r\n      &lt;version&gt;4.11&lt;\/version&gt;\r\n      &lt;scope&gt;test&lt;\/scope&gt;\r\n      &lt;exclusions&gt;\r\n        &lt;exclusion&gt;\r\n          &lt;groupId&gt;org.hamcrest&lt;\/groupId&gt;\r\n          &lt;artifactId&gt;hamcrest-core&lt;\/artifactId&gt;\r\n        &lt;\/exclusion&gt;\r\n      &lt;\/exclusions&gt;\r\n    &lt;\/dependency&gt;\r\n    &lt;dependency&gt;\r\n      &lt;groupId&gt;org.hamcrest&lt;\/groupId&gt;\r\n      &lt;artifactId&gt;hamcrest-all&lt;\/artifactId&gt;\r\n      &lt;version&gt;1.3&lt;\/version&gt;\r\n      &lt;scope&gt;test&lt;\/scope&gt;\r\n    &lt;\/dependency&gt;\r\n  &lt;\/dependencies&gt;\r\n\r\n  &lt;build&gt;\r\n    &lt;plugins&gt;\r\n      &lt;plugin&gt;\r\n        &lt;groupId&gt;org.apache.maven.plugins&lt;\/groupId&gt;\r\n        &lt;artifactId&gt;maven-compiler-plugin&lt;\/artifactId&gt;\r\n        &lt;version&gt;2.5.1&lt;\/version&gt;\r\n        &lt;configuration&gt;\r\n          &lt;source&gt;${java.version}&lt;\/source&gt;\r\n          &lt;target&gt;${java.version}&lt;\/target&gt;\r\n          &lt;compilerArgument&gt;-Xlint:all&lt;\/compilerArgument&gt;\r\n          &lt;showWarnings&gt;true&lt;\/showWarnings&gt;\r\n          &lt;showDeprecation&gt;true&lt;\/showDeprecation&gt;\r\n        &lt;\/configuration&gt;\r\n      &lt;\/plugin&gt;\r\n    &lt;\/plugins&gt;\r\n  &lt;\/build&gt;\r\n&lt;\/project&gt;<\/pre>\n<p>Now it\u2019s time to configure our database. I will use the following schema in all of my future JPA examples which I found from <a href=\"http:\/\/en.wikibooks.org\/wiki\/Java_Persistence\" target=\"_blank\">this excellent online book<\/a>:<div style=\"display:inline-block; margin: 15px 0;\"> <div id=\"adngin-JavaCodeGeeks_incontent_video-0\" style=\"display:inline-block;\"><\/div> <\/div><\/p>\n<p><figure id=\"attachment_29452\" aria-describedby=\"caption-attachment-29452\" style=\"width: 300px\" class=\"wp-caption aligncenter\"><a href=\"http:\/\/www.javacodegeeks.com\/wp-content\/uploads\/2014\/08\/schema.png\"><img decoding=\"async\" class=\"size-medium wp-image-29452\" src=\"http:\/\/www.javacodegeeks.com\/wp-content\/uploads\/2014\/08\/schema-300x185.png\" alt=\"Database Schema\" width=\"300\" height=\"185\" srcset=\"https:\/\/www.javacodegeeks.com\/wp-content\/uploads\/2014\/08\/schema-300x185.png 300w, https:\/\/www.javacodegeeks.com\/wp-content\/uploads\/2014\/08\/schema.png 796w\" sizes=\"(max-width: 300px) 100vw, 300px\" \/><\/a><figcaption id=\"caption-attachment-29452\" class=\"wp-caption-text\">Database Schema<\/figcaption><\/figure><\/p>\n<p>&nbsp;<br \/>\nCreate an equivalent database following the above schema\u00a0in your local MySQL installation. Our next step is to create the <em>persistence.xml<\/em> file which will contain our database specific information for JPA to use. By default JPA expects this file to be in the class path under the <em>META-INF<\/em> folder. For\u00a0our maven project, I have created this file under <em>project_root\/<\/em><em>src\/main\/resources\/META-INF<\/em> folder:<\/p>\n<pre class=\" brush:xml;wrap-lines:false\">&lt;persistence xmlns=\"http:\/\/xmlns.jcp.org\/xml\/ns\/persistence\" xmlns:xsi=\"http:\/\/www.w3.org\/2001\/XMLSchema-instance\"\r\n  xsi:schemaLocation=\"http:\/\/xmlns.jcp.org\/xml\/ns\/persistence\r\n\r\nhttp:\/\/xmlns.jcp.org\/xml\/ns\/persistence\/persistence_2_1.xsd\"\r\n\r\n  version=\"2.1\"&gt;\r\n\r\n  &lt;persistence-unit name=\"jpa-example\" transaction-type=\"RESOURCE_LOCAL\"&gt;\r\n  &lt;provider&gt;org.hibernate.jpa.HibernatePersistenceProvider&lt;\/provider&gt;\r\n\r\n  &lt;properties&gt;\r\n    &lt;property name=\"javax.persistence.jdbc.url\" value=\"jdbc:mysql:\/\/localhost\/jpa_example\" \/&gt;\r\n    &lt;property name=\"javax.persistence.jdbc.user\" value=\"root\" \/&gt;\r\n    &lt;property name=\"javax.persistence.jdbc.password\" value=\"my_root_password\" \/&gt;\r\n    &lt;property name=\"javax.persistence.jdbc.driver\" value=\"com.mysql.jdbc.Driver\" \/&gt;\r\n\r\n    &lt;property name=\"hibernate.show_sql\" value=\"true\" \/&gt;\r\n    &lt;property name=\"hibernate.format_sql\" value=\"true\" \/&gt;\r\n    &lt;property name=\"hibernate.dialect\" value=\"org.hibernate.dialect.MySQL5InnoDBDialect\" \/&gt;\r\n    &lt;property name=\"hibernate.hbm2ddl.auto\" value=\"validate\" \/&gt;\r\n\r\n    &lt;!-- Configuring Connection Pool --&gt;\r\n    &lt;property name=\"hibernate.c3p0.min_size\" value=\"5\" \/&gt;\r\n    &lt;property name=\"hibernate.c3p0.max_size\" value=\"20\" \/&gt;\r\n    &lt;property name=\"hibernate.c3p0.timeout\" value=\"500\" \/&gt;\r\n    &lt;property name=\"hibernate.c3p0.max_statements\" value=\"50\" \/&gt;\r\n    &lt;property name=\"hibernate.c3p0.idle_test_period\" value=\"2000\" \/&gt;\r\n    &lt;\/properties&gt;\r\n  &lt;\/persistence-unit&gt;\r\n&lt;\/persistence&gt;<\/pre>\n<p>The above file requires some explanation if you are an absolute begineer in JPA. In my next article I will try to explain it as much as possible, but for running this example you will only need to change the first three property values to match your environment (namely the database name, username and password). Also keep a note of the value of the\u00a0<em>name<\/em> attribute of the <em>persistence-unit<\/em> element. This value will be used to instantiate our EntityManagerFactory instance later in the code.<br \/>\nOk, let us now create an entity to test our configuration. Create a class called <em>Address<\/em> with the following contents:<\/p>\n<pre class=\" brush:java\">import javax.persistence.Entity;\r\nimport javax.persistence.GeneratedValue;\r\nimport javax.persistence.Id;\r\nimport javax.persistence.Table;\r\n\r\n@Entity\r\n@Table(name = \"address\")\r\npublic class Address {\r\n  @Id\r\n  @GeneratedValue\r\n  private Integer id;\r\n\r\n  private String street;\r\n  private String city;\r\n  private String province;\r\n  private String country;\r\n  private String postcode;\r\n\r\n  \/**\r\n   * @return the id\r\n   *\/\r\n  public Integer getId() {\r\n    return id;\r\n  }\r\n\r\n  \/**\r\n   * @param id the id to set\r\n   *\/\r\n  public Address setId(Integer id) {\r\n    this.id = id;\r\n    return this;\r\n  }\r\n\r\n  \/**\r\n   * @return the street\r\n   *\/\r\n  public String getStreet() {\r\n    return street;\r\n  }\r\n\r\n  \/**\r\n   * @param street the street to set\r\n   *\/\r\n  public Address setStreet(String street) {\r\n    this.street = street;\r\n    return this;\r\n  }\r\n\r\n  \/**\r\n   * @return the city\r\n   *\/\r\n  public String getCity() {\r\n    return city;\r\n  }\r\n\r\n  \/**\r\n   * @param city the city to set\r\n   *\/\r\n  public Address setCity(String city) {\r\n    this.city = city;\r\n    return this;\r\n  }\r\n\r\n  \/**\r\n   * @return the province\r\n   *\/\r\n  public String getProvince() {\r\n    return province;\r\n  }\r\n\r\n  \/**\r\n   * @param province the province to set\r\n   *\/\r\n  public Address setProvince(String province) {\r\n    this.province = province;\r\n    return this;\r\n  }\r\n\r\n  \/**\r\n   * @return the country\r\n   *\/\r\n  public String getCountry() {\r\n    return country;\r\n  }\r\n\r\n  \/**\r\n   * @param country the country to set\r\n   *\/\r\n  public Address setCountry(String country) {\r\n    this.country = country;\r\n    return this;\r\n  }\r\n\r\n  \/**\r\n   * @return the postcode\r\n   *\/\r\n  public String getPostcode() {\r\n    return postcode;\r\n  }\r\n\r\n  \/**\r\n   * @param postcode the postcode to set\r\n   *\/\r\n  public Address setPostcode(String postcode) {\r\n    this.postcode = postcode;\r\n    return this;\r\n  }\r\n}<\/pre>\n<p>This class has been properly mapped to the <em>address<\/em> table and its instances are fully ready to be persisted in the database. Now let us create a helper class called <em>PersistenceManager<\/em> with the following contents:<\/p>\n<pre class=\" brush:java\">import javax.persistence.EntityManager;\r\nimport javax.persistence.EntityManagerFactory;\r\nimport javax.persistence.Persistence;\r\n\r\npublic enum PersistenceManager {\r\n  INSTANCE;\r\n\r\n  private EntityManagerFactory emFactory;\r\n\r\n  private PersistenceManager() {\r\n    \/\/ \"jpa-example\" was the value of the name attribute of the\r\n    \/\/ persistence-unit element.\r\n    emFactory = Persistence.createEntityManagerFactory(\"jpa-example\");\r\n  }\r\n\r\n  public EntityManager getEntityManager() {\r\n    return emFactory.createEntityManager();\r\n  }\r\n\r\n  public void close() {\r\n    emFactory.close();\r\n  }\r\n}<\/pre>\n<p>Now let us write some sample persistence code in our Main method to test everything out:<\/p>\n<pre class=\" brush:java\">import javax.persistence.EntityManager;\r\n\r\npublic class Main {\r\n  public static void main(String[] args) {\r\n    Address address = new Address();\r\n    address.setCity(\"Dhaka\")\r\n        .setCountry(\"Bangladesh\")\r\n        .setPostcode(\"1000\")\r\n        .setStreet(\"Poribagh\");\r\n\r\n    EntityManager em = PersistenceManager.INSTANCE.getEntityManager();\r\n    em.getTransaction()\r\n        .begin();\r\n    em.persist(address);\r\n    em.getTransaction()\r\n        .commit();\r\n\r\n    em.close();\r\n    PersistenceManager.INSTANCE.close();\r\n  }\r\n}<\/pre>\n<p>If you check your database, you will see that a new record has been inserted in your\u00a0<em>address<\/em> table. This article explains how to set up JPA without using any other frameworks like Spring. However it is a very good idea to use Spring to set up JPA because in that case we do not need to worry about managing entity managers, transactions etc. Beside setting up JPA, spring is also very good <a href=\"http:\/\/programmers.stackexchange.com\/q\/92393\" target=\"_blank\">for many other purposes<\/a>\u00a0too. That\u2019s it for today. In the next article I will try to explain the <em>persistence.xml<\/em> file and the corresponding configuration values as much as possible. Stay tuned! <\/p>\n<ul>\n<li>The full code can be found at <a href=\"https:\/\/github.com\/sayembd\/JavaSamples\/tree\/master\/jpa-example\" target=\"_blank\">github<\/a>.<\/li>\n<\/ul>\n<div class=\"attribution\">\n<table>\n<tbody>\n<tr>\n<td><span class=\"reference\">Reference: <\/span><\/td>\n<td><a href=\"http:\/\/www.codesod.com\/2014\/08\/15\/jpa-tutorial-setting-up-jpa-in-a-java-se-environment\/\">JPA Tutorial \u2013 Setting Up JPA in a Java SE Environment<\/a> from our <a href=\"http:\/\/www.javacodegeeks.com\/jcg\">JCG partner<\/a> Sayem Ahmed at the <a href=\"http:\/\/www.codesod.com\/\">Codesod<\/a> blog.<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<\/div>\n","protected":false},"excerpt":{"rendered":"<p>JPA stands for Java Persistence API, which basically is a specification that\u00a0describes a way to persist\u00a0data into a persistent storage, usually a database. We\u00a0can think of it as something similar to\u00a0ORM tools like Hibernate, except that it is an official part of the Java EE specification (and it\u2019s also supported on Java SE). There are &hellip;<\/p>\n","protected":false},"author":474,"featured_media":112,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[8],"tags":[33],"class_list":["post-29255","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-enterprise-java","tag-jpa"],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v27.5 - https:\/\/yoast.com\/product\/yoast-seo-wordpress\/ -->\n<title>JPA Tutorial \u2013 Setting Up JPA in a Java SE Environment<\/title>\n<meta name=\"description\" content=\"JPA stands for Java Persistence API, which basically is a specification that\u00a0describes a way to persist\u00a0data into a persistent storage, usually a\" \/>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/www.javacodegeeks.com\/2014\/08\/jpa-tutorial-setting-up-jpa-in-a-java-se-environment.html\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"JPA Tutorial \u2013 Setting Up JPA in a Java SE Environment\" \/>\n<meta property=\"og:description\" content=\"JPA stands for Java Persistence API, which basically is a specification that\u00a0describes a way to persist\u00a0data into a persistent storage, usually a\" \/>\n<meta property=\"og:url\" content=\"https:\/\/www.javacodegeeks.com\/2014\/08\/jpa-tutorial-setting-up-jpa-in-a-java-se-environment.html\" \/>\n<meta property=\"og:site_name\" content=\"Java Code Geeks\" \/>\n<meta property=\"article:publisher\" content=\"https:\/\/www.facebook.com\/javacodegeeks\" \/>\n<meta property=\"article:published_time\" content=\"2014-08-27T16:00:06+00:00\" \/>\n<meta property=\"article:modified_time\" content=\"2015-08-28T07:58:21+00:00\" \/>\n<meta property=\"og:image\" content=\"https:\/\/www.javacodegeeks.com\/wp-content\/uploads\/2012\/10\/enterprise-java-logo.jpg\" \/>\n\t<meta property=\"og:image:width\" content=\"150\" \/>\n\t<meta property=\"og:image:height\" content=\"150\" \/>\n\t<meta property=\"og:image:type\" content=\"image\/jpeg\" \/>\n<meta name=\"author\" content=\"MD Sayem Ahmed\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:creator\" content=\"@say3mbd\" \/>\n<meta name=\"twitter:site\" content=\"@javacodegeeks\" \/>\n<meta name=\"twitter:label1\" content=\"Written by\" \/>\n\t<meta name=\"twitter:data1\" content=\"MD Sayem Ahmed\" \/>\n\t<meta name=\"twitter:label2\" content=\"Est. reading time\" \/>\n\t<meta name=\"twitter:data2\" content=\"10 minutes\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\\\/\\\/schema.org\",\"@graph\":[{\"@type\":\"Article\",\"@id\":\"https:\\\/\\\/www.javacodegeeks.com\\\/2014\\\/08\\\/jpa-tutorial-setting-up-jpa-in-a-java-se-environment.html#article\",\"isPartOf\":{\"@id\":\"https:\\\/\\\/www.javacodegeeks.com\\\/2014\\\/08\\\/jpa-tutorial-setting-up-jpa-in-a-java-se-environment.html\"},\"author\":{\"name\":\"MD Sayem Ahmed\",\"@id\":\"https:\\\/\\\/www.javacodegeeks.com\\\/#\\\/schema\\\/person\\\/c28c3b2d8f198300d25627addda7df17\"},\"headline\":\"JPA Tutorial \u2013 Setting Up JPA in a Java SE Environment\",\"datePublished\":\"2014-08-27T16:00:06+00:00\",\"dateModified\":\"2015-08-28T07:58:21+00:00\",\"mainEntityOfPage\":{\"@id\":\"https:\\\/\\\/www.javacodegeeks.com\\\/2014\\\/08\\\/jpa-tutorial-setting-up-jpa-in-a-java-se-environment.html\"},\"wordCount\":886,\"commentCount\":4,\"publisher\":{\"@id\":\"https:\\\/\\\/www.javacodegeeks.com\\\/#organization\"},\"image\":{\"@id\":\"https:\\\/\\\/www.javacodegeeks.com\\\/2014\\\/08\\\/jpa-tutorial-setting-up-jpa-in-a-java-se-environment.html#primaryimage\"},\"thumbnailUrl\":\"https:\\\/\\\/www.javacodegeeks.com\\\/wp-content\\\/uploads\\\/2012\\\/10\\\/enterprise-java-logo.jpg\",\"keywords\":[\"JPA\"],\"articleSection\":[\"Enterprise Java\"],\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"CommentAction\",\"name\":\"Comment\",\"target\":[\"https:\\\/\\\/www.javacodegeeks.com\\\/2014\\\/08\\\/jpa-tutorial-setting-up-jpa-in-a-java-se-environment.html#respond\"]}]},{\"@type\":\"WebPage\",\"@id\":\"https:\\\/\\\/www.javacodegeeks.com\\\/2014\\\/08\\\/jpa-tutorial-setting-up-jpa-in-a-java-se-environment.html\",\"url\":\"https:\\\/\\\/www.javacodegeeks.com\\\/2014\\\/08\\\/jpa-tutorial-setting-up-jpa-in-a-java-se-environment.html\",\"name\":\"JPA Tutorial \u2013 Setting Up JPA in a Java SE Environment\",\"isPartOf\":{\"@id\":\"https:\\\/\\\/www.javacodegeeks.com\\\/#website\"},\"primaryImageOfPage\":{\"@id\":\"https:\\\/\\\/www.javacodegeeks.com\\\/2014\\\/08\\\/jpa-tutorial-setting-up-jpa-in-a-java-se-environment.html#primaryimage\"},\"image\":{\"@id\":\"https:\\\/\\\/www.javacodegeeks.com\\\/2014\\\/08\\\/jpa-tutorial-setting-up-jpa-in-a-java-se-environment.html#primaryimage\"},\"thumbnailUrl\":\"https:\\\/\\\/www.javacodegeeks.com\\\/wp-content\\\/uploads\\\/2012\\\/10\\\/enterprise-java-logo.jpg\",\"datePublished\":\"2014-08-27T16:00:06+00:00\",\"dateModified\":\"2015-08-28T07:58:21+00:00\",\"description\":\"JPA stands for Java Persistence API, which basically is a specification that\u00a0describes a way to persist\u00a0data into a persistent storage, usually a\",\"breadcrumb\":{\"@id\":\"https:\\\/\\\/www.javacodegeeks.com\\\/2014\\\/08\\\/jpa-tutorial-setting-up-jpa-in-a-java-se-environment.html#breadcrumb\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\\\/\\\/www.javacodegeeks.com\\\/2014\\\/08\\\/jpa-tutorial-setting-up-jpa-in-a-java-se-environment.html\"]}]},{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\\\/\\\/www.javacodegeeks.com\\\/2014\\\/08\\\/jpa-tutorial-setting-up-jpa-in-a-java-se-environment.html#primaryimage\",\"url\":\"https:\\\/\\\/www.javacodegeeks.com\\\/wp-content\\\/uploads\\\/2012\\\/10\\\/enterprise-java-logo.jpg\",\"contentUrl\":\"https:\\\/\\\/www.javacodegeeks.com\\\/wp-content\\\/uploads\\\/2012\\\/10\\\/enterprise-java-logo.jpg\",\"width\":150,\"height\":150,\"caption\":\"java-interview-questions-answers\"},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\\\/\\\/www.javacodegeeks.com\\\/2014\\\/08\\\/jpa-tutorial-setting-up-jpa-in-a-java-se-environment.html#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Home\",\"item\":\"https:\\\/\\\/www.javacodegeeks.com\\\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"Java\",\"item\":\"https:\\\/\\\/www.javacodegeeks.com\\\/category\\\/java\"},{\"@type\":\"ListItem\",\"position\":3,\"name\":\"Enterprise Java\",\"item\":\"https:\\\/\\\/www.javacodegeeks.com\\\/category\\\/java\\\/enterprise-java\"},{\"@type\":\"ListItem\",\"position\":4,\"name\":\"JPA Tutorial \u2013 Setting Up JPA in a Java SE Environment\"}]},{\"@type\":\"WebSite\",\"@id\":\"https:\\\/\\\/www.javacodegeeks.com\\\/#website\",\"url\":\"https:\\\/\\\/www.javacodegeeks.com\\\/\",\"name\":\"Java Code Geeks\",\"description\":\"Java Developers Resource Center\",\"publisher\":{\"@id\":\"https:\\\/\\\/www.javacodegeeks.com\\\/#organization\"},\"alternateName\":\"JCG\",\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\\\/\\\/www.javacodegeeks.com\\\/?s={search_term_string}\"},\"query-input\":{\"@type\":\"PropertyValueSpecification\",\"valueRequired\":true,\"valueName\":\"search_term_string\"}}],\"inLanguage\":\"en-US\"},{\"@type\":\"Organization\",\"@id\":\"https:\\\/\\\/www.javacodegeeks.com\\\/#organization\",\"name\":\"Exelixis Media P.C.\",\"url\":\"https:\\\/\\\/www.javacodegeeks.com\\\/\",\"logo\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\\\/\\\/www.javacodegeeks.com\\\/#\\\/schema\\\/logo\\\/image\\\/\",\"url\":\"https:\\\/\\\/www.javacodegeeks.com\\\/wp-content\\\/uploads\\\/2022\\\/06\\\/exelixis-logo.png\",\"contentUrl\":\"https:\\\/\\\/www.javacodegeeks.com\\\/wp-content\\\/uploads\\\/2022\\\/06\\\/exelixis-logo.png\",\"width\":864,\"height\":246,\"caption\":\"Exelixis Media P.C.\"},\"image\":{\"@id\":\"https:\\\/\\\/www.javacodegeeks.com\\\/#\\\/schema\\\/logo\\\/image\\\/\"},\"sameAs\":[\"https:\\\/\\\/www.facebook.com\\\/javacodegeeks\",\"https:\\\/\\\/x.com\\\/javacodegeeks\"]},{\"@type\":\"Person\",\"@id\":\"https:\\\/\\\/www.javacodegeeks.com\\\/#\\\/schema\\\/person\\\/c28c3b2d8f198300d25627addda7df17\",\"name\":\"MD Sayem Ahmed\",\"image\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\\\/\\\/secure.gravatar.com\\\/avatar\\\/cb149440e74e234a74de8359fb6164f93a29b69a2a1980b6cebe5afd7f841fa7?s=96&d=mm&r=g\",\"url\":\"https:\\\/\\\/secure.gravatar.com\\\/avatar\\\/cb149440e74e234a74de8359fb6164f93a29b69a2a1980b6cebe5afd7f841fa7?s=96&d=mm&r=g\",\"contentUrl\":\"https:\\\/\\\/secure.gravatar.com\\\/avatar\\\/cb149440e74e234a74de8359fb6164f93a29b69a2a1980b6cebe5afd7f841fa7?s=96&d=mm&r=g\",\"caption\":\"MD Sayem Ahmed\"},\"description\":\"Sayem is an experienced software developer who loves to work with anything related to the internet. He has worked in various domains using a large number of programming languages. Although he specially likes to work with Java and JavaScript, he enjoys working with other languages too.\",\"sameAs\":[\"https:\\\/\\\/www.sayemahmed.com\\\/\",\"https:\\\/\\\/www.linkedin.com\\\/in\\\/sayem64\",\"https:\\\/\\\/x.com\\\/say3mbd\"],\"url\":\"https:\\\/\\\/www.javacodegeeks.com\\\/author\\\/sayem-ahmed\"}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"JPA Tutorial \u2013 Setting Up JPA in a Java SE Environment","description":"JPA stands for Java Persistence API, which basically is a specification that\u00a0describes a way to persist\u00a0data into a persistent storage, usually a","robots":{"index":"index","follow":"follow","max-snippet":"max-snippet:-1","max-image-preview":"max-image-preview:large","max-video-preview":"max-video-preview:-1"},"canonical":"https:\/\/www.javacodegeeks.com\/2014\/08\/jpa-tutorial-setting-up-jpa-in-a-java-se-environment.html","og_locale":"en_US","og_type":"article","og_title":"JPA Tutorial \u2013 Setting Up JPA in a Java SE Environment","og_description":"JPA stands for Java Persistence API, which basically is a specification that\u00a0describes a way to persist\u00a0data into a persistent storage, usually a","og_url":"https:\/\/www.javacodegeeks.com\/2014\/08\/jpa-tutorial-setting-up-jpa-in-a-java-se-environment.html","og_site_name":"Java Code Geeks","article_publisher":"https:\/\/www.facebook.com\/javacodegeeks","article_published_time":"2014-08-27T16:00:06+00:00","article_modified_time":"2015-08-28T07:58:21+00:00","og_image":[{"width":150,"height":150,"url":"https:\/\/www.javacodegeeks.com\/wp-content\/uploads\/2012\/10\/enterprise-java-logo.jpg","type":"image\/jpeg"}],"author":"MD Sayem Ahmed","twitter_card":"summary_large_image","twitter_creator":"@say3mbd","twitter_site":"@javacodegeeks","twitter_misc":{"Written by":"MD Sayem Ahmed","Est. reading time":"10 minutes"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"Article","@id":"https:\/\/www.javacodegeeks.com\/2014\/08\/jpa-tutorial-setting-up-jpa-in-a-java-se-environment.html#article","isPartOf":{"@id":"https:\/\/www.javacodegeeks.com\/2014\/08\/jpa-tutorial-setting-up-jpa-in-a-java-se-environment.html"},"author":{"name":"MD Sayem Ahmed","@id":"https:\/\/www.javacodegeeks.com\/#\/schema\/person\/c28c3b2d8f198300d25627addda7df17"},"headline":"JPA Tutorial \u2013 Setting Up JPA in a Java SE Environment","datePublished":"2014-08-27T16:00:06+00:00","dateModified":"2015-08-28T07:58:21+00:00","mainEntityOfPage":{"@id":"https:\/\/www.javacodegeeks.com\/2014\/08\/jpa-tutorial-setting-up-jpa-in-a-java-se-environment.html"},"wordCount":886,"commentCount":4,"publisher":{"@id":"https:\/\/www.javacodegeeks.com\/#organization"},"image":{"@id":"https:\/\/www.javacodegeeks.com\/2014\/08\/jpa-tutorial-setting-up-jpa-in-a-java-se-environment.html#primaryimage"},"thumbnailUrl":"https:\/\/www.javacodegeeks.com\/wp-content\/uploads\/2012\/10\/enterprise-java-logo.jpg","keywords":["JPA"],"articleSection":["Enterprise Java"],"inLanguage":"en-US","potentialAction":[{"@type":"CommentAction","name":"Comment","target":["https:\/\/www.javacodegeeks.com\/2014\/08\/jpa-tutorial-setting-up-jpa-in-a-java-se-environment.html#respond"]}]},{"@type":"WebPage","@id":"https:\/\/www.javacodegeeks.com\/2014\/08\/jpa-tutorial-setting-up-jpa-in-a-java-se-environment.html","url":"https:\/\/www.javacodegeeks.com\/2014\/08\/jpa-tutorial-setting-up-jpa-in-a-java-se-environment.html","name":"JPA Tutorial \u2013 Setting Up JPA in a Java SE Environment","isPartOf":{"@id":"https:\/\/www.javacodegeeks.com\/#website"},"primaryImageOfPage":{"@id":"https:\/\/www.javacodegeeks.com\/2014\/08\/jpa-tutorial-setting-up-jpa-in-a-java-se-environment.html#primaryimage"},"image":{"@id":"https:\/\/www.javacodegeeks.com\/2014\/08\/jpa-tutorial-setting-up-jpa-in-a-java-se-environment.html#primaryimage"},"thumbnailUrl":"https:\/\/www.javacodegeeks.com\/wp-content\/uploads\/2012\/10\/enterprise-java-logo.jpg","datePublished":"2014-08-27T16:00:06+00:00","dateModified":"2015-08-28T07:58:21+00:00","description":"JPA stands for Java Persistence API, which basically is a specification that\u00a0describes a way to persist\u00a0data into a persistent storage, usually a","breadcrumb":{"@id":"https:\/\/www.javacodegeeks.com\/2014\/08\/jpa-tutorial-setting-up-jpa-in-a-java-se-environment.html#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/www.javacodegeeks.com\/2014\/08\/jpa-tutorial-setting-up-jpa-in-a-java-se-environment.html"]}]},{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/www.javacodegeeks.com\/2014\/08\/jpa-tutorial-setting-up-jpa-in-a-java-se-environment.html#primaryimage","url":"https:\/\/www.javacodegeeks.com\/wp-content\/uploads\/2012\/10\/enterprise-java-logo.jpg","contentUrl":"https:\/\/www.javacodegeeks.com\/wp-content\/uploads\/2012\/10\/enterprise-java-logo.jpg","width":150,"height":150,"caption":"java-interview-questions-answers"},{"@type":"BreadcrumbList","@id":"https:\/\/www.javacodegeeks.com\/2014\/08\/jpa-tutorial-setting-up-jpa-in-a-java-se-environment.html#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https:\/\/www.javacodegeeks.com\/"},{"@type":"ListItem","position":2,"name":"Java","item":"https:\/\/www.javacodegeeks.com\/category\/java"},{"@type":"ListItem","position":3,"name":"Enterprise Java","item":"https:\/\/www.javacodegeeks.com\/category\/java\/enterprise-java"},{"@type":"ListItem","position":4,"name":"JPA Tutorial \u2013 Setting Up JPA in a Java SE Environment"}]},{"@type":"WebSite","@id":"https:\/\/www.javacodegeeks.com\/#website","url":"https:\/\/www.javacodegeeks.com\/","name":"Java Code Geeks","description":"Java Developers Resource Center","publisher":{"@id":"https:\/\/www.javacodegeeks.com\/#organization"},"alternateName":"JCG","potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/www.javacodegeeks.com\/?s={search_term_string}"},"query-input":{"@type":"PropertyValueSpecification","valueRequired":true,"valueName":"search_term_string"}}],"inLanguage":"en-US"},{"@type":"Organization","@id":"https:\/\/www.javacodegeeks.com\/#organization","name":"Exelixis Media P.C.","url":"https:\/\/www.javacodegeeks.com\/","logo":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/www.javacodegeeks.com\/#\/schema\/logo\/image\/","url":"https:\/\/www.javacodegeeks.com\/wp-content\/uploads\/2022\/06\/exelixis-logo.png","contentUrl":"https:\/\/www.javacodegeeks.com\/wp-content\/uploads\/2022\/06\/exelixis-logo.png","width":864,"height":246,"caption":"Exelixis Media P.C."},"image":{"@id":"https:\/\/www.javacodegeeks.com\/#\/schema\/logo\/image\/"},"sameAs":["https:\/\/www.facebook.com\/javacodegeeks","https:\/\/x.com\/javacodegeeks"]},{"@type":"Person","@id":"https:\/\/www.javacodegeeks.com\/#\/schema\/person\/c28c3b2d8f198300d25627addda7df17","name":"MD Sayem Ahmed","image":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/secure.gravatar.com\/avatar\/cb149440e74e234a74de8359fb6164f93a29b69a2a1980b6cebe5afd7f841fa7?s=96&d=mm&r=g","url":"https:\/\/secure.gravatar.com\/avatar\/cb149440e74e234a74de8359fb6164f93a29b69a2a1980b6cebe5afd7f841fa7?s=96&d=mm&r=g","contentUrl":"https:\/\/secure.gravatar.com\/avatar\/cb149440e74e234a74de8359fb6164f93a29b69a2a1980b6cebe5afd7f841fa7?s=96&d=mm&r=g","caption":"MD Sayem Ahmed"},"description":"Sayem is an experienced software developer who loves to work with anything related to the internet. He has worked in various domains using a large number of programming languages. Although he specially likes to work with Java and JavaScript, he enjoys working with other languages too.","sameAs":["https:\/\/www.sayemahmed.com\/","https:\/\/www.linkedin.com\/in\/sayem64","https:\/\/x.com\/say3mbd"],"url":"https:\/\/www.javacodegeeks.com\/author\/sayem-ahmed"}]}},"_links":{"self":[{"href":"https:\/\/www.javacodegeeks.com\/wp-json\/wp\/v2\/posts\/29255","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.javacodegeeks.com\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.javacodegeeks.com\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.javacodegeeks.com\/wp-json\/wp\/v2\/users\/474"}],"replies":[{"embeddable":true,"href":"https:\/\/www.javacodegeeks.com\/wp-json\/wp\/v2\/comments?post=29255"}],"version-history":[{"count":0,"href":"https:\/\/www.javacodegeeks.com\/wp-json\/wp\/v2\/posts\/29255\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.javacodegeeks.com\/wp-json\/wp\/v2\/media\/112"}],"wp:attachment":[{"href":"https:\/\/www.javacodegeeks.com\/wp-json\/wp\/v2\/media?parent=29255"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.javacodegeeks.com\/wp-json\/wp\/v2\/categories?post=29255"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.javacodegeeks.com\/wp-json\/wp\/v2\/tags?post=29255"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}