mercredi 10 juin 2015

JSF Ajax Form and JPA Version Column isn't updating

The TL;DR is that I'm submitting a form via f:ajax but the new jpa version number from JPA isn't being rendered correctly.

Form snippet:

                    <div class="control-group">
                        <o:outputLabel
                            id="versionInputTextLabel"
                            for="versionInputText"
                            value="Version" />
                        <div class="controls">
                            <h:inputText
                                id="versionInputText"
                                styleClass="input-xlarge"
                                value="#{leadMB.lead.version}"
                                disabled="true" />
                        </div>
                    </div>
                    <div class="control-group">
                        <o:outputLabel
                            id="leadFirstNameInputTextLabel"
                            styleClass="control-label"
                            for="leadFirstNameInputText"
                            value="First Name" />
                        <div class="controls">
                            <h:inputText
                                id="leadFirstNameInputText"
                                styleClass="input-xlarge"
                                placeholder="Lead Name"
                                value="#{leadMB.lead.firstName}">
                            </h:inputText>
                            <h:message
                                id="leadFirstNameInputTextMessage"
                                for="leadFirstNameInputText"
                                errorClass="blocked alert alert-error"
                                fatalClass="blocked alert alert-error"
                                infoClass="blocked alert alert-info"
                                warnClass="blocked alert" />
                        </div>
                    </div>
                    <div class="control-group">
                    <br />
                    <h:message
                        id="updateDemographicInformationCommandLinkMessage"
                        for="updateDemographicInformationCommandLink"
                        errorClass="blocked alert alert-error"
                        fatalClass="blocked alert alert-error"
                        infoClass="blocked alert alert-success"
                        warnClass="blocked alert" />
                    <h:commandLink
                        id="updateDemographicInformationCommandLink"
                        styleClass="btn btn-default"
                        actionListener="#{leadMB.updateLead}"
                        value="Save">
                        <f:ajax
                            event="click"
                            execute="@form"
                            render="@form" />
                    </h:commandLink>
                </div>

View Backing bean:

@ManagedBean
@ViewScoped
public class LeadMB implements Serializable {
    private static final long serialVersionUID = 1L;
    @Inject
    private Logger log;
    @Inject
    private LeadService leadService;
    // == Page objects below ==
    private Lead lead;

    public void updateLead() {
        log.debug("updateLead()");
        lead = leadService.createOrUpdateLead(lead);
        Messages.addInfo("leadDemographicForm:updateDemographicInformationCommandLink", "Update Sucessful");
    }

    public Lead getLead() {
        return lead;
    }

    public void setLead(Lead lead) {
        this.lead = lead;
    }
}

Service:

@Stateless
@TransactionAttribute(TransactionAttributeType.REQUIRED)
public class LeadService {
    @PersistenceContext(name = "joose")
    private EntityManager em;

    public Lead createOrUpdateLead(Lead lead) {
        if (lead.getId() != null) {
            em.merge(lead);
        } else {
            em.persist(lead);
        }
        return lead;
    }

What's happening is that my field updates correctly render to the frontend, but the version column stays the same. So if you hit the same button twice, you'll get an OptimisticLockException because the Version column has been advanced.

How do you deal with this? Thanks!

Aucun commentaire:

Enregistrer un commentaire