I would like to be updated in the database only those fields that are specified in the form. In entity "account" I use annotation @DynamicUpdate
.
Class AccountMB
(@RequestScoped
) with methods:
public String update() {
getDao().update(getInstance());
return SUCCESS;
}
public Account getInstance() {
//setId(new Long("1"));
if (instance == null) {
if (id != null) {
instance = loadInstance();
} else {
instance = createInstance();
}
}
return instance;
}
And form form.xhtml:
<f:metadata>
<f:viewParam name="accountId" value="#{accountMB.id}" />
</f:metadata>
<h:form prependId="false">
<h:inputHidden id="accountId" value="#{accountMB.id}"/>
<h:inputHidden id="id" value="#{accountMB.instance.id}"/>
<h:inputText id="firstName" value="#{accountMB.instance.firstName}"/>
<h:commandButton type="submit" action="#{accountMB.update}" value="Save">
<f:setPropertyActionListener target="#{accountMB.id}" value="1" />
</h:commandButton>
</h:form>
I open page form.xhtml?accountId=1
, in the form of loaded data, click to "Save". It writes an error:
java.sql.SQLException: ORA-01407: cannot update ("MYBD"."ACCOUNTS"."EMAIL") to NULL
If in getInstance()
method uncomment setId(new Long("1"));
, the data is saved.
And if I use annotation @ViewScoped
in AccountMB
, the data is saved.
But I want to use annotation @RequestScoped
.
I understand, I triggered createInstance();
and email field is not filled.
Tell me how to pass id
to load method loadInstance();
.
I use <f:setPropertyActionListener target="#{accountMB.id}" value="1" />
and
<h:inputHidden id="accountId" value="#{accountMB.id}"/>
. But it is not work. Please help me.