简体   繁体   English

从单个html表单(jsf页)更新不同的jpa实体

[英]updating different jpa entities from a single html form (jsf page)

I have been reviewing some samples posted online and they all do the simple CRUD. 我一直在审查一些在线发布的示例,它们都执行简单的CRUD。

1 jsf page = 1 entity = 1 table.

most of the time, this is what I see. 大多数时候,这就是我所看到的。 but what if you only have 1 jsf page with 1 form, and you need to supply data to 3 entities. 但是如果您只有1个带有1个表单的jsf页面,并且需要将数据提供给3个实体,该怎么办? having form fields such as name, company and hobby. 具有诸如姓名,公司和爱好的表格字段。

their values need to be put to entities 他们的价值需要放在实体上

person.name, work.company_name and other_info.hobby.

is this done automatically by binding? 这是通过绑定自动完成的吗? or we need to do some manual assigning of values? 还是我们需要手动分配值? please shed some light, i am a kind of confused right now 请说明一下,我现在有点困惑

I'm not sure if I see the problem. 我不确定是否看到问题。 You could just make them properties of the same backing bean: 您可以使它们成为同一个支持bean的属性:

@ManagedBean
@ViewScoped
public class Profile {

    private Person person;
    private Work work;
    private OtherInfo otherInfo;

    // ...
}

with

<h:inputText value="#{profile.person.name}" />
<h:inputText value="#{profile.work.companyName}" />
<h:inputText value="#{profile.otherInfo.hobby}" />

Or if Work and OtherInfo have an @OneToOne relationship with Person (in real world, they undoubtedly have): 或者,如果WorkOtherInfoPerson具有@OneToOne关系(在现实世界中,它们无疑具有):

@ManagedBean
@ViewScoped
public class Profile {

    private Person person; // Has in turn Work and OtherInfo properties.

    // ...
}

with

<h:inputText value="#{profile.person.name}" />
<h:inputText value="#{profile.person.work.companyName}" />
<h:inputText value="#{profile.person.otherInfo.hobby}" />

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM