0

我是 Java EE 的初学者,我正在尝试创建一个在数据库中保留用户实体的应用程序。我有以下代码:

用户实体

    @Entity
    public class User implements Serializable {
        private static final long serialVersionUID = 1L;

        @Id
        @GeneratedValue(strategy=GenerationType.AUTO)
        private int id;

        private String department;

        @Email
        private String email;

        @Column(name="`first name`")
        private String firstName;

        @Column(name="`last name`")
        private String lastName;

        private String password;

        //bi-directional many-to-one association to Unit
        @OneToMany(mappedBy="user")
        private Set<Unit> units;

    public User() {
    }

    public int getId() {
        return this.id;
    }

    public void setId(int id) {
        this.id = id;
    }

    public String getDepartment() {
        return this.department;
    }

    public void setDepartment(String department) {
        this.department = department;
    }

    public String getEmail() {
        return this.email;
    }

    public void setEmail(String email) {
        this.email = email;
    }

    public String getFirstName() {
        return this.firstName;
    }

    public void setFirstName(String firstName) {
        this.firstName = firstName;
    }

    public String getlastNameame() {
        return this.lastName;
    }

    public void setLastName(String lastName) {
        this.lastName = lastName;
    }

    public String getPassword() {
        return this.password;
    }

    public void setPassword(String password) {
        this.password = password;
    }

    public Set<Unit> getUnits() {
        return this.units;
    }

    public void setUnits(Set<Unit> units) {
        this.units = units;
    }

}

用户无状态会话 bean

    @Stateless
public class userEJB {

    @PersistenceContext
    private EntityManager em;
    /**
     * Default constructor. 
     */
    public userEJB() {
        // TODO Auto-generated constructor stub
    }

    public void createUser(User user)
    {
        em.persist(user);
    }

}

用户托管 Bean(只实现了 create 方法)

    public class UserMBean {

    @EJB
    userEJB userSessionBean;

    private User user;



    public UserMBean() {
        this.user = new User();
    }

    public void save()
    {
        userSessionBean.createUser(user);
    }

}

注册.xhtml

 <h:form>
            <h:panelGrid columns = "2">
                <h:outputLabel for="firstName" value="First Name"/>
                <h:inputText id="firstName" value="#{userMBean.user.firstName}" required = "true"/>
                <h:outputLabel for="lastName" value="Last Name"/>
                <h:inputText id="lastName" value="#{userMBean.user.lastName}" required = "true" />
                <h:outputLabel for="department" value="Department"/>
                <h:inputText id="department" value="#{userMBean.user.department}" required = "true"/>
                <h:outputLabel for ="email" value="Email"/>
                <h:inputText id="email" value="#{userMBean.user.email}" required = "true"/>
                <h:outputLabel for="password" value="Password"/>
                <h:inputText id="password" value="#{userMBean.user.password}" required = "true"/>

            </h:panelGrid>
            <h:commandButton action="#{userMBean.save}" value="Submit" />
        </h:form>

尽管 from 正在正确显示,但在单击“提交”按钮时它什么也没给出。没有服务器错误。它根本没有发出任何信号。如果我的代码有任何错误,你能帮我吗?

谢谢

4

1 回答 1

2

你一次有太多事情要做。

当您单击提交按钮时,您说什么都没有发生。为什么需要 UI 来验证持久层是否正常工作?编写一个除了执行持久性代码之外什么都不做的单元测试。隔离已完成的工作,直到您解决问题。

如所写,您不知道您的持久性是坏的,还是会话 bean 或 UI。

于 2012-05-03T09:15:31.847 回答