You are viewing a plain text version of this content. The canonical link for it is here.
Posted to ojb-dev@db.apache.org by th...@apache.org on 2003/12/05 23:47:04 UTC
cvs commit: db-ojb/src/jdori/org/apache/ojb/tutorial5 Application.java UCEditProduct.java UCListAllProducts.java
thma 2003/12/05 14:47:04
Modified: src/jdori/org/apache/ojb/tutorial5 Application.java
UCEditProduct.java UCListAllProducts.java
Log:
Let the tutorial app use the repaired features. Now we do not need any PersistenceBroker calls!
Revision Changes Path
1.6 +0 -2 db-ojb/src/jdori/org/apache/ojb/tutorial5/Application.java
Index: Application.java
===================================================================
RCS file: /home/cvs/db-ojb/src/jdori/org/apache/ojb/tutorial5/Application.java,v
retrieving revision 1.5
retrieving revision 1.6
diff -u -r1.5 -r1.6
--- Application.java 13 Sep 2003 15:03:00 -0000 1.5
+++ Application.java 5 Dec 2003 22:47:04 -0000 1.6
@@ -33,8 +33,6 @@
try
{
factory = new OjbStorePMF();
- //manager = factory.getPersistenceManager();
-
}
catch (Throwable t)
{
1.6 +17 -24 db-ojb/src/jdori/org/apache/ojb/tutorial5/UCEditProduct.java
Index: UCEditProduct.java
===================================================================
RCS file: /home/cvs/db-ojb/src/jdori/org/apache/ojb/tutorial5/UCEditProduct.java,v
retrieving revision 1.5
retrieving revision 1.6
diff -u -r1.5 -r1.6
--- UCEditProduct.java 30 Mar 2003 20:11:34 -0000 1.5
+++ UCEditProduct.java 5 Dec 2003 22:47:04 -0000 1.6
@@ -1,15 +1,15 @@
package org.apache.ojb.tutorial5;
+import java.util.Collection;
+
import javax.jdo.PersistenceManagerFactory;
import javax.jdo.PersistenceManager;
-//import javax.jdo.Query;
+import javax.jdo.Query;
-import org.apache.ojb.broker.Identity;
-import org.apache.ojb.broker.PersistenceBroker;
-import org.apache.ojb.broker.PersistenceBrokerFactory;
/**
*
* @author Travis Reeder - travis@spaceprogram.com
+ * @author Thomas Mahler
*/
public class UCEditProduct extends AbstractUseCase
{
@@ -27,30 +27,29 @@
public void apply()
{
PersistenceManager manager = null;
+
+ // ask user which object should edited
String in = readLineWithMessage("Edit Product with id:");
int id = Integer.parseInt(in);
- // We don't have a reference to the selected Product.
- // So first we have to lookup the object,
Product toBeEdited;
try
{
- // 1. Build a query to look up product with the id
+ // We don't have a reference to the selected Product.
+ // So we have to look it up first,
- manager = factory.getPersistenceManager();
-
- Product example = new Product();
- example.setId(id);
- PersistenceBroker broker = PersistenceBrokerFactory.defaultPersistenceBroker();
- Identity oid = new Identity(example, broker);
-
+ // 1. start transaction
+ manager = factory.getPersistenceManager();
manager.currentTransaction().begin();
+
+ // 2. Build a query to look up product by the id
+ Query query = manager.newQuery(Product.class, "id == " + id);
- //Query query = manager.newQuery(Product.class, "id == " + id);
- //toBeEdited = (Product) query.execute();
+ // 3. execute query
+ Collection result = (Collection) query.execute();
+ toBeEdited = (Product) result.iterator().next();
- toBeEdited = (Product) manager.getObjectById(oid, false);
if (toBeEdited == null)
{
System.out.println("did not find a matching instance...");
@@ -58,7 +57,7 @@
return;
}
- // 5. edit the existing entry
+ // 4. edit the existing entry
System.out.println("please edit the product entry");
in =
readLineWithMessage(
@@ -74,17 +73,11 @@
+ toBeEdited.getStock()
+ "):");
toBeEdited.setStock(Integer.parseInt(in));
-
-
-
- //manager.makePersistent(toBeEdited);
-
manager.currentTransaction().commit();
}
catch (Throwable t)
{
// rollback in case of errors
- //broker.abortTransaction();
manager.currentTransaction().rollback();
t.printStackTrace();
}
1.4 +3 -3 db-ojb/src/jdori/org/apache/ojb/tutorial5/UCListAllProducts.java
Index: UCListAllProducts.java
===================================================================
RCS file: /home/cvs/db-ojb/src/jdori/org/apache/ojb/tutorial5/UCListAllProducts.java,v
retrieving revision 1.3
retrieving revision 1.4
diff -u -r1.3 -r1.4
--- UCListAllProducts.java 8 Apr 2003 20:19:27 -0000 1.3
+++ UCListAllProducts.java 5 Dec 2003 22:47:04 -0000 1.4
@@ -1,6 +1,5 @@
package org.apache.ojb.tutorial5;
-
import javax.jdo.PersistenceManager;
import javax.jdo.PersistenceManagerFactory;
import javax.jdo.Query;
@@ -51,7 +50,8 @@
}
while (iter.hasNext())
{
- System.out.println(iter.next());
+ Product p = (Product) iter.next();
+ System.out.println(p.toString());
}
manager.currentTransaction().commit();
}
@@ -64,7 +64,7 @@
manager.close();
}
}
-
+
/** get descriptive information on use case*/
public String getDescription()
{
---------------------------------------------------------------------
To unsubscribe, e-mail: ojb-dev-unsubscribe@db.apache.org
For additional commands, e-mail: ojb-dev-help@db.apache.org