do you use OJB default settings (OJB.properties)? Your test seems ok.
regards, Armin
Stijn de Witt wrote:
I have created a test program that attempts to load a Candidate from the database with lastName="Tester". If none is found it creates a new one. It then sets the title field to a random string and stores the object.
On the first run, no candidate is found (as expected) and a new one is created and stored correctly. On the second run, the candidate stored before is found correctly, and the title is set to a new random string. broker.store() is called and finishes with no errors. When I look in the database however, the title field is not changed...
What am I doing wrong?
-Stijn
package nl.bergland.codamo;
import java.util.*; import org.apache.log4j.LogManager; import org.apache.log4j.Logger; import org.apache.log4j.PropertyConfigurator; import org.apache.ojb.broker.PersistenceBrokerFactory; import org.apache.ojb.broker.PersistenceBroker; import org.apache.ojb.broker.query.*;
public class OjbTester { private static final Logger logger = LogManager.getLogger(OjbTester.class.getName()); /** Creates a new instance of Test */ public OjbTester() { }
public static void main(String[] args) { PropertyConfigurator.configure("log4j.properties");
logger.info("main: Starting..."); logger.info("main: Getting persistence broker..."); PersistenceBroker broker = null; try { broker = PersistenceBrokerFactory.defaultPersistenceBroker(); logger.info("main: Creating query criteria..."); Criteria crit = new Criteria(); logger.info("main: Set criteria lastName==\"Tester\"..."); crit.addEqualTo("lastName", "Tester"); logger.info("main: Create query..."); Query q = QueryFactory.newQuery(Candidate.class, crit); logger.info("main: Fire query for a Candidate with lastName == \"Tester\"..."); Collection results = broker.getCollectionByQuery(q); logger.info("main: Get result (if any)..."); broker.close();
broker = PersistenceBrokerFactory.defaultPersistenceBroker(); logger.info("main: Starting transaction..."); broker.beginTransaction(); Iterator it = results.iterator(); Candidate candidate; if (it.hasNext()) { logger.info("main: Found a candidate..."); candidate = (Candidate) it.next(); } else { logger.info("main: No candidate found, create new..."); candidate = new Candidate(); candidate.setFirstName("Tommy"); candidate.setLastName("Tester"); }
logger.info("main: Assign availableFrom as current Date..."); candidate.setAvailableFrom(new java.sql.Date(new java.util.Date().getTime()));
logger.info("main: Set random title..."); Random rand = new Random(); byte[] bytes = new byte[4]; rand.nextBytes(bytes); String title = new String(); for (int i=0; i<bytes.length; i++) { Byte bt = new Byte(bytes[i]); title += bt.toString(); }
logger.info("main: Set candidate.title == " + title + "..."); candidate.setTitle(title);
logger.info("main: Trying to store candidate..."); broker.store(candidate); logger.info("main: Stored candidate " + candidate.getId());
logger.info("main: Commit transaction..."); broker.commitTransaction(); } catch(Exception e) { if (broker == null) logger.error("main: ERROR getting persistence broker: " + e.getMessage()); else { logger.error("main: ERROR: " + e.getMessage()); if (broker.isInTransaction()) { logger.error("main: Rollback Transaction..."); broker.abortTransaction(); } } } finally { if (broker != null) broker.close(); }
logger.info("main: Done."); } }
--------------------------------------------------------------------- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED]
--------------------------------------------------------------------- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED]
