Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/jpa/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何使用JPA JSF在一对一关系中持久化数据_Jsf_Jpa_Primefaces_One To One_Facade - Fatal编程技术网

如何使用JPA JSF在一对一关系中持久化数据

如何使用JPA JSF在一对一关系中持久化数据,jsf,jpa,primefaces,one-to-one,facade,Jsf,Jpa,Primefaces,One To One,Facade,首先我会让你知道我想要实现什么,然后我会告诉你问题所在 在上面的屏幕中,标题和描述来自AboutUs实体,其余字段用于GeneralImage实体 现在,我想要实现的是,当我单击create按钮时,我希望将数据持久化到两个实体中,并且彼此之间具有一对一的关系 为了做到这一点,我写了下面的代码有问题 AboutUs.java 通用图像 下面是我的Facelets图像 后来在外立面课上 保存方法 关于正面 Façade Implementation类 关于Facadeimp GenericDAO类

首先我会让你知道我想要实现什么,然后我会告诉你问题所在

在上面的屏幕中,标题和描述来自AboutUs实体,其余字段用于GeneralImage实体

现在,我想要实现的是,当我单击create按钮时,我希望将数据持久化到两个实体中,并且彼此之间具有一对一的关系

为了做到这一点,我写了下面的代码有问题

AboutUs.java

通用图像

下面是我的Facelets图像

后来在外立面课上 保存方法 关于正面

Façade Implementation类 关于Facadeimp

GenericDAO类

因此,在使用上述代码后,我无法持久化generalImage数据,只有aboutus数据被持久化了,而且您可以看到作为关系字段的imageid为空

因此,如果有人纠正可能存在的问题,我错在哪里,需要做什么


是我在使用一个表单来持久化是导致问题的原因

我遇到了与您相同的问题,您只在一侧映射双向映射

在AboutsMB createAboutus中,尝试执行以下操作:

而不是

aboutus.setImage(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
}
<!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>
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();
}
}
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);
}
public class AboutUsFacadeImp implements AboutUsFacade 
{
@EJB
private AboutUsDAO aboutusDAO;

@Override
public void save(AboutUs aboutus) 
{
    aboutusDAO.save(aboutus);
}
}
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);
 }
}
aboutus.setImage(generalImage);
aboutus.setImage(generalImage);
generalImage.setAboutus(aboutus);