You are viewing a plain text version of this content. The canonical link for it is here.
Posted to ojb-user@db.apache.org by Abid Hussain <ab...@abid76.de> on 2008/05/15 16:14:41 UTC

question about storing objects

Hi everybody,

I have written a storeAll()-method in a dao to store all given objects 
in one transaction (you find the code at the end of this mail).

Now please have a look at the following lines of code:
Account_2DAO dao1 = new Account_2DAO("DB1");
Collection<Account_2> accounts = dao1.findAll(Account_2.class);
logger.debug(accounts);
Account_2DAO dao2 = new Account_2DAO("DB2");
dao2.storeAll(accounts);

As you see, that there are two daos initialized. The first (dao1) 
retrieves all objects (using findAll()) from a database (DB1). The 
second (dao2) stores all retrieved objects into another database (DB2).
The findAll()-method works fine.

But the storeAll()-method only works when the line of code
logger.debug(accounts);
is called. If not, no data is inserted.

Any ideas what's going wrong (you find the class-descriptor file below)?

Regards,

Abid


Code of storeAll():
public void storeAll(Collection<T> valueObjects)
		throws PersistentStoringException {

	if (valueObjects != null) {
		try {
			broker.beginTransaction();
			for (T valueObject : valueObjects) {
				broker.store(valueObject, ObjectModification.INSERT);
			}
			broker.commitTransaction();
		} catch (Exception e) {
			broker.abortTransaction();
			throw new PersistentStoringException(
					"Error while storing objects.\n" + e);
		} finally {
			if (broker != null && !broker.isClosed())
				broker.close();
		}
	}
}

Class-descriptor of Account_2:
<class-descriptor class="modulverwaltung.beans.Account_2"
	table="ACCOUNT_2" proxy="dynamic">
	<field-descriptor name="id" column="ID" primarykey="true"
		autoincrement="true" sequence-name="Account_Id_Seq" />
	<field-descriptor name="username" column="Username"
		jdbc-type="VARCHAR" />
	<field-descriptor name="role" column="Rolle" jdbc-type="VARCHAR" />
</class-descriptor>

-- 

Abid Hussain
Mail: abid.hussain@abid76.de
Web: http://www.abid76.de

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


Re: question about storing objects

Posted by Armin Waibel <ar...@apache.org>.
Abid Hussain wrote:
> Hi Armin,
> 
> thanks for help.
> 
> Just to be sure that I got the point: When there is a 'proxy="dynamic"' 
> in the class-descriptor, proxies are always used when more than one 
> object is queried?

The returned persistence capable object is always a proxy object, even 
if the query result returns a single object.
In my opinion using a proxy on class-descriptor level (persistence 
capable object proxy) doesn't give much advantage, because OJB always 
perform the full query and only materialize the object identity from the 
result set.

> 
> I thought that proxies are only used when retrieving a collection 
> through a relation (1:n or m:n, e.g. ShoppingCart.getArticles() - see 
> below)...
>

If you declare proxy="true" on collection-/reference-descriptor level 
(1:1, 1:n or m:n relation) then yes you are right.

regards,
Armin


> Regards,
> 
> Abid
> 
> Armin Waibel schrieb:
>> Hi Abid,
>>
>> Abid Hussain wrote:
>>> Hi again,
>>>
>>> I think this issue has to something with the materialization of 
>>> objects (when using proxies). But I thought that proxies are only 
>>> used, when retrieving a corresponding collection of an object (in 1:n 
>>> and m:n relations, e.g. ShoppingCart.getArticles()...).
>>>
>>> What I'm doing when using findAll() (see below) is something like 
>>> SELECT * FROM <tablename>. I thought, in this case no proxies are 
>>> used???
>>>
>>
>> You are using dynamic proxies for all Account_2 objects:
>>
>> <class-descriptor class="modulverwaltung.beans.Account_2"
>>     table="ACCOUNT_2" proxy="dynamic">
>>
>> The query result collection isn't a proxy object. Instead each result 
>> object is a proxy object of persistence capable object (Account_2 
>> object). In most cases this doesn't have an performance advantage.
>>
>> If you now store each of these objects without materialization (e.g. 
>> your log.debug() force the materialization of the object) OJB thinks 
>> that the object didn't change and skip the update.
>>
>> regards,
>> Armin
>>
>>
>>> Regards,
>>>
>>> Abid
>>>
>>>
>>> Abid Hussain schrieb:
>>>> Hi everybody,
>>>>
>>>> I have written a storeAll()-method in a dao to store all given 
>>>> objects in one transaction (you find the code at the end of this mail).
>>>>
>>>> Now please have a look at the following lines of code:
>>>> Account_2DAO dao1 = new Account_2DAO("DB1");
>>>> Collection<Account_2> accounts = dao1.findAll(Account_2.class);
>>>> logger.debug(accounts);
>>>> Account_2DAO dao2 = new Account_2DAO("DB2");
>>>> dao2.storeAll(accounts);
>>>>
>>>> As you see, that there are two daos initialized. The first (dao1) 
>>>> retrieves all objects (using findAll()) from a database (DB1). The 
>>>> second (dao2) stores all retrieved objects into another database (DB2).
>>>> The findAll()-method works fine.
>>>>
>>>> But the storeAll()-method only works when the line of code
>>>> logger.debug(accounts);
>>>> is called. If not, no data is inserted.
>>>>
>>>> Any ideas what's going wrong (you find the class-descriptor file 
>>>> below)?
>>>>
>>>> Regards,
>>>>
>>>> Abid
>>>>
>>>>
>>>> Code of storeAll():
>>>> public void storeAll(Collection<T> valueObjects)
>>>>         throws PersistentStoringException {
>>>>
>>>>     if (valueObjects != null) {
>>>>         try {
>>>>             broker.beginTransaction();
>>>>             for (T valueObject : valueObjects) {
>>>>                 broker.store(valueObject, ObjectModification.INSERT);
>>>>             }
>>>>             broker.commitTransaction();
>>>>         } catch (Exception e) {
>>>>             broker.abortTransaction();
>>>>             throw new PersistentStoringException(
>>>>                     "Error while storing objects.\n" + e);
>>>>         } finally {
>>>>             if (broker != null && !broker.isClosed())
>>>>                 broker.close();
>>>>         }
>>>>     }
>>>> }
>>>>
>>>> Class-descriptor of Account_2:
>>>> <class-descriptor class="modulverwaltung.beans.Account_2"
>>>>     table="ACCOUNT_2" proxy="dynamic">
>>>>     <field-descriptor name="id" column="ID" primarykey="true"
>>>>         autoincrement="true" sequence-name="Account_Id_Seq" />
>>>>     <field-descriptor name="username" column="Username"
>>>>         jdbc-type="VARCHAR" />
>>>>     <field-descriptor name="role" column="Rolle" jdbc-type="VARCHAR" />
>>>> </class-descriptor>
>>>>
>>>
>>
>> ---------------------------------------------------------------------
>> To unsubscribe, e-mail: ojb-user-unsubscribe@db.apache.org
>> For additional commands, e-mail: ojb-user-help@db.apache.org
>>
>>
> 

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


Re: question about storing objects

Posted by Abid Hussain <ab...@abid76.de>.
Hi Armin,

thanks for help.

Just to be sure that I got the point: When there is a 'proxy="dynamic"' 
in the class-descriptor, proxies are always used when more than one 
object is queried?

I thought that proxies are only used when retrieving a collection 
through a relation (1:n or m:n, e.g. ShoppingCart.getArticles() - see 
below)...

Regards,

Abid

Armin Waibel schrieb:
> Hi Abid,
> 
> Abid Hussain wrote:
>> Hi again,
>>
>> I think this issue has to something with the materialization of 
>> objects (when using proxies). But I thought that proxies are only 
>> used, when retrieving a corresponding collection of an object (in 1:n 
>> and m:n relations, e.g. ShoppingCart.getArticles()...).
>>
>> What I'm doing when using findAll() (see below) is something like 
>> SELECT * FROM <tablename>. I thought, in this case no proxies are used???
>>
> 
> You are using dynamic proxies for all Account_2 objects:
> 
> <class-descriptor class="modulverwaltung.beans.Account_2"
>     table="ACCOUNT_2" proxy="dynamic">
> 
> The query result collection isn't a proxy object. Instead each result 
> object is a proxy object of persistence capable object (Account_2 
> object). In most cases this doesn't have an performance advantage.
> 
> If you now store each of these objects without materialization (e.g. 
> your log.debug() force the materialization of the object) OJB thinks 
> that the object didn't change and skip the update.
> 
> regards,
> Armin
> 
> 
>> Regards,
>>
>> Abid
>>
>>
>> Abid Hussain schrieb:
>>> Hi everybody,
>>>
>>> I have written a storeAll()-method in a dao to store all given 
>>> objects in one transaction (you find the code at the end of this mail).
>>>
>>> Now please have a look at the following lines of code:
>>> Account_2DAO dao1 = new Account_2DAO("DB1");
>>> Collection<Account_2> accounts = dao1.findAll(Account_2.class);
>>> logger.debug(accounts);
>>> Account_2DAO dao2 = new Account_2DAO("DB2");
>>> dao2.storeAll(accounts);
>>>
>>> As you see, that there are two daos initialized. The first (dao1) 
>>> retrieves all objects (using findAll()) from a database (DB1). The 
>>> second (dao2) stores all retrieved objects into another database (DB2).
>>> The findAll()-method works fine.
>>>
>>> But the storeAll()-method only works when the line of code
>>> logger.debug(accounts);
>>> is called. If not, no data is inserted.
>>>
>>> Any ideas what's going wrong (you find the class-descriptor file below)?
>>>
>>> Regards,
>>>
>>> Abid
>>>
>>>
>>> Code of storeAll():
>>> public void storeAll(Collection<T> valueObjects)
>>>         throws PersistentStoringException {
>>>
>>>     if (valueObjects != null) {
>>>         try {
>>>             broker.beginTransaction();
>>>             for (T valueObject : valueObjects) {
>>>                 broker.store(valueObject, ObjectModification.INSERT);
>>>             }
>>>             broker.commitTransaction();
>>>         } catch (Exception e) {
>>>             broker.abortTransaction();
>>>             throw new PersistentStoringException(
>>>                     "Error while storing objects.\n" + e);
>>>         } finally {
>>>             if (broker != null && !broker.isClosed())
>>>                 broker.close();
>>>         }
>>>     }
>>> }
>>>
>>> Class-descriptor of Account_2:
>>> <class-descriptor class="modulverwaltung.beans.Account_2"
>>>     table="ACCOUNT_2" proxy="dynamic">
>>>     <field-descriptor name="id" column="ID" primarykey="true"
>>>         autoincrement="true" sequence-name="Account_Id_Seq" />
>>>     <field-descriptor name="username" column="Username"
>>>         jdbc-type="VARCHAR" />
>>>     <field-descriptor name="role" column="Rolle" jdbc-type="VARCHAR" />
>>> </class-descriptor>
>>>
>>
> 
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: ojb-user-unsubscribe@db.apache.org
> For additional commands, e-mail: ojb-user-help@db.apache.org
> 
> 

-- 

Abid Hussain
Mail: abid.hussain@abid76.de
Web: http://www.abid76.de

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


Re: question about storing objects

Posted by Armin Waibel <ar...@apache.org>.
Hi Abid,

Abid Hussain wrote:
> Hi again,
> 
> I think this issue has to something with the materialization of objects 
> (when using proxies). But I thought that proxies are only used, when 
> retrieving a corresponding collection of an object (in 1:n and m:n 
> relations, e.g. ShoppingCart.getArticles()...).
> 
> What I'm doing when using findAll() (see below) is something like SELECT 
> * FROM <tablename>. I thought, in this case no proxies are used???
>

You are using dynamic proxies for all Account_2 objects:

<class-descriptor class="modulverwaltung.beans.Account_2"
     table="ACCOUNT_2" proxy="dynamic">

The query result collection isn't a proxy object. Instead each result 
object is a proxy object of persistence capable object (Account_2 
object). In most cases this doesn't have an performance advantage.

If you now store each of these objects without materialization (e.g. 
your log.debug() force the materialization of the object) OJB thinks 
that the object didn't change and skip the update.

regards,
Armin


> Regards,
> 
> Abid
> 
> 
> Abid Hussain schrieb:
>> Hi everybody,
>>
>> I have written a storeAll()-method in a dao to store all given objects 
>> in one transaction (you find the code at the end of this mail).
>>
>> Now please have a look at the following lines of code:
>> Account_2DAO dao1 = new Account_2DAO("DB1");
>> Collection<Account_2> accounts = dao1.findAll(Account_2.class);
>> logger.debug(accounts);
>> Account_2DAO dao2 = new Account_2DAO("DB2");
>> dao2.storeAll(accounts);
>>
>> As you see, that there are two daos initialized. The first (dao1) 
>> retrieves all objects (using findAll()) from a database (DB1). The 
>> second (dao2) stores all retrieved objects into another database (DB2).
>> The findAll()-method works fine.
>>
>> But the storeAll()-method only works when the line of code
>> logger.debug(accounts);
>> is called. If not, no data is inserted.
>>
>> Any ideas what's going wrong (you find the class-descriptor file below)?
>>
>> Regards,
>>
>> Abid
>>
>>
>> Code of storeAll():
>> public void storeAll(Collection<T> valueObjects)
>>         throws PersistentStoringException {
>>
>>     if (valueObjects != null) {
>>         try {
>>             broker.beginTransaction();
>>             for (T valueObject : valueObjects) {
>>                 broker.store(valueObject, ObjectModification.INSERT);
>>             }
>>             broker.commitTransaction();
>>         } catch (Exception e) {
>>             broker.abortTransaction();
>>             throw new PersistentStoringException(
>>                     "Error while storing objects.\n" + e);
>>         } finally {
>>             if (broker != null && !broker.isClosed())
>>                 broker.close();
>>         }
>>     }
>> }
>>
>> Class-descriptor of Account_2:
>> <class-descriptor class="modulverwaltung.beans.Account_2"
>>     table="ACCOUNT_2" proxy="dynamic">
>>     <field-descriptor name="id" column="ID" primarykey="true"
>>         autoincrement="true" sequence-name="Account_Id_Seq" />
>>     <field-descriptor name="username" column="Username"
>>         jdbc-type="VARCHAR" />
>>     <field-descriptor name="role" column="Rolle" jdbc-type="VARCHAR" />
>> </class-descriptor>
>>
> 

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


Re: question about storing objects

Posted by Abid Hussain <ab...@abid76.de>.
Hi again,

I think this issue has to something with the materialization of objects 
(when using proxies). But I thought that proxies are only used, when 
retrieving a corresponding collection of an object (in 1:n and m:n 
relations, e.g. ShoppingCart.getArticles()...).

What I'm doing when using findAll() (see below) is something like SELECT 
* FROM <tablename>. I thought, in this case no proxies are used???

Regards,

Abid


Abid Hussain schrieb:
> Hi everybody,
> 
> I have written a storeAll()-method in a dao to store all given objects 
> in one transaction (you find the code at the end of this mail).
> 
> Now please have a look at the following lines of code:
> Account_2DAO dao1 = new Account_2DAO("DB1");
> Collection<Account_2> accounts = dao1.findAll(Account_2.class);
> logger.debug(accounts);
> Account_2DAO dao2 = new Account_2DAO("DB2");
> dao2.storeAll(accounts);
> 
> As you see, that there are two daos initialized. The first (dao1) 
> retrieves all objects (using findAll()) from a database (DB1). The 
> second (dao2) stores all retrieved objects into another database (DB2).
> The findAll()-method works fine.
> 
> But the storeAll()-method only works when the line of code
> logger.debug(accounts);
> is called. If not, no data is inserted.
> 
> Any ideas what's going wrong (you find the class-descriptor file below)?
> 
> Regards,
> 
> Abid
> 
> 
> Code of storeAll():
> public void storeAll(Collection<T> valueObjects)
>         throws PersistentStoringException {
> 
>     if (valueObjects != null) {
>         try {
>             broker.beginTransaction();
>             for (T valueObject : valueObjects) {
>                 broker.store(valueObject, ObjectModification.INSERT);
>             }
>             broker.commitTransaction();
>         } catch (Exception e) {
>             broker.abortTransaction();
>             throw new PersistentStoringException(
>                     "Error while storing objects.\n" + e);
>         } finally {
>             if (broker != null && !broker.isClosed())
>                 broker.close();
>         }
>     }
> }
> 
> Class-descriptor of Account_2:
> <class-descriptor class="modulverwaltung.beans.Account_2"
>     table="ACCOUNT_2" proxy="dynamic">
>     <field-descriptor name="id" column="ID" primarykey="true"
>         autoincrement="true" sequence-name="Account_Id_Seq" />
>     <field-descriptor name="username" column="Username"
>         jdbc-type="VARCHAR" />
>     <field-descriptor name="role" column="Rolle" jdbc-type="VARCHAR" />
> </class-descriptor>
> 

-- 

Abid Hussain
Mail: abid.hussain@abid76.de
Web: http://www.abid76.de

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