db-torque-user mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From Robert Kromkamp <r.kromk...@pagelink.nl>
Subject RE: Problem with Criteria After Deserializing
Date Tue, 29 Mar 2005 07:20:56 GMT
I had the same problem. To solve it I've written my own serialize and
deserialize functions for Criteria.java for torque 3.1.1:

/** Serialize this Object in a manner which is binary-compatible with the
JDK */
    private void writeObject(ObjectOutputStream s) throws IOException {
			ObjectOutputStream.PutField oFields;
			Iterator it = entrySet().iterator();
			Map.Entry oEntry;
			oFields = s.putFields();
			
			oFields.put("offset", this.offset);
			oFields.put("limit", this.limit);
			oFields.put("ignoreCase", ignoreCase);
			oFields.put("singleRecord", isSingleRecord());
			oFields.put("cascade", isCascade());
			oFields.put("dbName", getDbName());
			
			oFields.put("selectModifiers",
getSelectModifiers());
			oFields.put("selectColumns", getSelectColumns());
			oFields.put("orderByColumns", getOrderByColumns());
			
			oFields.put("groupByColumns", getGroupByColumns());
			oFields.put("having", getHaving());
    			oFields.put("asColumns", getAsColumns());
    			oFields.put("joinL", getJoinL());
    			oFields.put("joinR", getJoinR());
			oFields.put("aliases", this.aliases);

			oFields.put("useTransaction", this.useTransaction);
			oFields.put("originalDbName", this.originalDbName);
			
			s.writeFields();
		
			//s.writeInt(capacity);
			s.writeInt(super.size());
			while (it.hasNext())
			    {
				oEntry = (Map.Entry) it.next();
				String key = (String)oEntry.getKey();
				s.writeObject(key);
				s.writeObject(oEntry.getValue());
	    
        			if (this.containsKey(key)){
            			Criterion aCriterion =
this.getCriterion(key);
            			s.writeObject(aCriterion);
        			} else {
            			s.writeObject(new Boolean(false));
        			}
	    		}
    }



    /** Deserialize this Object in a manner which is binary-compatible with
the JDK */
    
    private void readObject(ObjectInputStream s) throws IOException,
ClassNotFoundException
    {
      int i;
      int iLen;
      Object oKey, oValue, oCriterion;
      ObjectInputStream.GetField oFields;
      oFields = s.readFields();

			this.offset = oFields.get("offset", 0);
			this.limit = oFields.get("limit", -1);
			ignoreCase = oFields.get("ignoreCase", false);
			singleRecord = oFields.get("singleRecord", false);
			cascade = oFields.get("cascade", false);
			dbName = (String) oFields.get("dbName", "");
			selectModifiers = (UniqueList)
oFields.get("selectModifiers", new UniqueList());
			selectColumns = (UniqueList)
oFields.get("selectColumns", new UniqueList());
			orderByColumns = (UniqueList)
oFields.get("orderByColumns", new UniqueList());
			
			groupByColumns =
(UniqueList)oFields.get("groupByColumns", new UniqueList());
			
			having = (Criteria.Criterion)oFields.get("having",
null);
    			asColumns = (Hashtable)oFields.get("asColumns",
null);
    			joinL = (ArrayList)oFields.get("joinL", null);
    			joinR = (ArrayList)oFields.get("joinR", null);
			aliases = (HashMap)oFields.get("aliases", null);

			useTransaction = oFields.get("useTransaction",
false);
			originalDbName =
(String)oFields.get("originalDbName", "");
			
      		iLen = s.readInt();
      
      		for (i = 0; i < iLen; i++){
			  oKey = s.readObject();
			  oValue = s.readObject();
			  super.put(oKey, oValue);
			  
			  oCriterion = s.readObject();
			  if (oCriterion instanceof
org.apache.torque.util.Criteria.Criterion){
			  	Criterion criterion = (Criterion)
oCriterion;
			  	add(criterion);
			  }
			}
    }


-----Original Message-----
From: Joe Morrogh [mailto:jmorrogh@hotmail.com]
Sent: maandag 28 maart 2005 3:54
To: torque-user@db.apache.org
Subject: Problem with Criteria After Deserializing


Hi,

I've got a Torque 3.0 application (part of a Turbine 2.2 webapp) and I want 
to add persistent query
funtionality to it.  Since the Torque Criteria class already represents a 
set of query criteria, and is serializable, it seems logical to persist 
instances of this class for my purposes.  The problem I'm
having is that the deserialized Criteria object doesn't behave exactly the 
same as the object that
existed prior to serialization.  Specifically, my Criterion objects in the 
hash seem to contain an
addtional layer of Criterion objects after deserialization.  For example, if

my search criteria is    "MY_TABLE.MYCOLUMN = 'AAA'", before serialization, 
I've got one Criterion in the hash whose value is "AAA".  After 
serialization, I've still got one Criterion in the hash, but the value is 
not the String
"AAA", but another Criterion object, whose value is then "AAA".  The end 
result of all this is that when the query is generated, I end up with an 
invalid query like:

    select * from MY_TABLE where MYCOLUMN=MYCOLUMN='AAA'

I'm rying to avoid coding my own classes to represent the "where clause".  
Any ideas?

TIA,
Joe

Below is a simple program that demonstrates the problem.


import java.io.*;
import org.apache.torque.util.Criteria;

public class SerializeCriteriaTest {

public static void main(String args[]) throws Exception {

	System.out.println("Entering SerializeCriteriaTest::main");
	Criteria criteria = new Criteria();
	criteria.add("MY_TABLE.MYCOLUMN", (Object) "AAA",
	Criteria.EQUAL);
	System.out.println(criteria.toString());

	ByteArrayOutputStream bas = new ByteArrayOutputStream();
	ObjectOutputStream oos = new ObjectOutputStream(bas);
	oos.writeObject(criteria);
	byte [] bytes = bas.toByteArray();
	System.out.println("the length of bytes is " + 	bytes.length);

	Criteria criteriaReborn =
	   (Criteria) new ObjectInputStream(new 
ByteArrayInputStream(bytes)).readObject();
	System.out.println(criteriaReborn.toString());

}} // end class


Here's the stdout

Entering SerializeCriteriaTest::main
Criteria:: MY_TABLE.MYCOLUMN<=>MY_TABLE.MYCOLUMN='AAA':
Current Query SQL (may not be complete or applicable):
the length of bytes is 1544
Criteria:: MY_TABLE.MYCOLUMN<=>MY_TABLE.MYCOLUMN=MY_TABLE.MYCOLUMN='AAA':
Current Query SQL (may not be complete or applicable):



---------------------------------------------------------------------
To unsubscribe, e-mail: torque-user-unsubscribe@db.apache.org
For additional commands, e-mail: torque-user-help@db.apache.org

---------------------------------------------------------------------
To unsubscribe, e-mail: torque-user-unsubscribe@db.apache.org
For additional commands, e-mail: torque-user-help@db.apache.org


Mime
View raw message