我在Netbeans中使用JPA 2.0。我正在使用實體。如果我的數據庫沒有表,那麼它應該從實體創建表。這裏是我的代碼表不是使用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)
關注我的裁判實體如果表不存在,那麼它應該創建t能夠自動。難道我做錯了什麼?
謝謝
hhmm所以如果我有一個數據庫,IKE我創建了一個名爲dbbank的數據庫在mysql中使用netbeans。然後我創建了一個實體並將其與dbbank數據庫鏈接起來。現在我有數據庫和實體鏈接。現在持久性API爲我自動創建表? – Basit
只有當您告訴EclipseLink爲您創建數據庫表時。閱讀我給你的鏈接。這裏都有解釋。 –