请帮我看看我哪里错了。我试图使用EJB依赖注入在我的数据库中创建一个新的成员实例。我有以下代码由Netbeans(AbstractFacade)生成
package session;
import java.util.List;
import javax.persistence.EntityManager;
/**
*
* @author JO
*/
public abstract class AbstractFacade<T> {
private Class<T> entityClass;
public AbstractFacade(Class<T> entityClass) {
this.entityClass = entityClass;
}
protected abstract EntityManager getEntityManager();
public void create(T entity) {
getEntityManager().persist(entity);
}
//removed some code for clarity here, I'm only trying to understand create
}
然后我有我的EJB Member1的代码,我在其中添加了一个方法,它将两个STrings作为参数,名字和姓氏。
package entity;
import java.io.Serializable;
import java.util.Collection;
import javax.persistence.Basic;
import javax.persistence.CascadeType;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.Lob;
import javax.persistence.NamedQueries;
import javax.persistence.NamedQuery;
import javax.persistence.OneToMany;
import javax.persistence.OneToOne;
import javax.persistence.Table;
import javax.validation.constraints.NotNull;
import javax.validation.constraints.Size;
import javax.xml.bind.annotation.XmlRootElement;
import javax.xml.bind.annotation.XmlTransient;
/**
*
* @author JO
*/
@Entity
@Table(name = "member")
@XmlRootElement
public class Member1 implements Serializable {
private static final long serialVersionUID = 1L;
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
@Basic(optional = false)
@Column(name = "id_member")
private Integer idMember;
@Basic(optional = false)
@NotNull
@Size(min = 1, max = 45)
@Column(name = "first_name")
private String firstName;
@Basic(optional = false)
@NotNull
@Size(min = 1, max = 45)
@Column(name = "surname")
private String surname;
@Basic(optional = false)
@NotNull
@Column(name = "number_of_credits")
private short numberOfCredits;
// @Pattern(regexp="^\\(?(\\d{3})\\)?[- ]?(\\d{3})[- ]?(\\d{4})$", message="Invalid phone/fax format, should be as xxx-xxx-xxxx")//if the field contains phone or fax number consider using this annotation to enforce field validation
@Size(max = 45)
@Column(name = "phone")
private String phone;
// @Pattern(regexp="[a-z0-9!#$%&'*+/=?^_`{|}~-]+(?:\\.[a-z0-9!#$%&'*+/=?^_`{|}~-]+)*@(?:[a-z0-9](?:[a-z0-9-]*[a-z0-9])?\\.)+[a-z0-9](?:[a-z0-9-]*[a-z0-9])?", message="Invalid email")//if the field contains email address consider using this annotation to enforce field validation
@Size(max = 45)
@Column(name = "email")
private String email;
@Lob
@Column(name = "image")
private byte[] image;
@Basic(optional = false)
@NotNull
@Size(min = 1, max = 45)
@Column(name = "password")
private String password;
@OneToMany(cascade = CascadeType.ALL, mappedBy = "member1")
private Collection<AttendedSession> attendedSessionCollection;
@OneToOne(cascade = CascadeType.ALL, mappedBy = "member1")
private NextSession nextSession;
@OneToMany(cascade = CascadeType.ALL, mappedBy = "memberIdMember")
private Collection<Block> blockCollection;
@OneToMany(cascade = CascadeType.ALL, mappedBy = "member1")
private Collection<MemberWithSession> memberWithSessionCollection;
public Member1() {
}
// added this to allow creation of Member in way that suits me
public Member1(String firstName, String surname){
this.firstName = firstName;
this.surname = surname;
}
///removed other generated code for clarity here
}
然后我再次生成了这个Member1Facade代码,我添加了代码来创建并将成员保存到数据库中。
@Stateless
public class Member1Facade extends AbstractFacade<Member1> {
@PersistenceContext(unitName = "fitnessPU")
private EntityManager em;
@Override
protected EntityManager getEntityManager() {
return em;
}
public Member1Facade() {
super(Member1.class);
}
// added this to allow creation of Member in way that suits me
public Member1 newMember(String firstName, String surname){
Member1 member = new Member1();
member.setFirstName(firstName);
member.setSurname(surname);
em.persist(member);
return member;
}
}
最后,这是我的addMember.java代码,它应该从JSP表单获取数据,并且想法是它将通过依赖注入添加到数据库。
package servlets;
import entity.Member1;
import java.io.IOException;
import javax.ejb.EJB;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import session.Member1Facade;
public class addMember extends HttpServlet {
@EJB
Member1Facade memberFacade;
Member1 member;
/**
* Processes requests for both HTTP <code>GET</code> and <code>POST</code>
* methods.
*
* @param request servlet request
* @param response servlet response
* @throws ServletException if a servlet-specific error occurs
* @throws IOException if an I/O error occurs
*/
/**
* declare password as member
* @param request
* @param response
* @throws javax.servlet.ServletException
* @throws java.io.IOException
*/
protected void processRequest(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
try{
String firstname = request.getParameter("first");
String surname = request.getParameter("surname");
String email = request.getParameter("email");
String phone = request.getParameter("phone");
memberFacade.newMember(firstname, surname);
}
catch (Exception e){
System.out.println("error");
}
//some further code removed for brevity
}
这一切都编译但没有什么事情发生。我无法弄清楚我做错了什么,而且我花了几天时间在网上看。 我真的很感激我的错误。