2012-05-03 69 views
0

我是Java EE的初學者,我試圖創建一個在數據庫中保留用戶實體的應用程序。我有以下代碼:無法在數據庫中堅持實體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); 
    } 

} 

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> 

雖然from正在正確顯示,但單擊Submit按鈕時沒有提供任何內容。沒有服務器錯誤。它只是沒有信號。如果我在代碼中有任何錯誤,你能幫我嗎?

謝謝

+1

使用調試器瞭解當您單擊該按鈕時發生了什麼。或者至少添加日誌語句來查看執行的內容。 –

+0

您的託管bean是否被識別?你使用JSF或CDI的註釋嗎? –

回答

2

你有太多的事情在這裏一次。

你說什麼都沒有發生,當你點擊一個提交按鈕。爲什麼你需要一個UI來驗證持久層是否正常工作?編寫一個單元測試,除了練習持久性代碼之外什麼也不做。隔離所做的工作,直到找出問題爲止。

正如你寫的,你不知道你的持久性是壞的,會話bean還是UI。