title: How to Persist data in One to One Relationship Using JPA JSF
body: <p><strong>At the First I will let u Know what i want to achieve and Later I will Show u the Problem.</strong></p>
  
  <p><img src="https://i.stack.imgur.com/Z6UtI.jpg" alt="enter image description here"></p>
  
  <p>In the Above screen Title  and Description is from the AboutUs Entity and rest of the fields are for GeneralImage Entity.</p>
  
  <p>Now what I want to achieve is when I click on create Button I want to persist data in two entities and have one to one relationship with each other</p>
  
  <p>In order to do this I have written the below code which has the problem</p>
  
  <p><strong>AboutUs.java</strong></p>
  
  <pre><code>package com.model;
  import java.io.Serializable;
  import javax.persistence.CascadeType;
  import javax.persistence.Entity;
  import javax.persistence.GeneratedValue;
  import javax.persistence.FetchType;
  import javax.persistence.GenerationType;
  import javax.persistence.Id;
  import javax.persistence.JoinColumn;
  import javax.persistence.NamedQuery;
  import javax.persistence.OneToOne;
  
  @Entity
  @NamedQuery(name = "AboutUs.findAboutUsByIdWithImages", query = "Select s from AboutUs s where
  s.aboutusid = :aboutusid")       
  public class AboutUs implements Serializable
  {   
    private static final long serialVersionUID = 1L;
    @Id
    @GeneratedValue
    private int aboutusid;
    private String aboutustitle;
    private String description;
  
    public static final String FIND_ABOUTUS_BY_ID_WITH_IMAGES =   
    "AboutUs.findAboutUsByIdWithImages";
    @OneToOne(cascade=CascadeType.ALL,fetch = FetchType.EAGER)
    @JoinColumn(name = "imageid")
  private GeneralImage image;
  //Here Below I have Added Setters and Getters
  ……….
  }
  </code></pre>
  
  <p><strong>GeneralImage</strong></p>
  
  <pre><code>@Entity
  public class GeneralImage {
  
  @Id
  @GeneratedValue
  private int imageid;
  private String fileName;
  private String categoryid;
  private int orderofappearance;
  private String mimetype;
  private int filesize;
  private int foreignkeyid;
  
  @OneToOne(mappedBy="image", cascade=CascadeType.ALL)
  private AboutUs aboutus;
  
  //The Below Are The Setters and Getters
  }
  </code></pre>
  
  <p><strong>The below is My Facelets Image</strong></p>
  
  <pre><code>&lt;!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"   
  "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"&gt;
  &lt;html xmlns="http://www.w3.org/1999/xhtml"
  xmlns:ui="http://java.sun.com/jsf/facelets"
  xmlns:h="http://java.sun.com/jsf/html"
  xmlns:f="http://java.sun.com/jsf/core"
  xmlns:p="http://primefaces.org/ui"&gt;
  &lt;h:body&gt;
    &lt;p:dialog widgetVar="aboutusCreateDialogWidget"
      id="aboutusCreateDialogId" height="400" width="1000" modal="true"
    closable="true" draggable="false" resizable="false" showEffect="puff" hideEffect="fold"
    header="Create About Us"&gt;  
      &lt;h:form id="aboutusCreateDialogForm" prependId="false"&gt;
          &lt;h:panelGrid columns="2"&gt;
              &lt;h:outputText value="* #{msgs.aboutustitle}" /&gt;
              &lt;h:inputText value="#{aboutUsMB.aboutus.aboutustitle}" required="true" label="#
              {msgs.aboutustitle}" &gt;
                  &lt;f:validateLength minimum="4" /&gt;
              &lt;/h:inputText&gt;
  
              &lt;h:outputText value="* #{msgs.aboutusescription}" /&gt;
              &lt;h:inputText value="#{aboutUsMB.aboutus.description}" required="true" label="#
              {msgs.aboutusescription}" /&gt;
  
              &lt;h:outputText value="* #{msgs.categoryName}" /&gt;
              &lt;h:inputText value="#{generalImageMB.generalImage.categoryid}" required="true"   
              label="#{msgs.categoryName}" /&gt;
  
              &lt;h:outputText value="* #{msgs.orderOfAppearance}" /&gt;
              &lt;h:inputText value="#{generalImageMB.generalImage.orderofappearance}" required="true" 
              label="#{msgs.orderOfAppearance}" /&gt;
  
              &lt;h:outputText value="* #{msgs.mimetype}" /&gt;
              &lt;h:inputText value="#{generalImageMB.generalImage.mimetype}" required="true" label="#
              {msgs.mimetype}" /&gt;
  
              &lt;h:outputText value="* #{msgs.fileSize}" /&gt;
              &lt;h:inputText value="#{generalImageMB.generalImage.filesize}" required="true" label="#
              {msgs.fileSize}" /&gt;
          &lt;/h:panelGrid&gt;
  
          &lt;h:panelGrid columns = "2"&gt;
          &lt;p:commandButton value="#{msgs.create}" icon="ui-icon-plus"
                  action="#{aboutUsMB.createAboutus()}"
                  update=":messageGrowl :aboutusForm:aboutusTable"
                  oncomplete="closeDialogIfSucess(xhr, status, args, aboutusCreateDialogWidget,  
                  'aboutusCreateDialogId')" 
                  /&gt;
          &lt;p:commandButton value="#{msgs.cancel}" icon="ui-icon-cancel" actionListener="#
          {aboutusMB.resetAboutus()}" onclick="aboutusCreateDialogWidget.hide();" type="button" /&gt;
  
          &lt;/h:panelGrid&gt;
       &lt;/h:form&gt; 
     &lt;/p:dialog&gt;
   &lt;/h:body&gt;
  &lt;/html&gt;
  </code></pre>
  
  <p><strong>Next I have AboutUsMB
  AboutUsMB
  In AboutUsMB  my createAboutus()</strong> </p>
  
  <pre><code>public void createAboutus() 
  {
  try 
  {   
      aboutus.setImage(generalImage);  //Here I am adding the generalImage object to about so that 
                                       //it can be persisted
      aboutusFacade.save(aboutus);
      closeDialog();
      displayInfoMessageToUser("Created With Success......!");
      loadAboutus();
      resetAboutus();
  }
  catch (Exception e) 
  {
      keepDialogOpen();
      displayErrorMessageToUser("OOPS, We Could Not Create...... Try Again Later......!");
      e.printStackTrace();
  }
  }
  </code></pre>
  
  <p><strong>Later In Façade Classes
  Save Method
  AboutUsFacade</strong></p>
  
  <pre><code>public interface AboutUsFacade {
   //The Below I Am Adding Methods So That I Can Deal With Addition, Deletion and Updation of Users
   public abstract void save(AboutUs aboutus);
  }
  </code></pre>
  
  <p><strong>Façade Implimentation Class
  AboutUsFacadeImp</strong></p>
  
  <pre><code>public class AboutUsFacadeImp implements AboutUsFacade 
  {
  @EJB
  private AboutUsDAO aboutusDAO;
  
  @Override
  public void save(AboutUs aboutus) 
  {
      aboutusDAO.save(aboutus);
  }
  }
  </code></pre>
  
  <p><strong>GenericDAO Class</strong></p>
  
  <pre><code>public abstract class GenericDAO&lt;T&gt; 
  {
    private final static String UNIT_NAME = "SmartRealtorsPU";
  
    @PersistenceContext(unitName = UNIT_NAME)
    private EntityManager em;
    private Class&lt;T&gt; entityClass;
  
    public GenericDAO(Class&lt;T&gt; entityClass) 
    {
      this.entityClass = entityClass;
    }
  
    public void save(T entity) {
    em.persist(entity);
   }
  }
  </code></pre>
  
  <p><strong>So After Using the above Code I am not able to have generalImage data persisted only aboutus data has been persisted also u can see that the imageid which is the field of relationship is NULL</strong></p>
  
  <p><img src="https://i.stack.imgur.com/qeOAH.jpg" alt="enter image description here"></p>
  
  <p><strong>So cud any One rectify that What might be the Problem and also where i am wrong and what needs to be done</strong></p>
  
  <p><strong>Is i am using one form to persist is causing the Problem</strong></p>