At the First I will let u Know what i want to achieve and Later I will Show u the Problem.
In the Above screen Title and Description is from the AboutUs Entity and rest of the fields are for GeneralImage Entity.
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
In order to do this I have written the below code which has the problem
AboutUs.java
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
……….
}
GeneralImage
@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
}
The below is My Facelets Image
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<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">
<h:body>
<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">
<h:form id="aboutusCreateDialogForm" prependId="false">
<h:panelGrid columns="2">
<h:outputText value="* #{msgs.aboutustitle}" />
<h:inputText value="#{aboutUsMB.aboutus.aboutustitle}" required="true" label="#
{msgs.aboutustitle}" >
<f:validateLength minimum="4" />
</h:inputText>
<h:outputText value="* #{msgs.aboutusescription}" />
<h:inputText value="#{aboutUsMB.aboutus.description}" required="true" label="#
{msgs.aboutusescription}" />
<h:outputText value="* #{msgs.categoryName}" />
<h:inputText value="#{generalImageMB.generalImage.categoryid}" required="true"
label="#{msgs.categoryName}" />
<h:outputText value="* #{msgs.orderOfAppearance}" />
<h:inputText value="#{generalImageMB.generalImage.orderofappearance}" required="true"
label="#{msgs.orderOfAppearance}" />
<h:outputText value="* #{msgs.mimetype}" />
<h:inputText value="#{generalImageMB.generalImage.mimetype}" required="true" label="#
{msgs.mimetype}" />
<h:outputText value="* #{msgs.fileSize}" />
<h:inputText value="#{generalImageMB.generalImage.filesize}" required="true" label="#
{msgs.fileSize}" />
</h:panelGrid>
<h:panelGrid columns = "2">
<p:commandButton value="#{msgs.create}" icon="ui-icon-plus"
action="#{aboutUsMB.createAboutus()}"
update=":messageGrowl :aboutusForm:aboutusTable"
oncomplete="closeDialogIfSucess(xhr, status, args, aboutusCreateDialogWidget,
'aboutusCreateDialogId')"
/>
<p:commandButton value="#{msgs.cancel}" icon="ui-icon-cancel" actionListener="#
{aboutusMB.resetAboutus()}" onclick="aboutusCreateDialogWidget.hide();" type="button" />
</h:panelGrid>
</h:form>
</p:dialog>
</h:body>
</html>
Next I have AboutUsMB AboutUsMB In AboutUsMB my createAboutus()
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();
}
}
Later In Façade Classes Save Method AboutUsFacade
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);
}
Façade Implimentation Class AboutUsFacadeImp
public class AboutUsFacadeImp implements AboutUsFacade
{
@EJB
private AboutUsDAO aboutusDAO;
@Override
public void save(AboutUs aboutus)
{
aboutusDAO.save(aboutus);
}
}
GenericDAO Class
public abstract class GenericDAO<T>
{
private final static String UNIT_NAME = "SmartRealtorsPU";
@PersistenceContext(unitName = UNIT_NAME)
private EntityManager em;
private Class<T> entityClass;
public GenericDAO(Class<T> entityClass)
{
this.entityClass = entityClass;
}
public void save(T entity) {
em.persist(entity);
}
}
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
So cud any One rectify that What might be the Problem and also where i am wrong and what needs to be done
Is i am using one form to persist is causing the Problem
I had the same problem as you, you are mapping the bidirectional mapping on only one side.
In your AboutUsMB createAboutus() try to do the following:
Instead of
do