{"id":51688,"date":"2017-10-31T11:00:39","date_gmt":"2017-10-31T09:00:39","guid":{"rendered":"http:\/\/examples.javacodegeeks.com\/?p=51688"},"modified":"2019-02-27T13:54:58","modified_gmt":"2019-02-27T11:54:58","slug":"jpa-mappedby-example","status":"publish","type":"post","link":"https:\/\/examples.javacodegeeks.com\/java-development\/enterprise-java\/jpa\/jpa-mappedby-example\/","title":{"rendered":"JPA mappedBy Example"},"content":{"rendered":"<p>Hello readers, in this tutorial, we will show how to implement the <span style=\"text-decoration: underline;\">mappedBy<\/span> annotation in JPA using EclipseLink and MySQL in Java.<br \/>\n&nbsp;<br \/>\n&nbsp;<br \/>\n&nbsp;<br \/>\n&nbsp;<br \/>\n&nbsp;<br \/>\n&nbsp;<br \/>\n&nbsp;<br \/>\n&nbsp;<br \/>\n&nbsp;<br \/>\n&nbsp;\n<\/p>\n<h2>1. Introduction<\/h2>\n<p><strong>Java Persistence API<\/strong> (JPA), is a standard interface which wraps the different Object Relational Mapping (ORM) tools such as Hibernate, EclipseLink, OpenJPA etc. JPA provides a <code>javax.persistence.EntityManager<\/code> interface which is used to interact with the database. The instance of <code>EntityManager<\/code> plays around the persistence context and the <code>EntityManagerFactory<\/code> interacts with the <code>EntityManager<\/code> object.<\/p>\n<ul>\n<li><strong>Persistence Context<\/strong> is the set of entity instances where for any persistence entity identity, there is a unique entity instance. The lifecycle of entity instances is managed within the persistence context using the <code>EntityManager<\/code>. We can detach and merge the entity instances within a persistence context<\/li>\n<li><strong>Entity Manager<\/strong> is a model borrowed from the traditional JDBC frameworks i.e. making it easier for the developers to perform the basic database operations with a very little code<\/li>\n<\/ul>\n<p>In this standalone JPA example, we are using EclipseLink with MySQL Database. <a href=\"http:\/\/www.eclipse.org\/eclipselink\/\" target=\"_blank\" rel=\"noopener noreferrer\"><strong>EclipseLink<\/strong><\/a> is a popular open source ORM (Object Relation Mapping) tool for Java platform used for mapping an entity to a traditional relational database like Oracle, MySQL etc.<\/p>\n<h3>1.1 JPA Benefits<\/h3>\n<p>There are many advantages of using the JPA framework, for e.g.<\/p>\n<ul>\n<li>The benefit of using the JPA framework over any specific Object Relational Model (ORM) related libraries like Hibernate, iBatis etc. is that developers do not change the code when they change the vendor<\/li>\n<li>The code is loosely coupled with the underlying ORM framework<\/li>\n<li>Improves data security and data access to the users by using host and query languages<\/li>\n<li>Improves application performance by reducing the data redundancy<\/li>\n<li>Greater data integrity and independence of applications programs<\/li>\n<li>Provides simple querying of data<\/li>\n<\/ul>\n<h3>1.2 How it can be achieved?<\/h3>\n<p>Programmers can achieve persistence in their application by introducing the <code>persistence.xml<\/code> in their code, which has to be located in the <code>META-INF<\/code> directory in the project classpath. One <code>persistence.xml<\/code> file can <span style=\"text-decoration: underline;\">include definitions for one or more persistence units<\/span>. This file plays a crucial role in the concept of JPA as in this configuration file, developers will register the database and specify the entity class. Let\u2019s take a look and understand the sample code.<\/p>\n<p><span style=\"text-decoration: underline;\"><em>Sample persistence.xml<\/em><\/span><\/p>\n<pre class=\"brush:xml;\">&lt;?xml version=\"1.0\" encoding=\"UTF-8\"?&gt;\n&lt;persistence version=\"2.1\"\n\txmlns=\"http:\/\/xmlns.jcp.org\/xml\/ns\/persistence\" xmlns:xsi=\"http:\/\/www.w3.org\/2001\/XMLSchema-instance\"\n\txsi:schemaLocation=\"http:\/\/xmlns.jcp.org\/xml\/ns\/persistence http:\/\/xmlns.jcp.org\/xml\/ns\/persistence\/persistence_2_1.xsd\"&gt;\n\t&lt;persistence-unit name=\"TestPersistence\" transaction-type=\"RESOURCE_LOCAL\"&gt;\n\t\t&lt;class&gt;&lt;!-- Entity Manager Class Name --&gt;&lt;\/class&gt;\n\t\t&lt;properties&gt;\n\t\t\t&lt;property name=\"javax.persistence.jdbc.driver\" value=\"Database Driver Name\" \/&gt;\n\t\t\t&lt;property name=\"javax.persistence.jdbc.url\" value=\"Database Url\" \/&gt;\n\t\t\t&lt;property name=\"javax.persistence.jdbc.user\" value=\"Database Username\" \/&gt;\n\t\t\t&lt;property name=\"javax.persistence.jdbc.password\" value=\"Database Password\" \/&gt;\n\t\t&lt;\/properties&gt;\n\t&lt;\/persistence-unit&gt;\n&lt;\/persistence&gt;\n<\/pre>\n<p>The <code>persistence.xml<\/code> file indicates that there is only one Persistence Unit mapped with the name <strong>TestPersistence<\/strong> and the transaction type for this Persistence Unit is <code>RESOURCE_LOCAL<\/code>. There are two types of transactions:<\/p>\n<ul>\n<li>JTA<\/li>\n<li>RESOURCE_LOCAL<\/li>\n<\/ul>\n<p>If developers select the <code>RESOURCE_LOCAL<\/code>, then the transaction will be managed by the JPA Provider Implementation in use. If JTA is specified, then the transactions will be managed by the Application Server.<\/p>\n<p>Do remember, if a developer only wants to have JPA transactions, then <code>RESOURCE_LOCAL<\/code> is a good choice. But, if a developer would like the transactions to contain resources other than JPA, like EJBs, JMS then <code>JTA<\/code> is the correct choice.<\/p>\n<h3>1.3 Download &amp; Install EclipseLink<\/h3>\n<p>You can watch <a href=\"https:\/\/www.youtube.com\/watch?v=OzZcW7XgiPA\" target=\"_blank\" rel=\"noopener noreferrer\">this<\/a> video in order to download and install the JPA in Eclipse via the EclipseLink.<\/p>\n<h3>1.4 Download &amp; Install MySQL<\/h3>\n<p>You can watch <a href=\"\/\/www.youtube.com\/watch?v=z1t9m7K6cpI\u201d\" target=\"\u201d_blank\u201d\" rel=\"noopener noreferrer\">this<\/a> video in order to download and install the MySQL database on your Windows Operating system.<\/p>\n<p>Now, open up the Eclipse Ide and let\u2019s see how to implement the <code>mappedBy<\/code> annotation in JPA framework.<\/p>\n<h2>2. Java mappedBy Example<\/h2>\n<h3>2.1 Tools Used<\/h3>\n<p>We are using Eclipse Kepler SR2, JDK 8, MySQL and Maven. Having said that, we have tested the code against JDK 1.7 and it works well.<\/p>\n<h3>2.2 Project Structure<\/h3>\n<p>Firstly, let\u2019s review the final project structure, in case you are confused about where you should create the corresponding files or folder later!<\/p>\n<p><figure id=\"attachment_51689\" aria-describedby=\"caption-attachment-51689\" style=\"width: 236px\" class=\"wp-caption aligncenter\"><a href=\"http:\/\/examples.javacodegeeks.com\/wp-content\/uploads\/2017\/10\/java-mapped-by-example-project-structure-guide-1.jpg\"><img decoding=\"async\" class=\"size-full wp-image-51689\" src=\"http:\/\/examples.javacodegeeks.com\/wp-content\/uploads\/2017\/10\/java-mapped-by-example-project-structure-guide-1.jpg\" alt=\"Fig. 1: Application\u2019s Project Structure\" width=\"236\" height=\"324\" srcset=\"https:\/\/examples.javacodegeeks.com\/wp-content\/uploads\/2017\/10\/java-mapped-by-example-project-structure-guide-1.jpg 236w, https:\/\/examples.javacodegeeks.com\/wp-content\/uploads\/2017\/10\/java-mapped-by-example-project-structure-guide-1-219x300.jpg 219w\" sizes=\"(max-width: 236px) 100vw, 236px\" \/><\/a><figcaption id=\"caption-attachment-51689\" class=\"wp-caption-text\">Fig. 1: Application\u2019s Project Structure<\/figcaption><\/figure><\/p>\n<h3>2.3 Project Creation<\/h3>\n<p>This section will demonstrate on how to create a Java-based Maven project with Eclipse. In Eclipse IDE, go to <code>File -&gt; New -&gt; Maven Project<\/code>.<\/p>\n<p><figure id=\"attachment_51690\" aria-describedby=\"caption-attachment-51690\" style=\"width: 651px\" class=\"wp-caption aligncenter\"><a href=\"http:\/\/examples.javacodegeeks.com\/wp-content\/uploads\/2017\/10\/java-mapped-by-example-project-guide-1.jpg\"><img decoding=\"async\" class=\"size-full wp-image-51690\" src=\"http:\/\/examples.javacodegeeks.com\/wp-content\/uploads\/2017\/10\/java-mapped-by-example-project-guide-1.jpg\" alt=\"Fig. 2: Create Maven Project\" width=\"651\" height=\"608\" srcset=\"https:\/\/examples.javacodegeeks.com\/wp-content\/uploads\/2017\/10\/java-mapped-by-example-project-guide-1.jpg 651w, https:\/\/examples.javacodegeeks.com\/wp-content\/uploads\/2017\/10\/java-mapped-by-example-project-guide-1-300x280.jpg 300w\" sizes=\"(max-width: 651px) 100vw, 651px\" \/><\/a><figcaption id=\"caption-attachment-51690\" class=\"wp-caption-text\">Fig. 2: Create Maven Project<\/figcaption><\/figure><\/p>\n<p>In the New Maven Project window, it will ask you to select a project location. By default, &#8216;Use default workspace location&#8217; will be selected. Select the &#8216;Create a simple project (skip archetype selection)&#8217; checkbox and just click on next button to proceed.<\/p>\n<p><figure id=\"attachment_51691\" aria-describedby=\"caption-attachment-51691\" style=\"width: 804px\" class=\"wp-caption aligncenter\"><a href=\"http:\/\/examples.javacodegeeks.com\/wp-content\/uploads\/2017\/10\/java-mapped-by-example-project-guide-2.jpg\"><img decoding=\"async\" class=\"size-full wp-image-51691\" src=\"http:\/\/examples.javacodegeeks.com\/wp-content\/uploads\/2017\/10\/java-mapped-by-example-project-guide-2.jpg\" alt=\"Fig. 3: Project Details\" width=\"804\" height=\"541\" srcset=\"https:\/\/examples.javacodegeeks.com\/wp-content\/uploads\/2017\/10\/java-mapped-by-example-project-guide-2.jpg 804w, https:\/\/examples.javacodegeeks.com\/wp-content\/uploads\/2017\/10\/java-mapped-by-example-project-guide-2-300x202.jpg 300w, https:\/\/examples.javacodegeeks.com\/wp-content\/uploads\/2017\/10\/java-mapped-by-example-project-guide-2-768x517.jpg 768w\" sizes=\"(max-width: 804px) 100vw, 804px\" \/><\/a><figcaption id=\"caption-attachment-51691\" class=\"wp-caption-text\">Fig. 3: Project Details<\/figcaption><\/figure><\/p>\n<p>It will ask you to &#8216;Enter the group and the artifact id for the project&#8217;. We will input the details as shown in the below image. The version number will be by default: <code>0.0.1-SNAPSHOT<\/code>.<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_51692\" aria-describedby=\"caption-attachment-51692\" style=\"width: 598px\" class=\"wp-caption aligncenter\"><a href=\"http:\/\/examples.javacodegeeks.com\/wp-content\/uploads\/2017\/10\/java-mapped-by-example-project-guide-3.jpg\"><img decoding=\"async\" class=\"size-full wp-image-51692\" src=\"http:\/\/examples.javacodegeeks.com\/wp-content\/uploads\/2017\/10\/java-mapped-by-example-project-guide-3.jpg\" alt=\"Fig. 4: Archetype Parameters\" width=\"598\" height=\"543\" srcset=\"https:\/\/examples.javacodegeeks.com\/wp-content\/uploads\/2017\/10\/java-mapped-by-example-project-guide-3.jpg 598w, https:\/\/examples.javacodegeeks.com\/wp-content\/uploads\/2017\/10\/java-mapped-by-example-project-guide-3-300x272.jpg 300w\" sizes=\"(max-width: 598px) 100vw, 598px\" \/><\/a><figcaption id=\"caption-attachment-51692\" class=\"wp-caption-text\">Fig. 4: Archetype Parameters<\/figcaption><\/figure><\/p>\n<p>Click on finish and the creation of a maven project will be completed. If you observe, it has downloaded the maven dependencies and a <code>pom.xml<\/code> file will be created. It will have the following code:<\/p>\n<p><span style=\"text-decoration: underline;\"><em>pom.xml<\/em><\/span><\/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\" xsi:schemaLocation=\"http:\/\/maven.apache.org\/POM\/4.0.0 http:\/\/maven.apache.org\/xsd\/maven-4.0.0.xsd\"&gt;\n\t&lt;modelVersion&gt;4.0.0&lt;\/modelVersion&gt;\n\t&lt;groupId&gt;JPAMappedbyExample&lt;\/groupId&gt;\n\t&lt;artifactId&gt;JPAMappedbyExample&lt;\/artifactId&gt;\n\t&lt;version&gt;0.0.1-SNAPSHOT&lt;\/version&gt;\n&lt;\/project&gt;\n<\/pre>\n<p>We can start adding the dependencies that developers want like Eclipse Link, MySQL Connector Jar, and Hibernate etc. Let\u2019s start building the application!<\/p>\n<h2>3. Application Building<\/h2>\n<p>Below are the steps involved in developing this application.<\/p>\n<h3>3.1 Database &amp; Table Creation<\/h3>\n<p>The following MySQL script is used to create a database called <code>jpaMappedBy<\/code> with tables: <code>EMPLOYEE_TABLE<\/code> and <code>PASSPORT_TABLE<\/code>. Open the MySQL or the workbench terminal and execute the <code>SQL<\/code> script:<\/p>\n<pre class=\"brush:sql; wrap-lines:false;\">DROP DATABASE IF EXISTS jpaMappedBy;\n  \nCREATE DATABASE IF NOT EXISTS jpaMappedBy;\n\nUSE jpaMappedBy;\n\nCREATE TABLE EMPLOYEE_TABLE(\n  EMP_ID INT NOT NULL auto_increment, \n  EMP_NAME VARCHAR(50) NOT NULL, \n  PASSPORT_NUMBER INT NOT NULL, \n  PRIMARY KEY (EMP_ID)\n);\n\nCREATE TABLE PASSPORT_TABLE(\n  PASSPORT_NUMBER INT NOT NULL auto_increment, \n  ADDRESS_LINE1 VARCHAR(100) NOT NULL, \n  ADDRESS_LINE2 VARCHAR(100) NOT NULL, \n  STATE_NAME VARCHAR(50) NOT NULL, \n  COUNTRY_NAME VARCHAR(50) NOT NULL, \n  PRIMARY KEY (PASSPORT_NUMBER)\n);\n\nDESC EMPLOYEE_TABLE;\n\nDESC PASSPORT_TABLE;\n<\/pre>\n<p>If everything goes well, the database and the table will be shown in the MySQL Workbench.<\/p>\n<p><figure id=\"attachment_51693\" aria-describedby=\"caption-attachment-51693\" style=\"width: 849px\" class=\"wp-caption aligncenter\"><a href=\"http:\/\/examples.javacodegeeks.com\/wp-content\/uploads\/2017\/10\/java-mapped-by-example-database-structure-guide-1.jpg\"><img decoding=\"async\" class=\"size-full wp-image-51693\" src=\"http:\/\/examples.javacodegeeks.com\/wp-content\/uploads\/2017\/10\/java-mapped-by-example-database-structure-guide-1.jpg\" alt=\"Fig. 5: Database &amp; Table Creation\" width=\"849\" height=\"516\" srcset=\"https:\/\/examples.javacodegeeks.com\/wp-content\/uploads\/2017\/10\/java-mapped-by-example-database-structure-guide-1.jpg 849w, https:\/\/examples.javacodegeeks.com\/wp-content\/uploads\/2017\/10\/java-mapped-by-example-database-structure-guide-1-300x182.jpg 300w, https:\/\/examples.javacodegeeks.com\/wp-content\/uploads\/2017\/10\/java-mapped-by-example-database-structure-guide-1-768x467.jpg 768w\" sizes=\"(max-width: 849px) 100vw, 849px\" \/><\/a><figcaption id=\"caption-attachment-51693\" class=\"wp-caption-text\">Fig. 5: Database &amp; Table Creation<\/figcaption><\/figure><\/p>\n<h3>3.2 Maven Dependencies<\/h3>\n<p>In this example, we are using the stable Hibernate, MySQL, and Eclipse Link version in order to support the JPA Content and make a successful database connection. The rest dependencies will be automatically resolved by Maven and the <strong>updated<\/strong> file will have the following code:<\/p>\n<p><span style=\"text-decoration: underline;\"><em>pom.xml<\/em><\/span><\/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\"\n\txsi:schemaLocation=\"http:\/\/maven.apache.org\/POM\/4.0.0 http:\/\/maven.apache.org\/xsd\/maven-4.0.0.xsd\"&gt;\n\t&lt;modelVersion&gt;4.0.0&lt;\/modelVersion&gt;\n\t&lt;groupId&gt;JPAMappedbyExample&lt;\/groupId&gt;\n\t&lt;artifactId&gt;JPAMappedbyExample&lt;\/artifactId&gt;\n\t&lt;version&gt;0.0.1-SNAPSHOT&lt;\/version&gt;\n\t&lt;dependencies&gt;\n\t\t&lt;!-- https:\/\/mvnrepository.com\/artifact\/org.eclipse.persistence\/eclipselink --&gt;\n\t\t&lt;dependency&gt;\n\t\t\t&lt;groupId&gt;org.eclipse.persistence&lt;\/groupId&gt;\n\t\t\t&lt;artifactId&gt;eclipselink&lt;\/artifactId&gt;\n\t\t\t&lt;version&gt;2.5.2&lt;\/version&gt;\n\t\t&lt;\/dependency&gt;\n\t\t&lt;dependency&gt;\n\t\t\t&lt;groupId&gt;org.eclipse.persistence&lt;\/groupId&gt;\n\t\t\t&lt;artifactId&gt;javax.persistence&lt;\/artifactId&gt;\n\t\t\t&lt;version&gt;2.0.0&lt;\/version&gt;\n\t\t&lt;\/dependency&gt;\n\t\t&lt;!-- https:\/\/mvnrepository.com\/artifact\/mysql\/mysql-connector-java --&gt;\n\t\t&lt;dependency&gt;\n\t\t\t&lt;groupId&gt;mysql&lt;\/groupId&gt;\n\t\t\t&lt;artifactId&gt;mysql-connector-java&lt;\/artifactId&gt;\n\t\t\t&lt;version&gt;5.1.40&lt;\/version&gt;\n\t\t&lt;\/dependency&gt;\n\t\t&lt;!-- https:\/\/mvnrepository.com\/artifact\/org.hibernate\/hibernate-core --&gt;\n\t\t&lt;dependency&gt;\n\t\t\t&lt;groupId&gt;org.hibernate&lt;\/groupId&gt;\n\t\t\t&lt;artifactId&gt;hibernate-core&lt;\/artifactId&gt;\n\t\t\t&lt;version&gt;5.2.11.Final&lt;\/version&gt;\n\t\t&lt;\/dependency&gt;\n\t\t&lt;dependency&gt;\n\t\t\t&lt;groupId&gt;org.hibernate&lt;\/groupId&gt;\n\t\t\t&lt;artifactId&gt;hibernate-entitymanager&lt;\/artifactId&gt;\n\t\t\t&lt;version&gt;5.2.11.Final&lt;\/version&gt;\n\t\t&lt;\/dependency&gt;\n\t&lt;\/dependencies&gt;\n\t&lt;build&gt;\n\t\t&lt;finalName&gt;${project.artifactId}&lt;\/finalName&gt;\n\t&lt;\/build&gt;\n&lt;\/project&gt;\n<\/pre>\n<h3>3.3 Java Class Creation<\/h3>\n<p>Let\u2019s create the required Java files. Right-click on <code>src\/main\/java<\/code> folder, <code>New -&gt; Package<\/code>.<\/p>\n<p><figure id=\"attachment_51694\" aria-describedby=\"caption-attachment-51694\" style=\"width: 757px\" class=\"wp-caption aligncenter\"><a href=\"http:\/\/examples.javacodegeeks.com\/wp-content\/uploads\/2017\/10\/java-mapped-by-example-project-guide-4.jpg\"><img decoding=\"async\" class=\"size-full wp-image-51694\" src=\"http:\/\/examples.javacodegeeks.com\/wp-content\/uploads\/2017\/10\/java-mapped-by-example-project-guide-4.jpg\" alt=\"Fig. 6: Java Package Creation\" width=\"757\" height=\"664\" srcset=\"https:\/\/examples.javacodegeeks.com\/wp-content\/uploads\/2017\/10\/java-mapped-by-example-project-guide-4.jpg 757w, https:\/\/examples.javacodegeeks.com\/wp-content\/uploads\/2017\/10\/java-mapped-by-example-project-guide-4-300x263.jpg 300w\" sizes=\"(max-width: 757px) 100vw, 757px\" \/><\/a><figcaption id=\"caption-attachment-51694\" class=\"wp-caption-text\">Fig. 6: Java Package Creation<\/figcaption><\/figure><\/p>\n<p>A new pop window will open where we will enter the package name as: <code>com.jcg.jpa.mappedBy<\/code>.<\/p>\n<p><figure id=\"attachment_51695\" aria-describedby=\"caption-attachment-51695\" style=\"width: 516px\" class=\"wp-caption aligncenter\"><a href=\"http:\/\/examples.javacodegeeks.com\/wp-content\/uploads\/2017\/10\/java-mapped-by-example-project-guide-5.jpg\"><img decoding=\"async\" class=\"size-full wp-image-51695\" src=\"http:\/\/examples.javacodegeeks.com\/wp-content\/uploads\/2017\/10\/java-mapped-by-example-project-guide-5.jpg\" alt=\"Fig. 7: Java Package Name (com.jcg.jpa.mappedBy)\" width=\"516\" height=\"425\" srcset=\"https:\/\/examples.javacodegeeks.com\/wp-content\/uploads\/2017\/10\/java-mapped-by-example-project-guide-5.jpg 516w, https:\/\/examples.javacodegeeks.com\/wp-content\/uploads\/2017\/10\/java-mapped-by-example-project-guide-5-300x247.jpg 300w\" sizes=\"(max-width: 516px) 100vw, 516px\" \/><\/a><figcaption id=\"caption-attachment-51695\" class=\"wp-caption-text\">Fig. 7: Java Package Name (com.jcg.jpa.mappedBy)<\/figcaption><\/figure><\/p>\n<p>Once the package is created, we will need to create the model and the implementation classes. Right-click on the newly created package, <code>New -&gt; Class<\/code>.<\/p>\n<p><figure id=\"attachment_51696\" aria-describedby=\"caption-attachment-51696\" style=\"width: 820px\" class=\"wp-caption aligncenter\"><a href=\"http:\/\/examples.javacodegeeks.com\/wp-content\/uploads\/2017\/10\/java-mapped-by-example-project-guide-6.jpg\"><img decoding=\"async\" class=\"size-full wp-image-51696\" src=\"http:\/\/examples.javacodegeeks.com\/wp-content\/uploads\/2017\/10\/java-mapped-by-example-project-guide-6.jpg\" alt=\"Fig. 8: Java Class Creation\" width=\"820\" height=\"718\" srcset=\"https:\/\/examples.javacodegeeks.com\/wp-content\/uploads\/2017\/10\/java-mapped-by-example-project-guide-6.jpg 820w, https:\/\/examples.javacodegeeks.com\/wp-content\/uploads\/2017\/10\/java-mapped-by-example-project-guide-6-300x263.jpg 300w, https:\/\/examples.javacodegeeks.com\/wp-content\/uploads\/2017\/10\/java-mapped-by-example-project-guide-6-768x672.jpg 768w\" sizes=\"(max-width: 820px) 100vw, 820px\" \/><\/a><figcaption id=\"caption-attachment-51696\" class=\"wp-caption-text\">Fig. 8: Java Class Creation<\/figcaption><\/figure><\/p>\n<p>A new pop window will open and enter the file name as: <code>Employee<\/code>. The model class will be created inside the package: <code>com.jcg.jpa.mappedBy<\/code>.<\/p>\n<p><figure id=\"attachment_51697\" aria-describedby=\"caption-attachment-51697\" style=\"width: 533px\" class=\"wp-caption aligncenter\"><a href=\"http:\/\/examples.javacodegeeks.com\/wp-content\/uploads\/2017\/10\/java-mapped-by-example-project-guide-7.jpg\"><img decoding=\"async\" class=\"size-full wp-image-51697\" src=\"http:\/\/examples.javacodegeeks.com\/wp-content\/uploads\/2017\/10\/java-mapped-by-example-project-guide-7.jpg\" alt=\"Fig. 9: Java Class (Employee.java)\" width=\"533\" height=\"631\" srcset=\"https:\/\/examples.javacodegeeks.com\/wp-content\/uploads\/2017\/10\/java-mapped-by-example-project-guide-7.jpg 533w, https:\/\/examples.javacodegeeks.com\/wp-content\/uploads\/2017\/10\/java-mapped-by-example-project-guide-7-253x300.jpg 253w\" sizes=\"(max-width: 533px) 100vw, 533px\" \/><\/a><figcaption id=\"caption-attachment-51697\" class=\"wp-caption-text\">Fig. 9: Java Class (Employee.java)<\/figcaption><\/figure>[ulp id=&#8217;WII99u1JyPvMb8sP&#8217;]<\/p>\n<p>Repeat the step (i.e. Fig. 8) and enter the filename as: <code>Passport<\/code>. The entity model class will be created inside the package: <code>com.jcg.jpa.mappedBy<\/code>.<\/p>\n<p><figure id=\"attachment_51698\" aria-describedby=\"caption-attachment-51698\" style=\"width: 534px\" class=\"wp-caption aligncenter\"><a href=\"http:\/\/examples.javacodegeeks.com\/wp-content\/uploads\/2017\/10\/java-mapped-by-example-project-guide-8.jpg\"><img decoding=\"async\" class=\"size-full wp-image-51698\" src=\"http:\/\/examples.javacodegeeks.com\/wp-content\/uploads\/2017\/10\/java-mapped-by-example-project-guide-8.jpg\" alt=\"Fig 10: Java Class (Passport.java)\" width=\"534\" height=\"632\" srcset=\"https:\/\/examples.javacodegeeks.com\/wp-content\/uploads\/2017\/10\/java-mapped-by-example-project-guide-8.jpg 534w, https:\/\/examples.javacodegeeks.com\/wp-content\/uploads\/2017\/10\/java-mapped-by-example-project-guide-8-253x300.jpg 253w\" sizes=\"(max-width: 534px) 100vw, 534px\" \/><\/a><figcaption id=\"caption-attachment-51698\" class=\"wp-caption-text\">Fig 10: Java Class (Passport.java)<\/figcaption><\/figure><\/p>\n<p>Again, repeat the step listed in Fig. 8 and enter the file name as <code>AppDemo<\/code>. The implementation class will be created inside the package: <code>com.jcg.jpa.mappedBy<\/code>.<\/p>\n<p><figure id=\"attachment_51699\" aria-describedby=\"caption-attachment-51699\" style=\"width: 535px\" class=\"wp-caption aligncenter\"><a href=\"http:\/\/examples.javacodegeeks.com\/wp-content\/uploads\/2017\/10\/java-mapped-by-example-project-guide-9.jpg\"><img decoding=\"async\" class=\"size-full wp-image-51699\" src=\"http:\/\/examples.javacodegeeks.com\/wp-content\/uploads\/2017\/10\/java-mapped-by-example-project-guide-9.jpg\" alt=\"Fig. 11: Java Class (AppDemo.java)\" width=\"535\" height=\"628\" srcset=\"https:\/\/examples.javacodegeeks.com\/wp-content\/uploads\/2017\/10\/java-mapped-by-example-project-guide-9.jpg 535w, https:\/\/examples.javacodegeeks.com\/wp-content\/uploads\/2017\/10\/java-mapped-by-example-project-guide-9-256x300.jpg 256w\" sizes=\"(max-width: 535px) 100vw, 535px\" \/><\/a><figcaption id=\"caption-attachment-51699\" class=\"wp-caption-text\">Fig. 11: Java Class (AppDemo.java)<\/figcaption><\/figure><\/p>\n<h4>3.3.1 Implementation of Model Class<\/h4>\n<p>Add the following code to it:<\/p>\n<p><span style=\"text-decoration: underline;\"><em>Employee.java<\/em><\/span><\/p>\n<pre class=\"brush:java; wrap-lines:false;\">package com.jcg.jpa.mappedBy;\n\nimport java.io.Serializable;\n\nimport javax.persistence.CascadeType;\nimport javax.persistence.Column;\nimport javax.persistence.Entity;\nimport javax.persistence.FetchType;\nimport javax.persistence.GeneratedValue;\nimport javax.persistence.GenerationType;\nimport javax.persistence.Id;\nimport javax.persistence.JoinColumn;\nimport javax.persistence.OneToOne;\nimport javax.persistence.Table;\n\n@Entity\n@Table(name = \"EMPLOYEE_TABLE\")\npublic class Employee implements Serializable {\n\n\tprivate static final long serialVersionUID = 1L;\n\n\t@Id\n\t@Column(name = \"EMP_ID\")\n\t@GeneratedValue(strategy = GenerationType.IDENTITY)\n\tprivate int employeeId;\n\n\t@Column(name = \"EMP_NAME\")\n\tprivate String name;\n\n\t@OneToOne(fetch = FetchType.LAZY, cascade = CascadeType.ALL)\n\t@JoinColumn(name = \"PASSPORT_NUMBER\")\n\tprivate Passport passportDetails;\n\n\tpublic Employee() { }\n\n\tpublic int getEmployeeId() {\n\t\treturn employeeId;\n\t}\n\n\tpublic void setEmployeeId(int employeeId) {\n\t\tthis.employeeId = employeeId;\n\t}\n\n\tpublic String getName() {\n\t\treturn name;\n\t}\n\n\tpublic void setName(String name) {\n\t\tthis.name = name;\n\t}\n\n\tpublic Passport getPassportDetails() {\n\t\treturn passportDetails;\n\t}\n\n\tpublic void setPassportDetails(Passport passportDetails) {\n\t\tthis.passportDetails = passportDetails;\n\t}\n}\n<\/pre>\n<h4>3.3.2 Implementation of Entity Model Class<\/h4>\n<p>Add the following code to it:<\/p>\n<p><span style=\"text-decoration: underline;\"><em>Passport.java<\/em><\/span><\/p>\n<pre class=\"brush:java; wrap-lines:false;\">package com.jcg.jpa.mappedBy;\n\nimport java.io.Serializable;\n\nimport javax.persistence.Column;\nimport javax.persistence.Entity;\nimport javax.persistence.GeneratedValue;\nimport javax.persistence.GenerationType;\nimport javax.persistence.Id;\nimport javax.persistence.Table;\n\n@Entity\n@Table(name = \"PASSPORT_TABLE\")\npublic class Passport implements Serializable {\n\n\tprivate static final long serialVersionUID = 1L;\n\n\t@Id\n\t@Column(name = \"PASSPORT_NUMBER\")\n\t@GeneratedValue(strategy = GenerationType.IDENTITY)\n\tprivate int passportNumber;\n\n\t@Column(name = \"ADDRESS_LINE1\")\n\tprivate String addressLine1;\n\n\t@Column(name = \"ADDRESS_LINE2\")\n\tprivate String addressLine2;\n\n\t@Column(name = \"STATE_NAME\")\n\tprivate String state;\n\n\t@Column(name = \"COUNTRY_NAME\")\n\tprivate String country;\n\n\tpublic Passport() { }\n\n\tpublic int getPassportNumber() {\n\t\treturn passportNumber;\n\t}\n\n\tpublic void setPassportNumber(int passportNumber) {\n\t\tthis.passportNumber = passportNumber;\n\t}\n\n\tpublic String getAddressLine1() {\n\t\treturn addressLine1;\n\t}\n\n\tpublic void setAddressLine1(String addressLine1) {\n\t\tthis.addressLine1 = addressLine1;\n\t}\n\n\tpublic String getAddressLine2() {\n\t\treturn addressLine2;\n\t}\n\n\tpublic void setAddressLine2(String addressLine2) {\n\t\tthis.addressLine2 = addressLine2;\n\t}\n\n\tpublic String getState() {\n\t\treturn state;\n\t}\n\n\tpublic void setState(String state) {\n\t\tthis.state = state;\n\t}\n\n\tpublic String getCountry() {\n\t\treturn country;\n\t}\n\n\tpublic void setCountry(String country) {\n\t\tthis.country = country;\n\t}\n}\n<\/pre>\n<h4>3.3.3 Implementation of Utility Class<\/h4>\n<p>This is the service class which implements the Java Persistence API to perform a database transaction (i.e. <code>SQL<\/code> INSERT Operation). Add the following code to it:<\/p>\n<p><span style=\"text-decoration: underline;\"><em>AppDemo.java<\/em><\/span><\/p>\n<pre class=\"brush:java; wrap-lines:false;\">package com.jcg.jpa.mappedBy;\n\nimport javax.persistence.EntityManager;\nimport javax.persistence.EntityManagerFactory;\nimport javax.persistence.Persistence;\n\npublic class AppDemo {\n\n\tprivate static final EntityManagerFactory emFactoryObj;\n\tprivate static final String PERSISTENCE_UNIT_NAME = \"JPAMappedbyExample\";\t\n\n\tstatic {\n\t\temFactoryObj = Persistence.createEntityManagerFactory(PERSISTENCE_UNIT_NAME);\n\t}\n\n\t\/\/ This Method Is Used To Retrieve The 'EntityManager' Object\n\tpublic static EntityManager getEntityManager() {\n\t\treturn emFactoryObj.createEntityManager();\n\t}\n\n\tprivate static void insertRecord() {\n\t\tEntityManager entityMgrObj = getEntityManager();\n\t\tif (null != entityMgrObj) {\n\t\t\tentityMgrObj.getTransaction().begin();\n\n\t\t\tEmployee empObj = new Employee();\n\t\t\tempObj.setName(\"Harry Potter\");\n\n\t\t\tPassport passportDetailsObj = new Passport();\n\t\t\tpassportDetailsObj.setAddressLine1(\"Cupboard Under D' Stairs\");\n\t\t\tpassportDetailsObj.setAddressLine2(\" 4 Privet Drive\");\n\t\t\tpassportDetailsObj.setState(\"Little Whinging\");\n\t\t\tpassportDetailsObj.setCountry(\"Surrey\");\n\t\t\tempObj.setPassportDetails(passportDetailsObj);\n\t\t\tentityMgrObj.persist(empObj);\n\n\t\t\tentityMgrObj.getTransaction().commit();\n\n\t\t\tentityMgrObj.clear();\n\t\t\tSystem.out.println(\"Record Successfully Inserted In The Database\");\n\t\t}\n\t}\n\n\tpublic static void main(String[] args) {\n\t\tinsertRecord();\n\t}\n}\n<\/pre>\n<h3>3.4 Database Configuration File<\/h3>\n<p>Developers can achieve persistence in their application by introducing the <code>persistence.xml<\/code> in their code. This module plays a crucial role in the concept of JPA as in this configuration file we will register the database and specify the entity class. Create a directory <code>META-INF<\/code> in the src\/main\/java folder and create the file <code>persistence.xml<\/code> inside it. Add the following code to it:<\/p>\n<p><span style=\"text-decoration: underline;\"><em>persistence.xml<\/em><\/span><\/p>\n<pre class=\"brush:xml; wrap-lines:false;\">&lt;?xml version=\"1.0\" encoding=\"UTF-8\"?&gt;\n&lt;persistence version=\"2.1\"\n\txmlns=\"http:\/\/xmlns.jcp.org\/xml\/ns\/persistence\" xmlns:xsi=\"http:\/\/www.w3.org\/2001\/XMLSchema-instance\"\n\txsi:schemaLocation=\"http:\/\/xmlns.jcp.org\/xml\/ns\/persistence http:\/\/xmlns.jcp.org\/xml\/ns\/persistence\/persistence_2_1.xsd\"&gt;\n\t&lt;persistence-unit name=\"JPAMappedbyExample\" transaction-type=\"RESOURCE_LOCAL\"&gt;\n\t\t&lt;class&gt;com.jcg.jpa.mappedBy.Employee&lt;\/class&gt;\n\t\t&lt;class&gt;com.jcg.jpa.mappedBy.Passport&lt;\/class&gt;\n\n\t\t&lt;!-- Configuring The Database Connection Details --&gt;\n\t\t&lt;properties&gt;\n\t\t\t&lt;property name=\"javax.persistence.jdbc.driver\" value=\"com.mysql.jdbc.Driver\" \/&gt;\n\t\t\t&lt;property name=\"javax.persistence.jdbc.url\" value=\"jdbc:mysql:\/\/localhost:3306\/jpaMappedBy\" \/&gt;\n\t\t\t&lt;property name=\"javax.persistence.jdbc.user\" value=\"root\" \/&gt;\n\t\t\t&lt;property name=\"javax.persistence.jdbc.password\" value=\"\" \/&gt;\n\t\t&lt;\/properties&gt;\n\t&lt;\/persistence-unit&gt;\n&lt;\/persistence&gt;\n<\/pre>\n<p><strong>Notes<\/strong>:<\/p>\n<ul>\n<li>In this example, we are connecting the application with the MySQL database. So, developers must add the MySQL-connector-java-&lt;version&gt;-bin.jar to the project<\/li>\n<li>We have kept the <code>javax.persistence.jdbc.password<\/code> value as blank for simplicity, however, it is pure unto the user to keep it blank or set it during the MySQL configuration. If the user sets it, we need to provide the same password for this string<\/li>\n<\/ul>\n<h2>4. Run the Application<\/h2>\n<p>To run the application, Right click on the <code>AppDemo<\/code> class, <code>Run As -&gt; Java Application<\/code>. Developers can debug the example and see what happens after every step. Enjoy!<\/p>\n<p><figure id=\"attachment_51700\" aria-describedby=\"caption-attachment-51700\" style=\"width: 794px\" class=\"wp-caption aligncenter\"><a href=\"http:\/\/examples.javacodegeeks.com\/wp-content\/uploads\/2017\/10\/java-mapped-by-example-project-deploy-guide-1.jpg\"><img decoding=\"async\" class=\"size-full wp-image-51700\" src=\"http:\/\/examples.javacodegeeks.com\/wp-content\/uploads\/2017\/10\/java-mapped-by-example-project-deploy-guide-1.jpg\" alt=\"Fig. 12: Run Application\" width=\"794\" height=\"656\" srcset=\"https:\/\/examples.javacodegeeks.com\/wp-content\/uploads\/2017\/10\/java-mapped-by-example-project-deploy-guide-1.jpg 794w, https:\/\/examples.javacodegeeks.com\/wp-content\/uploads\/2017\/10\/java-mapped-by-example-project-deploy-guide-1-300x248.jpg 300w, https:\/\/examples.javacodegeeks.com\/wp-content\/uploads\/2017\/10\/java-mapped-by-example-project-deploy-guide-1-768x635.jpg 768w\" sizes=\"(max-width: 794px) 100vw, 794px\" \/><\/a><figcaption id=\"caption-attachment-51700\" class=\"wp-caption-text\">Fig. 12: Run Application<\/figcaption><\/figure><\/p>\n<h2>5. Project Demo<\/h2>\n<p>Hereafter running the code, the application shows the following status as output:<\/p>\n<p><figure id=\"attachment_51701\" aria-describedby=\"caption-attachment-51701\" style=\"width: 849px\" class=\"wp-caption aligncenter\"><a href=\"http:\/\/examples.javacodegeeks.com\/wp-content\/uploads\/2017\/10\/java-mapped-by-example-project-demo-guide-3.jpg\"><img decoding=\"async\" class=\"size-full wp-image-51701\" src=\"http:\/\/examples.javacodegeeks.com\/wp-content\/uploads\/2017\/10\/java-mapped-by-example-project-demo-guide-3.jpg\" alt=\"Fig. 13: Application Output\" width=\"849\" height=\"223\" srcset=\"https:\/\/examples.javacodegeeks.com\/wp-content\/uploads\/2017\/10\/java-mapped-by-example-project-demo-guide-3.jpg 849w, https:\/\/examples.javacodegeeks.com\/wp-content\/uploads\/2017\/10\/java-mapped-by-example-project-demo-guide-3-300x79.jpg 300w, https:\/\/examples.javacodegeeks.com\/wp-content\/uploads\/2017\/10\/java-mapped-by-example-project-demo-guide-3-768x202.jpg 768w\" sizes=\"(max-width: 849px) 100vw, 849px\" \/><\/a><figcaption id=\"caption-attachment-51701\" class=\"wp-caption-text\">Fig. 13: Application Output<\/figcaption><\/figure><\/p>\n<p>That\u2019s all for this post. Happy Learning!!<\/p>\n<h2>6. Conclusion<\/h2>\n<p>Through this example, we learned about the JPA implementation in Java. I hope this article served you whatever you were looking for. Developers can download the sample application as an Eclipse project in the Downloads section.<\/p>\n<h2>7. Download the Eclipse Project<\/h2>\n<p>This was an example of JPA mappedBy example.<\/p>\n<div class=\"download\"><strong>Download<\/strong><br \/>\nYou can download the full source code of this example here: <a href=\"http:\/\/examples.javacodegeeks.com\/wp-content\/uploads\/2017\/10\/JPAMappedbyExample.zip\" target=\"_blank\" rel=\"noopener noreferrer\"><strong>JPAMappedbyExample<\/strong><\/a><\/div>\n","protected":false},"excerpt":{"rendered":"<p>Hello readers, in this tutorial, we will show how to implement the mappedBy annotation in JPA using EclipseLink and MySQL in Java. &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; 1. Introduction Java Persistence API (JPA), is a standard interface which wraps the different Object Relational Mapping (ORM) tools such as Hibernate, EclipseLink, &hellip;<\/p>\n","protected":false},"author":119,"featured_media":1240,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[21],"tags":[1021,1028,647],"class_list":["post-51688","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-jpa","tag-hibernate","tag-jpa","tag-mysql"],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v26.5 - https:\/\/yoast.com\/wordpress\/plugins\/seo\/ -->\n<title>JPA mappedBy Example - Java Code Geeks<\/title>\n<meta name=\"description\" content=\"In this tutorial, we will show how to define and implement the mappedBy annotation in JPA using EclipseLink and MySQL in Java.\" \/>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/examples.javacodegeeks.com\/java-development\/enterprise-java\/jpa\/jpa-mappedby-example\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"JPA mappedBy Example - Java Code Geeks\" \/>\n<meta property=\"og:description\" content=\"In this tutorial, we will show how to define and implement the mappedBy annotation in JPA using EclipseLink and MySQL in Java.\" \/>\n<meta property=\"og:url\" content=\"https:\/\/examples.javacodegeeks.com\/java-development\/enterprise-java\/jpa\/jpa-mappedby-example\/\" \/>\n<meta property=\"og:site_name\" content=\"Examples Java Code Geeks\" \/>\n<meta property=\"article:publisher\" content=\"https:\/\/www.facebook.com\/javacodegeeks\" \/>\n<meta property=\"article:published_time\" content=\"2017-10-31T09:00:39+00:00\" \/>\n<meta property=\"article:modified_time\" content=\"2019-02-27T11:54:58+00:00\" \/>\n<meta property=\"og:image\" content=\"https:\/\/examples.javacodegeeks.com\/wp-content\/uploads\/2012\/12\/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=\"Yatin\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:creator\" content=\"@javacodegeeks\" \/>\n<meta name=\"twitter:site\" content=\"@javacodegeeks\" \/>\n<meta name=\"twitter:label1\" content=\"Written by\" \/>\n\t<meta name=\"twitter:data1\" content=\"Yatin\" \/>\n\t<meta name=\"twitter:label2\" content=\"Est. reading time\" \/>\n\t<meta name=\"twitter:data2\" content=\"13 minutes\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\/\/schema.org\",\"@graph\":[{\"@type\":\"Article\",\"@id\":\"https:\/\/examples.javacodegeeks.com\/java-development\/enterprise-java\/jpa\/jpa-mappedby-example\/#article\",\"isPartOf\":{\"@id\":\"https:\/\/examples.javacodegeeks.com\/java-development\/enterprise-java\/jpa\/jpa-mappedby-example\/\"},\"author\":{\"name\":\"Yatin\",\"@id\":\"https:\/\/examples.javacodegeeks.com\/#\/schema\/person\/9874407a37b028e8be3276e2b5960d13\"},\"headline\":\"JPA mappedBy Example\",\"datePublished\":\"2017-10-31T09:00:39+00:00\",\"dateModified\":\"2019-02-27T11:54:58+00:00\",\"mainEntityOfPage\":{\"@id\":\"https:\/\/examples.javacodegeeks.com\/java-development\/enterprise-java\/jpa\/jpa-mappedby-example\/\"},\"wordCount\":1421,\"commentCount\":1,\"publisher\":{\"@id\":\"https:\/\/examples.javacodegeeks.com\/#organization\"},\"image\":{\"@id\":\"https:\/\/examples.javacodegeeks.com\/java-development\/enterprise-java\/jpa\/jpa-mappedby-example\/#primaryimage\"},\"thumbnailUrl\":\"https:\/\/examples.javacodegeeks.com\/wp-content\/uploads\/2012\/12\/enterprise-java-logo.jpg\",\"keywords\":[\"hibernate\",\"jpa\",\"mysql\"],\"articleSection\":[\"jpa\"],\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"CommentAction\",\"name\":\"Comment\",\"target\":[\"https:\/\/examples.javacodegeeks.com\/java-development\/enterprise-java\/jpa\/jpa-mappedby-example\/#respond\"]}]},{\"@type\":\"WebPage\",\"@id\":\"https:\/\/examples.javacodegeeks.com\/java-development\/enterprise-java\/jpa\/jpa-mappedby-example\/\",\"url\":\"https:\/\/examples.javacodegeeks.com\/java-development\/enterprise-java\/jpa\/jpa-mappedby-example\/\",\"name\":\"JPA mappedBy Example - Java Code Geeks\",\"isPartOf\":{\"@id\":\"https:\/\/examples.javacodegeeks.com\/#website\"},\"primaryImageOfPage\":{\"@id\":\"https:\/\/examples.javacodegeeks.com\/java-development\/enterprise-java\/jpa\/jpa-mappedby-example\/#primaryimage\"},\"image\":{\"@id\":\"https:\/\/examples.javacodegeeks.com\/java-development\/enterprise-java\/jpa\/jpa-mappedby-example\/#primaryimage\"},\"thumbnailUrl\":\"https:\/\/examples.javacodegeeks.com\/wp-content\/uploads\/2012\/12\/enterprise-java-logo.jpg\",\"datePublished\":\"2017-10-31T09:00:39+00:00\",\"dateModified\":\"2019-02-27T11:54:58+00:00\",\"description\":\"In this tutorial, we will show how to define and implement the mappedBy annotation in JPA using EclipseLink and MySQL in Java.\",\"breadcrumb\":{\"@id\":\"https:\/\/examples.javacodegeeks.com\/java-development\/enterprise-java\/jpa\/jpa-mappedby-example\/#breadcrumb\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\/\/examples.javacodegeeks.com\/java-development\/enterprise-java\/jpa\/jpa-mappedby-example\/\"]}]},{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\/\/examples.javacodegeeks.com\/java-development\/enterprise-java\/jpa\/jpa-mappedby-example\/#primaryimage\",\"url\":\"https:\/\/examples.javacodegeeks.com\/wp-content\/uploads\/2012\/12\/enterprise-java-logo.jpg\",\"contentUrl\":\"https:\/\/examples.javacodegeeks.com\/wp-content\/uploads\/2012\/12\/enterprise-java-logo.jpg\",\"width\":150,\"height\":150},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\/\/examples.javacodegeeks.com\/java-development\/enterprise-java\/jpa\/jpa-mappedby-example\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Home\",\"item\":\"https:\/\/examples.javacodegeeks.com\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"Java Development\",\"item\":\"https:\/\/examples.javacodegeeks.com\/category\/java-development\/\"},{\"@type\":\"ListItem\",\"position\":3,\"name\":\"Enterprise Java\",\"item\":\"https:\/\/examples.javacodegeeks.com\/category\/java-development\/enterprise-java\/\"},{\"@type\":\"ListItem\",\"position\":4,\"name\":\"jpa\",\"item\":\"https:\/\/examples.javacodegeeks.com\/category\/java-development\/enterprise-java\/jpa\/\"},{\"@type\":\"ListItem\",\"position\":5,\"name\":\"JPA mappedBy Example\"}]},{\"@type\":\"WebSite\",\"@id\":\"https:\/\/examples.javacodegeeks.com\/#website\",\"url\":\"https:\/\/examples.javacodegeeks.com\/\",\"name\":\"Java Code Geeks\",\"description\":\"Java Examples and Code Snippets\",\"publisher\":{\"@id\":\"https:\/\/examples.javacodegeeks.com\/#organization\"},\"alternateName\":\"JCG\",\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\/\/examples.javacodegeeks.com\/?s={search_term_string}\"},\"query-input\":{\"@type\":\"PropertyValueSpecification\",\"valueRequired\":true,\"valueName\":\"search_term_string\"}}],\"inLanguage\":\"en-US\"},{\"@type\":\"Organization\",\"@id\":\"https:\/\/examples.javacodegeeks.com\/#organization\",\"name\":\"Exelixis Media P.C.\",\"url\":\"https:\/\/examples.javacodegeeks.com\/\",\"logo\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\/\/examples.javacodegeeks.com\/#\/schema\/logo\/image\/\",\"url\":\"https:\/\/examples.javacodegeeks.com\/wp-content\/uploads\/2022\/06\/exelixis-logo.png\",\"contentUrl\":\"https:\/\/examples.javacodegeeks.com\/wp-content\/uploads\/2022\/06\/exelixis-logo.png\",\"width\":864,\"height\":246,\"caption\":\"Exelixis Media P.C.\"},\"image\":{\"@id\":\"https:\/\/examples.javacodegeeks.com\/#\/schema\/logo\/image\/\"},\"sameAs\":[\"https:\/\/www.facebook.com\/javacodegeeks\",\"https:\/\/x.com\/javacodegeeks\"]},{\"@type\":\"Person\",\"@id\":\"https:\/\/examples.javacodegeeks.com\/#\/schema\/person\/9874407a37b028e8be3276e2b5960d13\",\"name\":\"Yatin\",\"image\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\/\/examples.javacodegeeks.com\/#\/schema\/person\/image\/\",\"url\":\"https:\/\/examples.javacodegeeks.com\/wp-content\/uploads\/2023\/09\/cropped-Yatin-Batra_avatar_1515758148-96x96.jpg\",\"contentUrl\":\"https:\/\/examples.javacodegeeks.com\/wp-content\/uploads\/2023\/09\/cropped-Yatin-Batra_avatar_1515758148-96x96.jpg\",\"caption\":\"Yatin\"},\"description\":\"An experience full-stack engineer well versed with Core Java, Spring\/Springboot, MVC, Security, AOP, Frontend (Angular &amp; React), and cloud technologies (such as AWS, GCP, Jenkins, Docker, K8).\",\"sameAs\":[\"https:\/\/www.javacodegeeks.com\"],\"url\":\"https:\/\/examples.javacodegeeks.com\/author\/yatin-batra\/\"}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"JPA mappedBy Example - Java Code Geeks","description":"In this tutorial, we will show how to define and implement the mappedBy annotation in JPA using EclipseLink and MySQL in Java.","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:\/\/examples.javacodegeeks.com\/java-development\/enterprise-java\/jpa\/jpa-mappedby-example\/","og_locale":"en_US","og_type":"article","og_title":"JPA mappedBy Example - Java Code Geeks","og_description":"In this tutorial, we will show how to define and implement the mappedBy annotation in JPA using EclipseLink and MySQL in Java.","og_url":"https:\/\/examples.javacodegeeks.com\/java-development\/enterprise-java\/jpa\/jpa-mappedby-example\/","og_site_name":"Examples Java Code Geeks","article_publisher":"https:\/\/www.facebook.com\/javacodegeeks","article_published_time":"2017-10-31T09:00:39+00:00","article_modified_time":"2019-02-27T11:54:58+00:00","og_image":[{"width":150,"height":150,"url":"https:\/\/examples.javacodegeeks.com\/wp-content\/uploads\/2012\/12\/enterprise-java-logo.jpg","type":"image\/jpeg"}],"author":"Yatin","twitter_card":"summary_large_image","twitter_creator":"@javacodegeeks","twitter_site":"@javacodegeeks","twitter_misc":{"Written by":"Yatin","Est. reading time":"13 minutes"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"Article","@id":"https:\/\/examples.javacodegeeks.com\/java-development\/enterprise-java\/jpa\/jpa-mappedby-example\/#article","isPartOf":{"@id":"https:\/\/examples.javacodegeeks.com\/java-development\/enterprise-java\/jpa\/jpa-mappedby-example\/"},"author":{"name":"Yatin","@id":"https:\/\/examples.javacodegeeks.com\/#\/schema\/person\/9874407a37b028e8be3276e2b5960d13"},"headline":"JPA mappedBy Example","datePublished":"2017-10-31T09:00:39+00:00","dateModified":"2019-02-27T11:54:58+00:00","mainEntityOfPage":{"@id":"https:\/\/examples.javacodegeeks.com\/java-development\/enterprise-java\/jpa\/jpa-mappedby-example\/"},"wordCount":1421,"commentCount":1,"publisher":{"@id":"https:\/\/examples.javacodegeeks.com\/#organization"},"image":{"@id":"https:\/\/examples.javacodegeeks.com\/java-development\/enterprise-java\/jpa\/jpa-mappedby-example\/#primaryimage"},"thumbnailUrl":"https:\/\/examples.javacodegeeks.com\/wp-content\/uploads\/2012\/12\/enterprise-java-logo.jpg","keywords":["hibernate","jpa","mysql"],"articleSection":["jpa"],"inLanguage":"en-US","potentialAction":[{"@type":"CommentAction","name":"Comment","target":["https:\/\/examples.javacodegeeks.com\/java-development\/enterprise-java\/jpa\/jpa-mappedby-example\/#respond"]}]},{"@type":"WebPage","@id":"https:\/\/examples.javacodegeeks.com\/java-development\/enterprise-java\/jpa\/jpa-mappedby-example\/","url":"https:\/\/examples.javacodegeeks.com\/java-development\/enterprise-java\/jpa\/jpa-mappedby-example\/","name":"JPA mappedBy Example - Java Code Geeks","isPartOf":{"@id":"https:\/\/examples.javacodegeeks.com\/#website"},"primaryImageOfPage":{"@id":"https:\/\/examples.javacodegeeks.com\/java-development\/enterprise-java\/jpa\/jpa-mappedby-example\/#primaryimage"},"image":{"@id":"https:\/\/examples.javacodegeeks.com\/java-development\/enterprise-java\/jpa\/jpa-mappedby-example\/#primaryimage"},"thumbnailUrl":"https:\/\/examples.javacodegeeks.com\/wp-content\/uploads\/2012\/12\/enterprise-java-logo.jpg","datePublished":"2017-10-31T09:00:39+00:00","dateModified":"2019-02-27T11:54:58+00:00","description":"In this tutorial, we will show how to define and implement the mappedBy annotation in JPA using EclipseLink and MySQL in Java.","breadcrumb":{"@id":"https:\/\/examples.javacodegeeks.com\/java-development\/enterprise-java\/jpa\/jpa-mappedby-example\/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/examples.javacodegeeks.com\/java-development\/enterprise-java\/jpa\/jpa-mappedby-example\/"]}]},{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/examples.javacodegeeks.com\/java-development\/enterprise-java\/jpa\/jpa-mappedby-example\/#primaryimage","url":"https:\/\/examples.javacodegeeks.com\/wp-content\/uploads\/2012\/12\/enterprise-java-logo.jpg","contentUrl":"https:\/\/examples.javacodegeeks.com\/wp-content\/uploads\/2012\/12\/enterprise-java-logo.jpg","width":150,"height":150},{"@type":"BreadcrumbList","@id":"https:\/\/examples.javacodegeeks.com\/java-development\/enterprise-java\/jpa\/jpa-mappedby-example\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https:\/\/examples.javacodegeeks.com\/"},{"@type":"ListItem","position":2,"name":"Java Development","item":"https:\/\/examples.javacodegeeks.com\/category\/java-development\/"},{"@type":"ListItem","position":3,"name":"Enterprise Java","item":"https:\/\/examples.javacodegeeks.com\/category\/java-development\/enterprise-java\/"},{"@type":"ListItem","position":4,"name":"jpa","item":"https:\/\/examples.javacodegeeks.com\/category\/java-development\/enterprise-java\/jpa\/"},{"@type":"ListItem","position":5,"name":"JPA mappedBy Example"}]},{"@type":"WebSite","@id":"https:\/\/examples.javacodegeeks.com\/#website","url":"https:\/\/examples.javacodegeeks.com\/","name":"Java Code Geeks","description":"Java Examples and Code Snippets","publisher":{"@id":"https:\/\/examples.javacodegeeks.com\/#organization"},"alternateName":"JCG","potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/examples.javacodegeeks.com\/?s={search_term_string}"},"query-input":{"@type":"PropertyValueSpecification","valueRequired":true,"valueName":"search_term_string"}}],"inLanguage":"en-US"},{"@type":"Organization","@id":"https:\/\/examples.javacodegeeks.com\/#organization","name":"Exelixis Media P.C.","url":"https:\/\/examples.javacodegeeks.com\/","logo":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/examples.javacodegeeks.com\/#\/schema\/logo\/image\/","url":"https:\/\/examples.javacodegeeks.com\/wp-content\/uploads\/2022\/06\/exelixis-logo.png","contentUrl":"https:\/\/examples.javacodegeeks.com\/wp-content\/uploads\/2022\/06\/exelixis-logo.png","width":864,"height":246,"caption":"Exelixis Media P.C."},"image":{"@id":"https:\/\/examples.javacodegeeks.com\/#\/schema\/logo\/image\/"},"sameAs":["https:\/\/www.facebook.com\/javacodegeeks","https:\/\/x.com\/javacodegeeks"]},{"@type":"Person","@id":"https:\/\/examples.javacodegeeks.com\/#\/schema\/person\/9874407a37b028e8be3276e2b5960d13","name":"Yatin","image":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/examples.javacodegeeks.com\/#\/schema\/person\/image\/","url":"https:\/\/examples.javacodegeeks.com\/wp-content\/uploads\/2023\/09\/cropped-Yatin-Batra_avatar_1515758148-96x96.jpg","contentUrl":"https:\/\/examples.javacodegeeks.com\/wp-content\/uploads\/2023\/09\/cropped-Yatin-Batra_avatar_1515758148-96x96.jpg","caption":"Yatin"},"description":"An experience full-stack engineer well versed with Core Java, Spring\/Springboot, MVC, Security, AOP, Frontend (Angular &amp; React), and cloud technologies (such as AWS, GCP, Jenkins, Docker, K8).","sameAs":["https:\/\/www.javacodegeeks.com"],"url":"https:\/\/examples.javacodegeeks.com\/author\/yatin-batra\/"}]}},"_links":{"self":[{"href":"https:\/\/examples.javacodegeeks.com\/wp-json\/wp\/v2\/posts\/51688","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/examples.javacodegeeks.com\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/examples.javacodegeeks.com\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/examples.javacodegeeks.com\/wp-json\/wp\/v2\/users\/119"}],"replies":[{"embeddable":true,"href":"https:\/\/examples.javacodegeeks.com\/wp-json\/wp\/v2\/comments?post=51688"}],"version-history":[{"count":0,"href":"https:\/\/examples.javacodegeeks.com\/wp-json\/wp\/v2\/posts\/51688\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/examples.javacodegeeks.com\/wp-json\/wp\/v2\/media\/1240"}],"wp:attachment":[{"href":"https:\/\/examples.javacodegeeks.com\/wp-json\/wp\/v2\/media?parent=51688"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/examples.javacodegeeks.com\/wp-json\/wp\/v2\/categories?post=51688"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/examples.javacodegeeks.com\/wp-json\/wp\/v2\/tags?post=51688"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}