我创建了一个数据库。该数据库有3个表
然后我创建了一个EJB3项目,包括每个表的实体的实体和会话bean。
我使用了JSF2并且我有一个管理Bean来插入那些表
这是将数据传递给托管bean的页面。
<h:form>
<h:messages globalOnly="true"/>
<h:inputText id="username" value="#{addBean.username}"/>
<h:inputText id="password" value="#{addBean.userpass}"/>
<h:inputText id="rolename" value="#{addBean.rolename}"/>
<h:commandButton id="add" value="add" action="#{addBean.ADD()}"/>
</h:form>
这是我的管理Bean:
package demo;
import javax.ejb.EJB;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.RequestScoped;
/**
*
* @author Sony
*/
@ManagedBean
@RequestScoped
public class AddBean {
@EJB
private UsersFacadeLocal usersFacade;
@EJB
private RolesFacadeLocal rolesFacade;
/**
* Creates a new instance of AddBean
*/
public AddBean() {
}
public void ADD()
{
Users u = new Users(username, userpass);
Roles r = new Roles(rolename);
usersFacade.create(u);
rolesFacade.create(r);
Users uC = usersFacade.find(username);
Roles rC = rolesFacade.find(rolename);
uC.getRolesCollection().add(rC);
rC.getUsersCollection().add(uC);
}
private String username;
private String userpass;
private String rolename;
public String getUsername() {
return username;
}
public void setUsername(String username) {
this.username = username;
}
public String getUserpass() {
return userpass;
}
public void setUserpass(String userpass) {
this.userpass = userpass;
}
public String getRolename() {
return rolename;
}
public void setRolename(String rolename) {
this.rolename = rolename;
}
}