可能是一個新手Java的東西,但它在這裏。使用JPA調用INSERT存儲過程到Oracle 11g數據庫。在將數據傳送到數據庫表格方面,一切正常。Java Swing/JPA/Oracle插入兩次
我已經將JPA代碼包裝到Java Swing應用程序中。我的問題是數據不知何故被提交兩次。我提交數據時顯示JOptionPane.showMessageDialog
。那隻顯示一次。但是當我查詢表時,表中有兩個條目。這裏是我的代碼....
按鈕代碼
JButton btnSubmit = new JButton("Submit");
btnSubmit.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent arg0) {
AddNewCustomer();
}
});
btnSubmit.setBounds(24, 341, 91, 23);
frmNewCustomer.getContentPane().add(btnSubmit);
操作方法
public void AddNewCustomer() {
Customer.AddCustomer(this.txtFirstName.getText(),
this.txtLastName.getText(), this.txtAddress.getText(),
this.txtCity.getText(), this.txtState.getText(),
this.txtEmail.getText(), this.txtPhone.getText());
JOptionPane.showMessageDialog(frmNewCustomer, "Added new customer",
"Status", 1);
}
protected static void AddCustomer(String sFirstName, String sLastName,
String sAddress, String sCity, String sState, String sEmail,
String sPhone) {
try {
factory = Persistence
.createEntityManagerFactory(PERSISTENCE_UNIT_NAME);
EntityManager em = factory.createEntityManager();
em.getTransaction().begin();
Query q = em.createNamedQuery("AddCustomer");
q.setParameter("p_PHONE", sPhone);
q.setParameter("p_STATE", sState);
q.setParameter("p_FIRST_NAME", sFirstName);
q.setParameter("p_ADDRESS", sAddress);
q.setParameter("p_EMAIL", sEmail);
q.setParameter("p_CITY", sCity);
q.setParameter("p_LAST_NAME", sLastName);
int r = q.executeUpdate();
em.getTransaction().commit();
List results = q.getResultList();
if (results != null) {
Object id = results.get(0);
if (id != null) {
System.out.println(id.toString());
}
}
em.close();
} catch (Exception e) {
e.printStackTrace();
}
}
如果你所刪除'列出結果= q.getResultList();'(及以下幾行)? – 2012-07-12 09:35:02
我這樣做的共振是從Insert SPROC中獲取新創建的ID和OUT參數。 SPROC有一個Sequence.nextVal的觸發器。 – Beachdog 2012-07-12 16:45:38