cayenne-user mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From Marek Wawrzyczny <marek_wawrzyc...@internode.on.net>
Subject Re: UpdateQuery not updating
Date Thu, 06 Apr 2006 04:47:12 GMT
Hi Alan,

I'd use the Entity classes and treat the records as objects:


Date dob = null;
DateFormat df = DateFormat.getDateInstance();
DataContext ctxt =
		BasicServletConfiguration.getDefaultContext(request.getSession());

String customerName = request.getParameter("customerName");
String dateOfBirth = request.getParameter("dateOfBirth");

Expression exp = ExpressionFactory.matchExp(
		Customer.CUSTOMER_NAME_PROPERTY, customerName );
SelectQuery query = new SelectQuery(Customer.class, exp);

List customers = null;
try {
	customers = ContextManager.getSharedContext().performQuery(query);
} catch (Exception e) {
	// Bad stuff happened, deal with it
	e.printStackTrace();
}

// Fetch the record as an Entity class... you can then implement fancy 
// stuff in the Entity class, like conversions...

// Anyway, we expect only one customer, right?
Customer customer = (customers != null) && (customers.count() == 1) ?
		(Customer)customers.get(0) : null;

if (customer != null) {
	try { 
		dob = df.parse(dateOfBirth);  
		// Of course you could override the setDateOfBirth(...) function to take an
		// Object and do the parsing itself...       
		customer.setDateOfBirth(dob);
		ctxt.commitChanges();
	} catch(ParseException e) {
		System.out.println("Unable to parse ");
	}
} else {
	// Perhaps notify someone... or something...
}


Hope this helps.

Marek Wawrzyczny


On Thursday 06 April 2006 08:56, Alan Baltazar wrote:
> How would you implement your way for updating tables?
>
> --- "Gentry, Michael (Contractor)" <michael_gentry@fanniemae.com> wrote:
> > I've never used UpdateQuery before, but try:
> >
> > UpdateQuery updated =
> >   new UpdateQuery(Customer.class,
> >                   template.expWithParameters(params));
> >
> > Also, I don't think you need the commitChanges() at the end ...  That
> > would be more useful if you were working with actual CayenneDataObjects,
> > which you aren't.  Also, I think your object graph might get out-of-sync
> > this way, which could lead to more problems.
> >
> > /dev/mrg
> >
> >
> > -----Original Message-----
> > From: Alan Baltazar [mailto:acatalanb@yahoo.com]
> > Sent: Wednesday, April 05, 2006 5:13 PM
> > To: cayenne-user@incubator.apache.org
> > Subject: UpdateQuery not updating
> >
> >
> > Hello,
> >
> >   I'm trying to update a field in a table using the UpdateQuery. The
> > following code
> > doesn't update my data.
> >
> > Date dob = null;
> > DateFormat df = DateFormat.getDateInstance();
> > DataContext ctxt =
> > BasicServletConfiguration.getDefaultContext(request.getSession());
> >
> > String customerName = request.getParameter("customerName");
> > String dateOfBirth = request.getParameter("dateOfBirth");
> >
> > Expression template = Expression.fromString("customerName =
> > $customerName");
> > Map params = new HashMap();
> > params.put("customerName", customerName);
> >
> > UpdateQuery updated = new UpdateQuery(Customer.class,template);
> >
> > try {
> >    dob = df.parse(dateOfBirth);
> > }
> > catch(ParseException e) {
> >    System.out.println("Unable to parse ");
> > }
> >
> > updated.addUpdAttribute("DATE_OF_BIRTH",dob);
> > ctxt.performNonSelectingQuery(updated);
> > ctxt.commitChanges();
> >
> > return mapping.findForward("success");
> > }
> >
> > this is the mysql table,
> > CREATE TABLE `CUSTOMER` (
> >   `CUSTOMER_ID` int(10) unsigned NOT NULL ,
> >   `CUSTOMER_NAME` varchar(20) NOT NULL default '',
> >   `DATE_OF_BIRTH` date NOT NULL default '0000-00-00',
> >   PRIMARY KEY  (`CUSTOMER_ID`)
> > ) TYPE=InnoDB;
> >
> > here's the data object,
> > public class _Customer extends org.objectstyle.cayenne.CayenneDataObject
> > {
> >
> >     public static final String CUSTOMER_NAME_PROPERTY = "customerName";
> >     public static final String DATE_OF_BIRTH_PROPERTY = "dateOfBirth";
> >     public static final String PRODUCT_ARRAY_PROPERTY = "productArray";
> >
> >     public static final String CUSTOMER_ID_PK_COLUMN = "CUSTOMER_ID";
> >
> >     public void setCustomerName(String customerName) {
> >         writeProperty("customerName", customerName);
> >     }
> >     public String getCustomerName() {
> >         return (String)readProperty("customerName");
> >     }
> >
> >
> >     public void setDateOfBirth(java.util.Date dateOfBirth) {
> >         writeProperty("dateOfBirth", dateOfBirth);
> >     }
> >     public java.util.Date getDateOfBirth() {
> >         return (java.util.Date)readProperty("dateOfBirth");
> >     }
> >
> >
> >     public void addToProductArray(dao.Product obj) {
> >         addToManyTarget("productArray", obj, true);
> >     }
> >     public void removeFromProductArray(dao.Product obj) {
> >         removeToManyTarget("productArray", obj, true);
> >     }
> >     public List getProductArray() {
> >         return (List)readProperty("productArray");
> >     }
> >
> >
> > }
> >
> >
> >   if there's any other shorter way to do this with UpdateQuery, I'm
> > listening
> >
> > thanks for any help,
> > alan
> >
> > __________________________________________________
> > Do You Yahoo!?
> > Tired of spam?  Yahoo! Mail has the best spam protection around
> > http://mail.yahoo.com
>
> __________________________________________________
> Do You Yahoo!?
> Tired of spam?  Yahoo! Mail has the best spam protection around
> http://mail.yahoo.com


Mime
View raw message