{"id":31213,"date":"2014-10-07T01:00:40","date_gmt":"2014-10-06T22:00:40","guid":{"rendered":"http:\/\/www.javacodegeeks.com\/?p=31213"},"modified":"2015-08-28T11:13:29","modified_gmt":"2015-08-28T08:13:29","slug":"jpa-tutorial-mapping-entities-part-2","status":"publish","type":"post","link":"https:\/\/www.javacodegeeks.com\/2014\/10\/jpa-tutorial-mapping-entities-part-2.html","title":{"rendered":"JPA Tutorial: Mapping Entities \u2013 Part 2"},"content":{"rendered":"<p>In my <a title=\"JPA tutorial: Mapping Entities \u2013 Part 1\" href=\"http:\/\/www.javacodegeeks.com\/2014\/09\/jpa-tutorial-mapping-entities-part-1.html\">last post<\/a> I showed a simple way of persisting an entity. I explained the default approach that JPA uses to determine the default table for an entity. Let\u2019s assume that we want to override this default name. We may like to do so because the data model has been designed and fixed before and the table names do not match with our class names (I have seen people to create tables with \u201ctbl_\u201d prefix, for example). So how should we override the default table names\u00a0to match the existing data model?<\/p>\n<p>Turns out, it\u2019s pretty simple. If we need to override the default table names assumed by JPA, then there are a couple of ways to do it:<\/p>\n<ol>\n<li>We can use the <em>name<\/em> attribute of the <em>@Entity<\/em>\u00a0annotation to provide an explicit entity name to match with the database table name. For our example we could have used <em>@Entity(name = \u201ctbl_address\u201d)<\/em> in our <em>Address<\/em> class if our table name was <em>tbl_address.<\/em><\/li>\n<li>We can use a <em>@Table<\/em> (defined in the <em>javax.persistence\u00a0<\/em>package) annotation just below the <em>@Entity<\/em> annotation and use its <em>name<\/em> attribute to specify the table name explicitly.<\/li>\n<\/ol>\n<pre class=\" brush:java\">@Entity\r\n@Table(name = \"tbl_address\")\r\npublic class Address {\r\n  \/\/ Rest of the class\r\n}<\/pre>\n<p>From these two approaches the <em>@Table<\/em> annotation provides more options to customize the mapping. For example, some databases like PostgreSQL have a concept of <a title=\"PostgreSQL Schema Definition\" href=\"http:\/\/www.postgresql.org\/docs\/9.3\/static\/ddl-schemas.html\" target=\"_blank\">schemas<\/a>, using which you can further categorize\/group your tables. Because of this feature\u00a0you can create two tables with the same name in a single database (although they will belong to two different schemas). To access these tables you then add the schema name as the table prefix in your query. So if a PostgreSQL database has two different schemas named <em>public<\/em> (which is sort of like\u00a0default schema for a PostgreSQL database) and <em>document<\/em>, and both of these schemas contain tables named <em>document_collection<\/em>, then both of these two queries are perfectly valid:<\/p>\n<pre class=\" brush:sql\">-- fetch from the table under public schema\r\nSELECT *\r\nFROM   public.document_collection;\r\n\r\n-- fetch from the table under document schema\r\nSELECT *\r\nFROM   document.document_collection;<\/pre>\n<p>In order to map an entity to the <em>document_collection<\/em> table in the <em>document<\/em> schema, you will then use the <em>@Table<\/em> annotation with its <em>schema<\/em> attribute set to <em>document<\/em>:<\/p>\n<pre class=\" brush:java\">@Entity\r\n@Table(name=\"document_collection\", schema=\"document\")\r\npublic class DocumentCollection {\r\n  \/\/ rest of the class\r\n}<\/pre>\n<p>When specified this way, the schema name will be added as a prefix to the table name when the JPA goes to the database to access the table, just like we did in our queries.<\/p>\n<p>What if rather than specifying the schema name in the @<em>Table<\/em> annotation you append the schema name in the table name itself, like this:<\/p>\n<pre class=\" brush:java\">@Entity\r\n@Table(name = \"document.document_collection\")\r\npublic class DocumentCollection {\r\n  \/\/ rest of the class\r\n}<\/pre>\n<p>Inlining the schema name with the table name this way is not guaranteed to work across all JPA implementations because support for this is not specified in the JPA specification (non-standard). So it\u2019s better if you do not make a habit of doing this even if your persistence provider supports it.<div style=\"display:inline-block; margin: 15px 0;\"> <div id=\"adngin-JavaCodeGeeks_incontent_video-0\" style=\"display:inline-block;\"><\/div> <\/div><\/p>\n<p>Let\u2019s turn our attention to the columns next. In order to determine the default columns, JPA does something similar to the following:<\/p>\n<ol>\n<li>At first it checks to see if any explicit column mapping information is given. If no column mapping information is found, it tries to guess the default values for columns.<\/li>\n<li>To determine the default values, JPA needs to know the access type of the entity states i.e., the way to read\/write the states of the entity. In JPA two different access types are possible \u2013 field and property. For our example we have used the field access (actually JPA assumed this from the location\/placement of the\u00a0<i>@Id<\/i> annotation, \u00a0but more on this later). If you use this access type then states will be written\/read directly from the entity fields using the Reflection API.<\/li>\n<li>After the access type is known, JPA then tries to determine the column names. For field access type JPA directly treats the field name as the column names,\u00a0which means if an entity has a field named <em>status<\/em> then it will be mapped to a column named <em>status<\/em>.<\/li>\n<\/ol>\n<p>At this point it should be clear to us how the states of the <em>Address<\/em> entities got saved into the corresponding columns. Each of the fields of the <em>Address<\/em> entity has an equivalent column in the database table <em>tbl_address<\/em>, so JPA directly saved them into their corresponding columns. The\u00a0<em>id<\/em> field was saved into the <em>id<\/em> column, <em>city<\/em> field into the <em>city<\/em> column and so on.<\/p>\n<p>OK then, let\u2019s move on to overriding column names. As far as I know there is only one way (if you happen to know of any other way please comment in!) to override the default column names for entity states, which is by using the <em>@Column<\/em> (defined in the <em>javax.persistence<\/em> package) annotation. So if the <em>id<\/em> column of the <em>tbl_address<\/em> table is renamed to be <em>address_id<\/em> then we could either change our field name to <em>address_id<\/em>, or we could use the <em>@Column<\/em> annotation with its <em>name<\/em> attribute set to <em>address_id<\/em>:<\/p>\n<pre class=\" brush:java\">@Entity\r\n@Table(name = \"tbl_address\")\r\npublic class Address {\r\n  @Id\r\n  @GeneratedValue\r\n  @Column(name = \"address_id\")\r\n  private Integer id;\r\n\r\n  \/\/ Rest of the class\r\n}<\/pre>\n<p>You can see that for all the above cases the default approaches that JPA uses are quite sensible, and most of the cases you will be happy with it. However, changing the default values are also very easy and can be done very quickly.<\/p>\n<p>What if we have a field in the <em>Address<\/em> entity\u00a0that we do not wish to save in the database? Suppose that the <em>Address<\/em> entity has a column named <em>transientColumn<\/em> which does not have any corresponding default column in the database table:<\/p>\n<pre class=\" brush:java\">@Entity\r\n@Table(name = \"tbl_address\")\r\npublic class Address {\r\n  @Id\r\n  @GeneratedValue\r\n  @Column(name = \"address_id\")\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  private String transientColumn;\r\n\r\n  \/\/ Rest of the class\r\n}<\/pre>\n<p>If you compile your code with the above change then you will get an exception which looks something like below:<\/p>\n<pre class=\"brush:bash;wrap-lines:false\">Exception in thread \u201cmain\u201d java.lang.ExceptionInInitializerError\r\nat com.keertimaan.javasamples.jpaexample.Main.main(Main.java:33)\r\nCaused by: javax.persistence.PersistenceException: Unable to build entity manager factory\r\nat org.hibernate.jpa.HibernatePersistenceProvider.createEntityManagerFactory(HibernatePersistenceProvider.java:83)\r\nat org.hibernate.ejb.HibernatePersistence.createEntityManagerFactory(HibernatePersistence.java:54)\r\nat javax.persistence.Persistence.createEntityManagerFactory(Persistence.java:55)\r\nat javax.persistence.Persistence.createEntityManagerFactory(Persistence.java:39)\r\nat com.keertimaan.javasamples.jpaexample.persistenceutil.PersistenceManager.&lt;init&gt;(PersistenceManager.java:31)\r\nat com.keertimaan.javasamples.jpaexample.persistenceutil.PersistenceManager.&lt;clinit&gt;(PersistenceManager.java:26)\r\n\u2026 1 more\r\nCaused by: org.hibernate.HibernateException: Missing column: transientColumn in jpa_example.tbl_address\r\nat org.hibernate.mapping.Table.validateColumns(Table.java:365)\r\nat org.hibernate.cfg.Configuration.validateSchema(Configuration.java:1336)\r\nat org.hibernate.tool.hbm2ddl.SchemaValidator.validate(SchemaValidator.java:155)\r\nat org.hibernate.internal.SessionFactoryImpl.&lt;init&gt;(SessionFactoryImpl.java:525)\r\nat org.hibernate.cfg.Configuration.buildSessionFactory(Configuration.java:1857)\r\nat org.hibernate.jpa.boot.internal.EntityManagerFactoryBuilderImpl$4.perform(EntityManagerFactoryBuilderImpl.java:850)\r\nat org.hibernate.jpa.boot.internal.EntityManagerFactoryBuilderImpl$4.perform(EntityManagerFactoryBuilderImpl.java:843)\r\nat org.hibernate.boot.registry.classloading.internal.ClassLoaderServiceImpl.withTccl(ClassLoaderServiceImpl.java:398)\r\nat org.hibernate.jpa.boot.internal.EntityManagerFactoryBuilderImpl.build(EntityManagerFactoryBuilderImpl.java:842)\r\nat org.hibernate.jpa.HibernatePersistenceProvider.createEntityManagerFactory(HibernatePersistenceProvider.java:75)\r\n\u2026 6 more<\/pre>\n<p>The exception is saying that the persistence provider could not find any column in the database whose name is\u00a0<em>transientColumn<\/em>, and we did not do anything to make it clear to the persistence provider that we do not wish to save this field in the database. The persistence provider took it as any other fields in the entity\u00a0which are mapped to database columns.<\/p>\n<p>In order to fix this problem, we can do any of the following:<\/p>\n<ol>\n<li>We can annotate the\u00a0<em>transientColumn<\/em> field with the <i>@Transient<\/i> (defined in\u00a0<em>javax.persistence<\/em> package) annotation to let the persistence provider know that we do not wish to save this field, and it does not have any corresponding column in the table.<\/li>\n<li>We can use the\u00a0<em>transient<\/em> keyword that Java has by default.<\/li>\n<\/ol>\n<p>The difference between these two approaches that comes to my mind is that, if we use the\u00a0<em>transient<\/em> keyword instead of the annotation, then\u00a0if one of the\u00a0<em>Address<\/em> entities gets serialized from one JVM to another then the\u00a0<em>transientColumn<\/em> field will get reinitialized again (just like any other\u00a0<em>transient<\/em> fields in Java). For the annotation, this will not happen and the\u00a0<em>transientColumn<\/em>\u00a0field will retain its value across the serialization. As a rule of thumb, I always use the annotation if I do not need to worry about serialization (and in most of the cases I don\u2019t).<\/p>\n<p>So using the annotation, we can fix the problem right away:<\/p>\n<pre class=\" brush:java\">@Entity\r\n@Table(name = \"tbl_address\")\r\npublic class Address {\r\n  @Id\r\n  @GeneratedValue\r\n  @Column(name = \"address_id\")\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  @Transient\r\n  private String transientColumn;\r\n\r\n  \/\/ Rest of the class\r\n}<\/pre>\n<p>So that\u2019s it for today folks. If you find any mistakes\/have any input, please feel free to comment in!<\/p>\n<p>Until next time.<\/p>\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\/09\/21\/jpa-tutorial-mapping-entities-part-2\/\">JPA Tutorial: Mapping Entities \u2013 Part 2<\/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>In my last post I showed a simple way of persisting an entity. I explained the default approach that JPA uses to determine the default table for an entity. Let\u2019s assume that we want to override this default name. We may like to do so because the data model has been designed and fixed before &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-31213","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: Mapping Entities \u2013 Part 2<\/title>\n<meta name=\"description\" content=\"In my last post I showed a simple way of persisting an entity. I explained the default approach that JPA uses to determine the default table for an\" \/>\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\/10\/jpa-tutorial-mapping-entities-part-2.html\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"JPA Tutorial: Mapping Entities \u2013 Part 2\" \/>\n<meta property=\"og:description\" content=\"In my last post I showed a simple way of persisting an entity. I explained the default approach that JPA uses to determine the default table for an\" \/>\n<meta property=\"og:url\" content=\"https:\/\/www.javacodegeeks.com\/2014\/10\/jpa-tutorial-mapping-entities-part-2.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-10-06T22:00:40+00:00\" \/>\n<meta property=\"article:modified_time\" content=\"2015-08-28T08:13:29+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=\"8 minutes\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\\\/\\\/schema.org\",\"@graph\":[{\"@type\":\"Article\",\"@id\":\"https:\\\/\\\/www.javacodegeeks.com\\\/2014\\\/10\\\/jpa-tutorial-mapping-entities-part-2.html#article\",\"isPartOf\":{\"@id\":\"https:\\\/\\\/www.javacodegeeks.com\\\/2014\\\/10\\\/jpa-tutorial-mapping-entities-part-2.html\"},\"author\":{\"name\":\"MD Sayem Ahmed\",\"@id\":\"https:\\\/\\\/www.javacodegeeks.com\\\/#\\\/schema\\\/person\\\/c28c3b2d8f198300d25627addda7df17\"},\"headline\":\"JPA Tutorial: Mapping Entities \u2013 Part 2\",\"datePublished\":\"2014-10-06T22:00:40+00:00\",\"dateModified\":\"2015-08-28T08:13:29+00:00\",\"mainEntityOfPage\":{\"@id\":\"https:\\\/\\\/www.javacodegeeks.com\\\/2014\\\/10\\\/jpa-tutorial-mapping-entities-part-2.html\"},\"wordCount\":1194,\"commentCount\":0,\"publisher\":{\"@id\":\"https:\\\/\\\/www.javacodegeeks.com\\\/#organization\"},\"image\":{\"@id\":\"https:\\\/\\\/www.javacodegeeks.com\\\/2014\\\/10\\\/jpa-tutorial-mapping-entities-part-2.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\\\/10\\\/jpa-tutorial-mapping-entities-part-2.html#respond\"]}]},{\"@type\":\"WebPage\",\"@id\":\"https:\\\/\\\/www.javacodegeeks.com\\\/2014\\\/10\\\/jpa-tutorial-mapping-entities-part-2.html\",\"url\":\"https:\\\/\\\/www.javacodegeeks.com\\\/2014\\\/10\\\/jpa-tutorial-mapping-entities-part-2.html\",\"name\":\"JPA Tutorial: Mapping Entities \u2013 Part 2\",\"isPartOf\":{\"@id\":\"https:\\\/\\\/www.javacodegeeks.com\\\/#website\"},\"primaryImageOfPage\":{\"@id\":\"https:\\\/\\\/www.javacodegeeks.com\\\/2014\\\/10\\\/jpa-tutorial-mapping-entities-part-2.html#primaryimage\"},\"image\":{\"@id\":\"https:\\\/\\\/www.javacodegeeks.com\\\/2014\\\/10\\\/jpa-tutorial-mapping-entities-part-2.html#primaryimage\"},\"thumbnailUrl\":\"https:\\\/\\\/www.javacodegeeks.com\\\/wp-content\\\/uploads\\\/2012\\\/10\\\/enterprise-java-logo.jpg\",\"datePublished\":\"2014-10-06T22:00:40+00:00\",\"dateModified\":\"2015-08-28T08:13:29+00:00\",\"description\":\"In my last post I showed a simple way of persisting an entity. I explained the default approach that JPA uses to determine the default table for an\",\"breadcrumb\":{\"@id\":\"https:\\\/\\\/www.javacodegeeks.com\\\/2014\\\/10\\\/jpa-tutorial-mapping-entities-part-2.html#breadcrumb\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\\\/\\\/www.javacodegeeks.com\\\/2014\\\/10\\\/jpa-tutorial-mapping-entities-part-2.html\"]}]},{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\\\/\\\/www.javacodegeeks.com\\\/2014\\\/10\\\/jpa-tutorial-mapping-entities-part-2.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\\\/10\\\/jpa-tutorial-mapping-entities-part-2.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: Mapping Entities \u2013 Part 2\"}]},{\"@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: Mapping Entities \u2013 Part 2","description":"In my last post I showed a simple way of persisting an entity. I explained the default approach that JPA uses to determine the default table for an","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\/10\/jpa-tutorial-mapping-entities-part-2.html","og_locale":"en_US","og_type":"article","og_title":"JPA Tutorial: Mapping Entities \u2013 Part 2","og_description":"In my last post I showed a simple way of persisting an entity. I explained the default approach that JPA uses to determine the default table for an","og_url":"https:\/\/www.javacodegeeks.com\/2014\/10\/jpa-tutorial-mapping-entities-part-2.html","og_site_name":"Java Code Geeks","article_publisher":"https:\/\/www.facebook.com\/javacodegeeks","article_published_time":"2014-10-06T22:00:40+00:00","article_modified_time":"2015-08-28T08:13:29+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":"8 minutes"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"Article","@id":"https:\/\/www.javacodegeeks.com\/2014\/10\/jpa-tutorial-mapping-entities-part-2.html#article","isPartOf":{"@id":"https:\/\/www.javacodegeeks.com\/2014\/10\/jpa-tutorial-mapping-entities-part-2.html"},"author":{"name":"MD Sayem Ahmed","@id":"https:\/\/www.javacodegeeks.com\/#\/schema\/person\/c28c3b2d8f198300d25627addda7df17"},"headline":"JPA Tutorial: Mapping Entities \u2013 Part 2","datePublished":"2014-10-06T22:00:40+00:00","dateModified":"2015-08-28T08:13:29+00:00","mainEntityOfPage":{"@id":"https:\/\/www.javacodegeeks.com\/2014\/10\/jpa-tutorial-mapping-entities-part-2.html"},"wordCount":1194,"commentCount":0,"publisher":{"@id":"https:\/\/www.javacodegeeks.com\/#organization"},"image":{"@id":"https:\/\/www.javacodegeeks.com\/2014\/10\/jpa-tutorial-mapping-entities-part-2.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\/10\/jpa-tutorial-mapping-entities-part-2.html#respond"]}]},{"@type":"WebPage","@id":"https:\/\/www.javacodegeeks.com\/2014\/10\/jpa-tutorial-mapping-entities-part-2.html","url":"https:\/\/www.javacodegeeks.com\/2014\/10\/jpa-tutorial-mapping-entities-part-2.html","name":"JPA Tutorial: Mapping Entities \u2013 Part 2","isPartOf":{"@id":"https:\/\/www.javacodegeeks.com\/#website"},"primaryImageOfPage":{"@id":"https:\/\/www.javacodegeeks.com\/2014\/10\/jpa-tutorial-mapping-entities-part-2.html#primaryimage"},"image":{"@id":"https:\/\/www.javacodegeeks.com\/2014\/10\/jpa-tutorial-mapping-entities-part-2.html#primaryimage"},"thumbnailUrl":"https:\/\/www.javacodegeeks.com\/wp-content\/uploads\/2012\/10\/enterprise-java-logo.jpg","datePublished":"2014-10-06T22:00:40+00:00","dateModified":"2015-08-28T08:13:29+00:00","description":"In my last post I showed a simple way of persisting an entity. I explained the default approach that JPA uses to determine the default table for an","breadcrumb":{"@id":"https:\/\/www.javacodegeeks.com\/2014\/10\/jpa-tutorial-mapping-entities-part-2.html#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/www.javacodegeeks.com\/2014\/10\/jpa-tutorial-mapping-entities-part-2.html"]}]},{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/www.javacodegeeks.com\/2014\/10\/jpa-tutorial-mapping-entities-part-2.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\/10\/jpa-tutorial-mapping-entities-part-2.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: Mapping Entities \u2013 Part 2"}]},{"@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\/31213","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=31213"}],"version-history":[{"count":0,"href":"https:\/\/www.javacodegeeks.com\/wp-json\/wp\/v2\/posts\/31213\/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=31213"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.javacodegeeks.com\/wp-json\/wp\/v2\/categories?post=31213"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.javacodegeeks.com\/wp-json\/wp\/v2\/tags?post=31213"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}