1 package org.apache.ojb.tutorial5; 2 3 17 18 import java.util.Collection ; 19 20 import javax.jdo.PersistenceManager; 21 import javax.jdo.PersistenceManagerFactory; 22 import javax.jdo.Query; 23 import javax.jdo.Transaction; 24 25 28 public class UCDeleteProduct extends AbstractUseCase 29 { 30 35 public UCDeleteProduct(PersistenceManagerFactory factory) 36 { 37 super(factory); 38 } 39 40 45 public String getDescription() 46 { 47 return "Delete a product entry"; 48 } 49 50 53 public void apply() 54 { 55 String in = readLineWithMessage("Delete Product with id:"); 56 int id = Integer.parseInt(in); 57 Transaction tx = null; 58 59 try 60 { 61 PersistenceManager manager = factory.getPersistenceManager(); 63 64 tx = manager.currentTransaction(); 65 tx.begin(); 66 67 Query query = manager.newQuery(Product.class, "id == " + id); 69 70 Collection result = (Collection ) query.execute(); 72 73 if (result.size() == 0) 75 { 76 System.out.println("did not find a Product with id=" + id); 77 tx.rollback(); 78 manager.close(); 79 return; 80 } 81 else 83 { 84 Product toBeDeleted = (Product) result.iterator().next(); 85 86 manager.deletePersistent(toBeDeleted); 87 tx.commit(); 88 manager.close(); 89 } 90 } 91 catch (Throwable t) 92 { 93 tx.rollback(); 95 t.printStackTrace(); 96 } 97 } 98 } 99 | Popular Tags |