Hi,
Stas Ostapenko wrote:
I have a problem with inserting all table data of one table to another table in other RDBMS. Following code works, but it stores not all the data, but only first object. Where I`m wrong ?
First you have to take care that metadata changes are global operations, all metadata are shared.
http://db.apache.org/ojb/docu/guides/metadata.html#Global%0A++++++++++++++++++++
If you change the table name of a class-descriptor all other PB/threads will use the changed table name.
In a single threaded application you can change metadata in the following way:
- Lookup the PB of the source DB
- get the objects you want to copy, use PB.getCollectionByQuery instead of PB.getIteratorByQuery (to guarantee that the objects be materialized before the metadata change, avoid lazy materialization settings in metadata)
- close the PB
- change the table name
- Lookup PB for target DB
- begin PB-tx
- loop and copy
- PB-tx commit
A more elegant and multithreaded safe solution will be the usage of different metadata profiles. You have a "normal" repository file with all class-descriptor (with source table names) and an additionally "profile" repository file with all class-descriptor using the target DB table names. This profile is read at runtime (e.g. at start of your application) and you are using the MetadataManager to setup a "source" and a "target" metadata-profile. More detailed info here
http://db.apache.org/ojb/docu/guides/metadata.html#Per+thread+metadata+changes
regards, Armin
Please help.
import org.apache.ojb.broker.PersistenceBroker; import org.apache.ojb.broker.PersistenceBrokerException; import org.apache.ojb.broker.PersistenceBrokerFactory; import org.apache.ojb.broker.query.QueryByCriteria; import org.apache.ojb.broker.query.Criteria; import org.apache.ojb.broker.metadata.DescriptorRepository; import org.apache.ojb.broker.metadata.FieldDescriptor; import org.apache.ojb.broker.metadata.MetadataManager; import org.apache.ojb.broker.metadata.ClassDescriptor;
import java.util.Iterator;
public class From_MySQL_2_HSQL_DB_testing { public static void main(String args[]) { PersistenceBroker hsql_db_broker = null;
PersistenceBroker mysql_db_broker = null;
Object result = null;
ClassDescriptor hsql_db_Domain_classDescriptor = null; ClassDescriptor mysql_db_Domain_classDescriptor = null; try { hsql_db_broker = PersistenceBrokerFactory.createPersistenceBroker("HSQL_OJB_Files", "root", "");
mysql_db_broker = PersistenceBrokerFactory.createPersistenceBroker("MySQL_Main_Database", "root", "");
hsql_db_Domain_classDescriptor = hsql_db_broker.getClassDescriptor(Domain.class); hsql_db_Domain_classDescriptor.setTableName("hsqldb_domain");
mysql_db_Domain_classDescriptor = mysql_db_broker.getClassDescriptor(Domain.class); mysql_db_Domain_classDescriptor.setTableName("domain");
Criteria criteria = new Criteria();
criteria.addLike("Domain", "%"); QueryByCriteria query = new QueryByCriteria(Domain.class, criteria);
Iterator bdIterator = mysql_db_broker.getIteratorByQuery(query);
while(bdIterator.hasNext()) { mysql_db_Domain_classDescriptor.setTableName("domain"); result = bdIterator.next();
hsql_db_Domain_classDescriptor.setTableName("hsqldb_domain"); hsql_db_broker.beginTransaction(); hsql_db_broker.store(result); hsql_db_broker.commitTransaction(); System.out.println(((Domain)result).Name); } } catch(Exception e) { e.printStackTrace(); } finally { //close HSQLDB connection hsql_db_broker.close();
//close MySQL connection mysql_db_broker.close(); } } }
--------------------------------------------------------------------- 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]