我在 Netbeans 中使用 JPA 2.0。我正在使用实体。如果我的数据库没有表,那么它应该从实体创建表。这是我的代码
public class BankServlet extends HttpServlet {
@EJB
private BankServiceBeanRemote bankServiceBean;
protected void processRequest(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
int custId = Integer.parseInt(request.getParameter("id"));
bankServiceBean.createCustomers();
Customer cust = bankServiceBean.findCustomer(custId);
response.setContentType("text/html;charset=UTF-8");
....
} //end of processRequest
} //end of class BankServlet
这是我的豆子
@Stateless
public class BankServiceBean implements BankServiceBeanRemote {
@PersistenceContext(unitName = "Bank_JPA-ejbPU")
private EntityManager em;
@Override
public void createCustomers() {
Referee r1 = new Referee();
r1.setId(1);
r1.setName("SIR JOHN DEED");
r1.setComments("JUDGE");
em.persist(r1);
Customer c1 = new Customer();
c1.setId(1);
c1.setFirstName("SIMON");
c1.setLastName("KING");
c1.setReferee(r1);
......
}
} //end of class BankServiceBean
这是我的裁判实体
@Entity
public class Referee implements Serializable {
private static final long serialVersionUID = 1L;
private int id;
private String name;
private String comments;
public Referee() {
}
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
// other getter setters
} //end of class Referee
当我运行代码时,我得到以下异常
Exception [EclipseLink-4002] (Eclipse Persistence Services - 2.0.1.v20100213-r6600): org.eclipse.persistence.exceptions.DatabaseException
Internal Exception: com.mysql.jdbc.exceptions.jdbc4.MySQLSyntaxErrorException: Table 'dbbank.referee' doesn't exist
Error Code: 1146
Call: INSERT INTO REFEREE (ID, NAME, COMMENTS) VALUES (?, ?, ?)
bind => [3, MICHAEL ELLIS, MAJOR SHAREHOLDER OF THIS BANK]
Query: InsertObjectQuery(pk.mazars.basitMahmood.entity.Referee[id=3])
at org.eclipse.persistence.exceptions.DatabaseException.sqlException(DatabaseException.java:324)
如果表不存在,那么它应该自动创建表。难道我做错了什么?
谢谢