1

我有一个实体,它在屏幕上显示为可以修改的输入框中的文本。当我提交点击保存的表单时,我希望我的 ejb3 组件处理数据并使用实体管理器持久化或合并它。但是由于某种原因,当我修改数据并点击保存时,数据会通过我的 ejb3 直接在数据库中更新,这当然是不可取的。我的代码如下所示

@Entity
@Table(name = "EMP")
public class Emp implements java.io.Serializable {

private short empno;
private Dept dept;
private String ename;
private String job;
private Short mgr;
private Date hiredate;
private BigDecimal sal;
private BigDecimal comm;

public Emp() {
}

public Emp(short empno) {
    this.empno = empno;
}
public Emp(short empno, Dept dept, String ename, String job, Short mgr,
        Date hiredate, BigDecimal sal, BigDecimal comm) {
    this.empno = empno;
    this.dept = dept;
    this.ename = ename;
    this.job = job;
    this.mgr = mgr;
    this.hiredate = hiredate;
    this.sal = sal;
    this.comm = comm;
}

@Id
@Column(name = "EMPNO", unique = true, nullable = false, precision = 4, scale = 0)
public short getEmpno() {
    return this.empno;
}

public void setEmpno(short empno) {
    this.empno = empno;
}

@ManyToOne(fetch = FetchType.LAZY)
@JoinColumn(name = "DEPTNO")
public Dept getDept() {
    return this.dept;
}

public void setDept(Dept dept) {
    this.dept = dept;
}

@Column(name = "ENAME", length = 10)
@Length(max = 10)
public String getEname() {
    return this.ename;
}

public void setEname(String ename) {
    this.ename = ename;
}

@Column(name = "JOB", length = 9)
@Length(max = 9)
public String getJob() {
    return this.job;
}

public void setJob(String job) {
    this.job = job;
}

@Column(name = "MGR", precision = 4, scale = 0)
public Short getMgr() {
    return this.mgr;
}

public void setMgr(Short mgr) {
    this.mgr = mgr;
}

@Temporal(TemporalType.DATE)
@Column(name = "HIREDATE", length = 7)
public Date getHiredate() {
    return this.hiredate;
}

public void setHiredate(Date hiredate) {
    this.hiredate = hiredate;
}

@Column(name = "SAL", precision = 7)
public BigDecimal getSal() {
    return this.sal;
}

public void setSal(BigDecimal sal) {
    this.sal = sal;
}

@Column(name = "COMM", precision = 7)
public BigDecimal getComm() {
    return this.comm;
}

public void setComm(BigDecimal comm) {
    this.comm = comm;
}

}

    @Stateful
    @Name("workflow")
    @Scope(ScopeType.CONVERSATION)
    public class WorkflowBean implements Workflow
    {
    @Logger private Log log;

    @In StatusMessages statusMessages;


    @PersistenceContext(type=PersistenceContextType.EXTENDED)
    EntityManager entityManager;


    @Out(required=false)
    Emp employee = new Emp();

    @RequestParameter("empEmpno")
    String empNo;


    public void workflow()
    {

        log.info("workflow.workflow() action called");
        statusMessages.add("workflow");

    } 
    @End
    public boolean save(){
       entityManager.merge(emp);
       entityManger.flush();
    }
    @Begin(join=true)
    public boolean populateEmp(){
        entityManager.setFlushMode(FlushModeType.COMMIT);
        System.out.println("The Emp No. is---"+empNo);
        int no = Integer.parseInt(empNo);
        short emp =(short)no;
        employee = entityManager.find(Emp.class, emp);
        entityManager.flush();
        return true;
    }
    public Emp getEmployee() {
        return employee;
    }

    public void setEmployee(Emp employee) {
        this.employee = employee;
    }
    // add additional action methods

    public String getEmpNo() {
        return empNo;
    }

    public void setEmpNo(String empNo) {
        this.empNo = empNo;
    }
    @Remove
    @Destroy
    public void destroy() {

    }
}

我的观点看起来像

<h:form id="emp" styleClass="edit">

        <rich:panel>
            <f:facet name="header">Edit Emp</f:facet>

            <s:decorate id="empnoField" template="layout/edit.xhtml">
                <ui:define name="label">Empno</ui:define>
                <h:inputText id="empno"
                       required="true"
                          value="#{workflow.employee.empno}">
                </h:inputText>
            </s:decorate>


            <s:decorate id="commField" template="layout/edit.xhtml">
                <ui:define name="label">Comm</ui:define>
                <h:inputText id="comm"
                          value="#{workflow.employee.comm}"
                           size="14">
                </h:inputText>
            </s:decorate>



            <s:decorate id="enameField" template="layout/edit.xhtml">
                <ui:define name="label">Ename</ui:define>
                <h:inputText id="ename"
                           size="10"
                      maxlength="10"
                          value="#{workflow.employee.ename}">
                </h:inputText>
            </s:decorate>


            <s:decorate id="hiredateField" template="layout/edit.xhtml">
                <ui:define name="label">Hiredate</ui:define>
                <rich:calendar id="hiredate"
                          value="#{workflow.employee.hiredate}" datePattern="MM/dd/yyyy" />
            </s:decorate>


            <s:decorate id="jobField" template="layout/edit.xhtml">
                <ui:define name="label">Job</ui:define>
                <h:inputText id="job"
                           size="9"
                      maxlength="9"
                          value="#{workflow.employee.job}">
                </h:inputText>
            </s:decorate>


            <s:decorate id="mgrField" template="layout/edit.xhtml">
                <ui:define name="label">Mgr</ui:define>
                <h:inputText id="mgr"
                          value="#{workflow.employee.mgr}">
                </h:inputText>
            </s:decorate>


            <s:decorate id="salField" template="layout/edit.xhtml">
                <ui:define name="label">Sal</ui:define>
                <h:inputText id="sal"
                          value="#{workflow.employee.sal}"
                           size="14">
                </h:inputText>
            </s:decorate>
            <s:decorate id="deptField" template="layout/edit.xhtml">
                <ui:define name="label">Department</ui:define>
                <h:inputText id="dname"
                          value="#{workflow.deptName}">
                </h:inputText>
            </s:decorate>
            <div style="clear:both">
                <span class="required">*</span>
                required fields
            </div>

        </rich:panel>

        <div class="actionButtons">

            <h:commandButton id="save"
                          value="Save"
                         action="#{workflow.save}"
                       rendered="true"/>
        </div>
    </h:form>
4

1 回答 1

0

[很难在评论中解决您的回复中的问题。]

有几件事我不明白以及为什么这样做。

  • 使用瞬态字段,它将增加在持久化时将它们添加到实体对象的开销。
  • 为什么自动提交会导致问题且无法实施。

不管这些,你可以试试

  • 使用 使实体分离entityManager.detach(entity)或清除持久性上下文entityManager.clear(),分离所有底层实体。但前一个更有利。
  • 可以手动管理事务而不是容器。在您可以控制操作的地方使用 BMT。
于 2013-02-13T05:22:45.490 回答