about castor
ipozeng Mar 18, 2003 8:47 PMsir,
i have tried using castor instead of entity bean and i feel castor is a great product.
however there are a few problems i can not fix as no document at hand :-(
Now i use castor in session bean as below,please give me suggestion about how to use
castor in such a situation
HelloBean.java
package test.stateless.hello;
import java.io.*;
import java.rmi.*;
import java.util.List;
import java.util.Iterator;
import javax.ejb.*;
import org.exolab.castor.jdo.JDO;
import org.exolab.castor.jdo.Database;
import org.exolab.castor.jdo.OQLQuery;
import org.exolab.castor.jdo.QueryResults;
import org.exolab.castor.jdo.TransactionNotInProgressException;
import org.exolab.castor.util.Logger;
import org.exolab.castor.xml.Marshaller;
import org.exolab.castor.xml.Unmarshaller;
import org.exolab.castor.mapping.Mapping;
import myapp.*;
public class HelloBean extends com.caucho.ejb.AbstractSessionBean
{
private JDO jdo;
private Database db;
private PrintWriter log = new Logger( System.out ).setPrefix( "test" );
public void ejbCreate()
{
try {
jdo = new JDO();
jdo.setLogWriter( log );
jdo.setTransactionManager("java:/comp/UserTransaction");
jdo.setClassLoader( getClass().getClassLoader() );
jdo.setConfiguration("database.xml");
jdo.setDatabaseName( "test" );
db = jdo.getDatabase();
}
catch(Exception e) {
}
}
public String getProducts()
{
Writer output = new StringWriter ();
try {
OQLQuery oql;
QueryResults results;
Marshaller marshaller;
Mapping _mapping;
_mapping = new Mapping( getClass().getClassLoader() );
_mapping.setLogWriter( log );
_mapping.loadMapping( "mapping.xml" );
marshaller = new Marshaller(output);
marshaller.setMapping( _mapping );
marshaller.setMarshalAsDocument(false);
db.begin();
oql = db.getOQLQuery( "SELECT p FROM myapp.Product p" );
results = oql.execute();
output.write("");
while( results.hasMore() ) {
myapp.Product prod = null;
prod = (myapp.Product)results.next();
marshaller.marshal( prod );
}
output.write("");
db.commit();
db.close();
}
catch ( Exception e ) {
try {
db.rollback();
}
catch(TransactionNotInProgressException te) {
}
return "failed:" + e.getMessage();
}
return output.toString();
}
public String applyUpdates(String delta)
{
StringBuffer tmp = new StringBuffer();
try {
Mapping _mapping;
Unmarshaller unmarshaller;
OQLQuery oql;
_mapping = new Mapping( getClass().getClassLoader() );
_mapping.setLogWriter( log );
_mapping.loadMapping( "delta-mapping.xml" );
unmarshaller = new Unmarshaller(ROWDATA.class) ;
unmarshaller.setMapping( _mapping );
Reader reader = new StringReader(delta);
ROWDATA rowdata = (ROWDATA)unmarshaller.unmarshal(reader);
db.begin();
oql = db.getOQLQuery( "SELECT p FROM myapp.Product p where id=$1" );
List rows = rowdata.getRows();
Iterator iter = rows.iterator();
while ( iter.hasNext() ) {
ROW row = (ROW) iter.next();
if(row.getId() == 9) {
oql.bind( 9 );
QueryResults results = oql.execute();
if( results.hasMore() ) {
Product prod = (Product)results.next();
prod.setName("OK");
}
}
}
db.commit();
db.close();
}
catch ( Exception e ) {
try {
db.rollback();
}
catch(TransactionNotInProgressException te) {
}
return "failed:" + e.getMessage() + "\n" + tmp.toString();
}
return tmp.toString();
}
}
getProducts() can return the data correctly but applyUpdates() doesnot work well.
when executing db.commit() it failed with the message:
"
Nested error: org.exolab.castor.jdo.PersistenceException: Nested error: java.sql.SQLException: ERROR: Unable to identify an operator '=' for types 'numeric' and 'double precision' You will have to retype this query using an explicit cast
"
i know it is SQL problem but i can not control castor to create right SQL :(
(i used PostgreSQL 7.2 on linux)
Table "prod"
Column | Type | Modifiers
----------+------------------------+-----------
id | integer | not null
name | character varying(200) | not null
price | numeric(18,2) | not null
group_id | integer | not null
Any suggestion is appreciated !