interboy
interboy

Reputation: 886

Cannot persist entity in database Java EE

I am a beginner in Java EE and I am trying to create an application that persists a user entity in the database. I have the following code:

User Entity

    @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;
    }

}

User Stateless session 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);
    }

}

User Managed Bean (only create method is implemented)

    public class UserMBean {

    @EJB
    userEJB userSessionBean;

    private User user;



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

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

}

signUp.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>

Although the from is being displayed correctly it gives nothing when the Submit button is clicked. The are no server errors. It simply gives no signal. Can you please help me if I have any error with the code?

Thanks

Upvotes: 0

Views: 435

Answers (1)

duffymo
duffymo

Reputation: 308733

You've got too much going on here at once.

You say nothing happens when you click a submit button. Why do you need a UI to validate whether or not your persistence layer is working properly? Write a unit test that does nothing but exercise the persistence code. Isolate what's done until you sort out the problems.

As written, you don't know if your persistence is bad, or the session bean, or the UI.

Upvotes: 2

Related Questions