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 Charles Anthony <ch...@hpdsoftware.com> on 2004/07/26 11:04:51 UTC
RE: how to postinitialize objects after being completely material
ized
Hi David,
Another approach : if you get your persistent objects to implement
org.apache.ojb.broker.PersistenceBrokerAware interface, you could then
implement the afterLookup method e.g.
public void afterLookup(PersistenceBroker broker) {
_postinit();
};
The afterLookup method is invoked after a persistence broker loads the
object - and, I believe, after any of the references are loaded too.
Some people would not like this approach (due to the persistency layer
"invading" your domain objects) - I am not one of them.
HTH,
Chaeers,
> -----Original Message-----
> From: David Zejda [mailto:dvid@atlas.cz]
> Sent: 26 July 2004 09:59
> To: OJB Users List
> Subject: how to postinitialize objects after being completely
> materialized
>
>
> I have a custom row reader able to initialize materialized objects by
> their _postinit() methods. Essentially it works, but there is
> a problem
> - the _postinit() should be called after the object is materialized
> completely, i.e. with all fields, but also references and collections
> filled. So, I guess I have to override some other method of
> rowreader or
> something completely different - please, give a hint.
>
> public class RowReaderPostinitializingImpl extends
> RowReaderDefaultImpl
> {
> public RowReaderPostinitializingImpl(ClassDescriptor c)
> {
> super(c);
> }
>
> public Object readObjectFrom(Map row)
> {
> Object result = super.readObjectFrom(row);
> if (result instanceof Postinitializable)
> {
> Postinitializable art = (Postinitializable) result;
> art._postinit();
> return art;
> }
> else
> {
> return result;
> }
> }
> }
>
> David
>
> ---------------------------------------------------------------------
> 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
>
>
___________________________________________________________
HPD Software Ltd. - Helping Business Finance Business
Email terms and conditions: www.hpdsoftware.com/disclaimer
---------------------------------------------------------------------
To unsubscribe, e-mail: ojb-user-unsubscribe@db.apache.org
For additional commands, e-mail: ojb-user-help@db.apache.org
Re: how to postinitialize...even with getCollectionByQuery
Posted by UHK <da...@uhk.cz>.
> The solution was to replace
> org.apache.ojb.broker.cache.ObjectCacheDefaultImpl
> with
> org.apache.ojb.broker.cache.ObjectCacheEmptyImpl, so now it goes OK:
Ugh.
The seeming of connection between the objectcache and the order of
initialization steps was only a result of a bug in my testcase, so the
problem is still open:
How to postinitialize objects after being completely materialized even
with getCollectionByQuery?
Are there any possibilities, please?
Btw I have no proxies set, that may cause the behavior..
Thanks once more
David
---------------------------------------------------------------------
To unsubscribe, e-mail: ojb-user-unsubscribe@db.apache.org
For additional commands, e-mail: ojb-user-help@db.apache.org
Re: OJB Foreign Key Problem in Persistence Broker (MySQL InnoDB)
Posted by Jakob Braeuchi <jb...@gmx.ch>.
hi thomas,
please also post the p6spy-log and your repository.
does this problem occur with other dbms (ie. hsqldb) as well ?
jakob
Thomas Vogt schrieb:
> Hi
>
> I'm using the Persistence Broker to access the data in an MySQL InnoDB
> Database, when the Primary Key is not in the same order as the foreign
> ky constraint is, OJB seems to erranously mix it up and tries the wrong
> way round, this way it won't find a result.
>
> OJB only does so in foreign keys, when the object itself is searched
> directly, it is found without any problem.
>
> I tried to solve the problem on my own, but i did not find the
> problematic part in the source, so could someone with more detail
> knowledge try to identify and fix the problem ?
>
> /I also made an entry in the BugDB Ticket OJB282./
>
> Thanks
>
> Thomas
>
> DATABASE DEFINITION EXTRACT
>
> .....
> PRIMARY KEY(Gruppennummer, Projektnummer, Quartal),
> INDEX Ergebnissdaten_Integer_FKIndex2(Projektnummer, Gruppennummer),
> FOREIGN KEY(Projektnummer, Gruppennummer)
> REFERENCES Projektgruppen(Projektnummer, Gruppennummer)
> ......
>
> CONTENTS EXTRACT
>
> Gruppennummer, Projektnummer
> 1, 1
> 1, 1
> 3, 1
> 2, 2
>
>
> DEBUG LOG EXTRACT
> ......
> core.PersistenceBrokerImpl - Creating RsIterator for class
> [org.fhw.cabaweb.ojb.dataobjects.Projektgruppen]
>
> accesslayer.JdbcAccessImpl - executeQuery : QueryByCriteria from class
> org.fhw.cabaweb.ojb.dataobjects.Projektgruppen where *[[gruppennummer =
> 1, projektnummer = 3]]*
>
> sql.SqlGeneratorDefaultImpl - SQL:SELECT
> A0.Aktiv,A0.Gruppennummer,A0.Gruppenname,A0.Projektnummer FROM
> Projektgruppen A0 WHERE (A0.Gruppennummer = ?) AND A0.Projektnummer = ?
>
> accesslayer.JdbcAccessImpl - executeQuery:
> com.mysql.jdbc.PreparedStatement@7db937: SELECT
> A0.Aktiv,A0.Gruppennummer,A0.Gruppenname,A0.Projektnummer FROM
> Projektgruppen A0 WHERE *(A0.Gruppennummer = 1) AND A0.Projektnummer = 3*
>
> accesslayer.RsIterator -
> RsIterator[org.apache.ojb.broker.accesslayer.RsQueryObject[query:
> QueryByCriteria from class
> org.fhw.cabaweb.ojb.dataobjects.Projektgruppen where [[gruppennummer =
> 1, projektnummer = 3]], class descriptor:
> org.fhw.cabaweb.ojb.dataobjects.Projektgruppen]] initialized
> ......
>
>
>
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: ojb-dev-unsubscribe@db.apache.org
> For additional commands, e-mail: ojb-dev-help@db.apache.org
>
>
---------------------------------------------------------------------
To unsubscribe, e-mail: ojb-dev-unsubscribe@db.apache.org
For additional commands, e-mail: ojb-dev-help@db.apache.org
Re: OJB Foreign Key Problem in Persistence Broker (MySQL InnoDB)
Posted by Thomas Dudziak <to...@first.fhg.de>.
Thomas Vogt wrote:
> Thats what I did, without result, but half an hour ago I found out the
> problem is in repository_user.xml
>
> <reference-descriptor
> name="projektgruppen"
> class-ref="org.fhw.cabaweb.ojb.dataobjects.Projektgruppen">
> <foreignkey field-ref="gruppennummer"/>
> <foreignkey field-ref="projektnummer"/>
> </reference-descriptor>
>
> The order of the foreign key fields here, seems to be mandatory, OJB
> just takes the order as is, and attaches the values it gets. By
> switching the order here, I solved the problem.
>
> But there needs to be some kind of fix in thios place because it took
> me abour 1 day to get there, and only after activating all possible
> debug logs I found that the key order in the search is not the way it
> should be. OJB should check where it puts which value in a select
> statement (compare columnnames).
The main 'problem' here is that OJB currently performs only a few
repository.xml checks. There is a (at the moment discountinued)
validation task that does/should perform these checks because it is not
something that should be done at runtime (e.g. each time your app
starts). due to its performance impact There are a few upcoming changes
to OJB (to be expected for 1.1) that would make the continuation of this
validation task feasible, though.
Could you perhaps 'convert' your bug report into a feature request for 1.1 ?
Tom
---------------------------------------------------------------------
To unsubscribe, e-mail: ojb-dev-unsubscribe@db.apache.org
For additional commands, e-mail: ojb-dev-help@db.apache.org
Re: OJB Foreign Key Problem in Persistence Broker (MySQL InnoDB)
Posted by Thomas Vogt <Th...@TVCS.de>.
Thomas Dudziak schrieb:
> Thomas Vogt wrote:
>
>> I'm using the Persistence Broker to access the data in an MySQL InnoDB
>> Database, when the Primary Key is not in the same order as the foreign
>> ky constraint is, OJB seems to erranously mix it up and tries the wrong
>> way round, this way it won't find a result.
>>
>> OJB only does so in foreign keys, when the object itself is searched
>> directly, it is found without any problem.
>>
>> I tried to solve the problem on my own, but i did not find the
>> problematic part in the source, so could someone with more detail
>> knowledge try to identify and fix the problem ?
>
>
> Why is that a problem ? You could simply change the order of primary
> keys in the class descriptor so that your pk actually matches the fk.
> This does not affect the field order in your class nor the column
> order in the database.
>
Thats what I did, without result, but half an hour ago I found out the
problem is in repository_user.xml
<reference-descriptor
name="projektgruppen"
class-ref="org.fhw.cabaweb.ojb.dataobjects.Projektgruppen">
<foreignkey field-ref="gruppennummer"/>
<foreignkey field-ref="projektnummer"/>
</reference-descriptor>
The order of the foreign key fields here, seems to be mandatory, OJB
just takes the order as is, and attaches the values it gets. By
switching the order here, I solved the problem.
But there needs to be some kind of fix in thios place because it took me
abour 1 day to get there, and only after activating all possible debug
logs I found that the key order in the search is not the way it should
be. OJB should check where it puts which value in a select statement
(compare columnnames).
Thanks
Thomas
> Tom
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: ojb-dev-unsubscribe@db.apache.org
> For additional commands, e-mail: ojb-dev-help@db.apache.org
>
>
>
---------------------------------------------------------------------
To unsubscribe, e-mail: ojb-dev-unsubscribe@db.apache.org
For additional commands, e-mail: ojb-dev-help@db.apache.org
Re: OJB Foreign Key Problem in Persistence Broker (MySQL InnoDB)
Posted by Thomas Dudziak <to...@first.fhg.de>.
Thomas Vogt wrote:
> I'm using the Persistence Broker to access the data in an MySQL InnoDB
> Database, when the Primary Key is not in the same order as the foreign
> ky constraint is, OJB seems to erranously mix it up and tries the wrong
> way round, this way it won't find a result.
>
> OJB only does so in foreign keys, when the object itself is searched
> directly, it is found without any problem.
>
> I tried to solve the problem on my own, but i did not find the
> problematic part in the source, so could someone with more detail
> knowledge try to identify and fix the problem ?
Why is that a problem ? You could simply change the order of primary
keys in the class descriptor so that your pk actually matches the fk.
This does not affect the field order in your class nor the column order
in the database.
Tom
---------------------------------------------------------------------
To unsubscribe, e-mail: ojb-dev-unsubscribe@db.apache.org
For additional commands, e-mail: ojb-dev-help@db.apache.org
OJB Foreign Key Problem in Persistence Broker (MySQL InnoDB)
Posted by Thomas Vogt <Th...@TVCS.de>.
Hi
I'm using the Persistence Broker to access the data in an MySQL InnoDB
Database, when the Primary Key is not in the same order as the foreign
ky constraint is, OJB seems to erranously mix it up and tries the wrong
way round, this way it won't find a result.
OJB only does so in foreign keys, when the object itself is searched
directly, it is found without any problem.
I tried to solve the problem on my own, but i did not find the
problematic part in the source, so could someone with more detail
knowledge try to identify and fix the problem ?
/I also made an entry in the BugDB Ticket OJB282./
Thanks
Thomas
DATABASE DEFINITION EXTRACT
.....
PRIMARY KEY(Gruppennummer, Projektnummer, Quartal),
INDEX Ergebnissdaten_Integer_FKIndex2(Projektnummer, Gruppennummer),
FOREIGN KEY(Projektnummer, Gruppennummer)
REFERENCES Projektgruppen(Projektnummer, Gruppennummer)
......
CONTENTS EXTRACT
Gruppennummer, Projektnummer
1, 1
1, 1
3, 1
2, 2
DEBUG LOG EXTRACT
......
core.PersistenceBrokerImpl - Creating RsIterator for class
[org.fhw.cabaweb.ojb.dataobjects.Projektgruppen]
accesslayer.JdbcAccessImpl - executeQuery : QueryByCriteria from class
org.fhw.cabaweb.ojb.dataobjects.Projektgruppen where *[[gruppennummer =
1, projektnummer = 3]]*
sql.SqlGeneratorDefaultImpl - SQL:SELECT
A0.Aktiv,A0.Gruppennummer,A0.Gruppenname,A0.Projektnummer FROM
Projektgruppen A0 WHERE (A0.Gruppennummer = ?) AND A0.Projektnummer = ?
accesslayer.JdbcAccessImpl - executeQuery:
com.mysql.jdbc.PreparedStatement@7db937: SELECT
A0.Aktiv,A0.Gruppennummer,A0.Gruppenname,A0.Projektnummer FROM
Projektgruppen A0 WHERE *(A0.Gruppennummer = 1) AND A0.Projektnummer = 3*
accesslayer.RsIterator -
RsIterator[org.apache.ojb.broker.accesslayer.RsQueryObject[query:
QueryByCriteria from class
org.fhw.cabaweb.ojb.dataobjects.Projektgruppen where [[gruppennummer =
1, projektnummer = 3]], class descriptor:
org.fhw.cabaweb.ojb.dataobjects.Projektgruppen]] initialized
......
---------------------------------------------------------------------
To unsubscribe, e-mail: ojb-dev-unsubscribe@db.apache.org
For additional commands, e-mail: ojb-dev-help@db.apache.org
Re: how to postinitialize...even with getCollectionByQuery
Posted by Thomas Dudziak <to...@first.fhg.de>.
David Zejda wrote:
> What do you recommend - should I redesign my model to make it not
> dependent on being informed whenewer an object completely materialized?
I guess, that would make a lot of things easier. In my experience, if
you can get away with POJOs in your app (e.g. the domain is not aware of
persistence) then you'll have an easier time developing and working
things. See e.g. the Domain Logic Chapters from Fowler's Book Enterprise
Application Architecture:
http://www.theserverside.com/articles/article.tss?l=FowlerPatterns
Tom
---------------------------------------------------------------------
To unsubscribe, e-mail: ojb-user-unsubscribe@db.apache.org
For additional commands, e-mail: ojb-user-help@db.apache.org
Re: how to postinitialize...even with getCollectionByQuery
Posted by David Zejda <dv...@atlas.cz>.
What do you recommend - should I redesign my model to make it not
dependent on being informed whenewer an object completely materialized?
Thanks.
David
David Zejda wrote:
>> Mhmm, two things:
>>
>> * Are you sure that the thing object is actually stored in the db,
>> i.e. is there a row for it after the store call ?
>
>
> As I'm currently testing it on HSQLDB, there is the script [with most of
> ojb internal parts cut off]:
>
> CREATE TABLE SALE(A_ID INTEGER NOT NULL PRIMARY KEY,THING_ID INTEGER)
> CREATE TABLE THING(A_ID INTEGER NOT NULL PRIMARY KEY)
> ALTER TABLE SALE ADD CONSTRAINT SALE_FK_1 FOREIGN KEY(THING_ID)
> REFERENCES THING(A_ID)
> /*C1*/CONNECT USER sa PASSWORD ""
> SET AUTOCOMMIT FALSE
> /*C2*/CONNECT USER sa PASSWORD ""
> SET AUTOCOMMIT FALSE
> INSERT INTO OJB_HL_SEQ VALUES('global - default sequence
> name','deprecatedColumn',10020,20,1)
> COMMIT
> SET AUTOCOMMIT TRUE
> /*C1*/INSERT INTO THING VALUES(10002)
> INSERT INTO SALE VALUES(10001,10002)
> COMMIT
> SET AUTOCOMMIT TRUE
>
> ..so I think, the "thing" row is there..
>
>> * The getCollectionByQuery call is not done within a transaction. I'm
>> not sure whether that's supposed to work. Try changing it to:
>
>
> No change - behaves equally.
>
>> Tom
>
>
> Seems like cursed, doesn't it? :-)
>
> Thanks for your concernment!
>
> David
---------------------------------------------------------------------
To unsubscribe, e-mail: ojb-user-unsubscribe@db.apache.org
For additional commands, e-mail: ojb-user-help@db.apache.org
Re: how to postinitialize...even with getCollectionByQuery
Posted by David Zejda <dv...@atlas.cz>.
> Mhmm, two things:
>
> * Are you sure that the thing object is actually stored in the db, i.e.
> is there a row for it after the store call ?
As I'm currently testing it on HSQLDB, there is the script [with most of
ojb internal parts cut off]:
CREATE TABLE SALE(A_ID INTEGER NOT NULL PRIMARY KEY,THING_ID INTEGER)
CREATE TABLE THING(A_ID INTEGER NOT NULL PRIMARY KEY)
ALTER TABLE SALE ADD CONSTRAINT SALE_FK_1 FOREIGN KEY(THING_ID)
REFERENCES THING(A_ID)
/*C1*/CONNECT USER sa PASSWORD ""
SET AUTOCOMMIT FALSE
/*C2*/CONNECT USER sa PASSWORD ""
SET AUTOCOMMIT FALSE
INSERT INTO OJB_HL_SEQ VALUES('global - default sequence
name','deprecatedColumn',10020,20,1)
COMMIT
SET AUTOCOMMIT TRUE
/*C1*/INSERT INTO THING VALUES(10002)
INSERT INTO SALE VALUES(10001,10002)
COMMIT
SET AUTOCOMMIT TRUE
..so I think, the "thing" row is there..
> * The getCollectionByQuery call is not done within a transaction. I'm
> not sure whether that's supposed to work. Try changing it to:
No change - behaves equally.
> Tom
Seems like cursed, doesn't it? :-)
Thanks for your concernment!
David
---------------------------------------------------------------------
To unsubscribe, e-mail: ojb-user-unsubscribe@db.apache.org
For additional commands, e-mail: ojb-user-help@db.apache.org
OJB Foreign Key Problem in Persistence Broker (MySQL InnoDB)
Posted by Tomahawk <To...@ThePentagon.com>.
Hi
I'm using the Persistence Broker to access the data in an MySQL InnoDB
Database, when the Primary Key is not in the same order as the foreign
ky constraint is, OJB seems to erranously mix it up and tries the wrong
way round, this way it won't find a result.
OJB only does so in foreign keys, when the object itself is searched
directly, it is found without any problem.
I tried to solve the problem on my own, but i did not find the
problematic part in the source, so could someone with more detail
knowledge try to identify and fix the problem ?
/I also made an entry in the BugDB Ticket OJB282./
Thanks
Tom
DATABASE DEFINITION EXTRACT
.....
PRIMARY KEY(Gruppennummer, Projektnummer, Quartal),
INDEX Ergebnissdaten_Integer_FKIndex2(Projektnummer, Gruppennummer),
FOREIGN KEY(Projektnummer, Gruppennummer)
REFERENCES Projektgruppen(Projektnummer, Gruppennummer)
......
CONTENTS EXTRACT
Gruppennummer, Projektnummer
1, 1
1, 1
3, 1
2, 2
DEBUG LOG EXTRACT
......
core.PersistenceBrokerImpl - Creating RsIterator for class
[org.fhw.cabaweb.ojb.dataobjects.Projektgruppen]
accesslayer.JdbcAccessImpl - executeQuery : QueryByCriteria from class
org.fhw.cabaweb.ojb.dataobjects.Projektgruppen where *[[gruppennummer =
1, projektnummer = 3]]*
sql.SqlGeneratorDefaultImpl - SQL:SELECT
A0.Aktiv,A0.Gruppennummer,A0.Gruppenname,A0.Projektnummer FROM
Projektgruppen A0 WHERE (A0.Gruppennummer = ?) AND A0.Projektnummer = ?
accesslayer.JdbcAccessImpl - executeQuery:
com.mysql.jdbc.PreparedStatement@7db937: SELECT
A0.Aktiv,A0.Gruppennummer,A0.Gruppenname,A0.Projektnummer FROM
Projektgruppen A0 WHERE *(A0.Gruppennummer = 1) AND A0.Projektnummer = 3*
accesslayer.RsIterator -
RsIterator[org.apache.ojb.broker.accesslayer.RsQueryObject[query:
QueryByCriteria from class
org.fhw.cabaweb.ojb.dataobjects.Projektgruppen where [[gruppennummer =
1, projektnummer = 3]], class descriptor:
org.fhw.cabaweb.ojb.dataobjects.Projektgruppen]] initialized
......
---------------------------------------------------------------------
To unsubscribe, e-mail: ojb-user-unsubscribe@db.apache.org
For additional commands, e-mail: ojb-user-help@db.apache.org
OJB Foreign Key Problem in Persistence Broker (MySQL InnoDB)
Posted by Thomas Vogt <Th...@TVCS.de>.
Hi
I'm using the Persistence Broker to access the data in an MySQL InnoDB
Database, when the Primary Key is not in the same order as the foreign
ky constraint is, OJB seems to erranously mix it up and tries the wrong
way round, this way it won't find a result.
OJB only does so in foreign keys, when the object itself is searched
directly, it is found without any problem.
I tried to solve the problem on my own, but i did not find the
problematic part in the source, so could someone with more detail
knowledge try to identify and fix the problem ?
/I also made an entry in the BugDB Ticket OJB282./
Thanks
Thomas
DATABASE DEFINITION EXTRACT
.....
PRIMARY KEY(Gruppennummer, Projektnummer, Quartal),
INDEX Ergebnissdaten_Integer_FKIndex2(Projektnummer, Gruppennummer),
FOREIGN KEY(Projektnummer, Gruppennummer)
REFERENCES Projektgruppen(Projektnummer, Gruppennummer)
......
CONTENTS EXTRACT
Gruppennummer, Projektnummer
1, 1
1, 1
3, 1
2, 2
DEBUG LOG EXTRACT
......
core.PersistenceBrokerImpl - Creating RsIterator for class
[org.fhw.cabaweb.ojb.dataobjects.Projektgruppen]
accesslayer.JdbcAccessImpl - executeQuery : QueryByCriteria from class
org.fhw.cabaweb.ojb.dataobjects.Projektgruppen where *[[gruppennummer =
1, projektnummer = 3]]*
sql.SqlGeneratorDefaultImpl - SQL:SELECT
A0.Aktiv,A0.Gruppennummer,A0.Gruppenname,A0.Projektnummer FROM
Projektgruppen A0 WHERE (A0.Gruppennummer = ?) AND A0.Projektnummer = ?
accesslayer.JdbcAccessImpl - executeQuery:
com.mysql.jdbc.PreparedStatement@7db937: SELECT
A0.Aktiv,A0.Gruppennummer,A0.Gruppenname,A0.Projektnummer FROM
Projektgruppen A0 WHERE *(A0.Gruppennummer = 1) AND A0.Projektnummer = 3*
accesslayer.RsIterator -
RsIterator[org.apache.ojb.broker.accesslayer.RsQueryObject[query:
QueryByCriteria from class
org.fhw.cabaweb.ojb.dataobjects.Projektgruppen where [[gruppennummer =
1, projektnummer = 3]], class descriptor:
org.fhw.cabaweb.ojb.dataobjects.Projektgruppen]] initialized
......
---------------------------------------------------------------------
To unsubscribe, e-mail: ojb-dev-unsubscribe@db.apache.org
For additional commands, e-mail: ojb-dev-help@db.apache.org
Re: how to postinitialize...even with getCollectionByQuery
Posted by Thomas Dudziak <to...@first.fhg.de>.
David Zejda wrote:
>> The PersistenceBrokerAware is a special case of listener, so you
>> probably would get the same behaviour. But why is it not working for
>> you ? Depending on what you're interested in, you override afterUpdate
>> and afterLookup. The latter is called when you traverse the collection
>> via the iterator's next() method.
>> Tom
>
>
> I really don't know, why it is not working for me and it's also a
> reason, why I'm asking you for help..
>
> Possibly, there is something smelling in my code?
>
> On the first run, it behaves OK:
>
> not in db
> test.Thing
> => stored
>
> On the second run, the exception is thrown to demonstrate,
> that Sale is not materialized completely (~ with Thing):
>
> test.Sale
> afterLookup
> java.lang.UnsupportedOperationException: not completely initialized!
> at test.Sale.afterLookup(Unknown Source)
> ...
>
> Before "afterLookup" there should be "test.Thing", informing about thing
> instantiation.
>
> If instanitated via getObjectByQuery instead of getCollectionByQuery,
> it works OK.
>
> There are the demonstration classes:
>
> package test;
> import java.util.*;
> import org.apache.ojb.broker.query.*;
> import org.apache.ojb.broker.*;
>
> public class Main
> {
> private static PersistenceBroker broker =
> PersistenceBrokerFactory.defaultPersistenceBroker();
>
> public Main()
> {
> // on the second run should materialize 1 instance
> Query query = new QueryByCriteria(Sale.class, null);
> Collection c = broker.getCollectionByQuery(query);
> // on first run stores the Sale with Thing
> if (c.size() == 0)
> {
> System.out.println("not in db");
> Thing t = new Thing();
> Sale s = new Sale(t);
> broker.beginTransaction();
> broker.store(s);
> System.out.println(" => stored");
> broker.commitTransaction();
> }
> else
> {
> System.out.println(c.iterator().next());
> }
> System.exit(0);
> }
>
> public static void main(String[] args)
> {
> new Main();
> }
> }
Mhmm, two things:
* Are you sure that the thing object is actually stored in the db, i.e.
is there a row for it after the store call ?
* The getCollectionByQuery call is not done within a transaction. I'm
not sure whether that's supposed to work. Try changing it to:
public Main()
{
try
{
broker.beginTransaction();
// on the second run should materialize 1 instance
Query query = new QueryByCriteria(Sale.class, null);
Collection c = broker.getCollectionByQuery(query);
// on first run stores the Sale with Thing
if (c.size() == 0)
{
System.out.println("not in db");
Thing t = new Thing();
Sale s = new Sale(t);
broker.store(s);
System.out.println(" => stored");
}
else
{
System.out.println(c.iterator().next());
}
}
finally
{
broker.commitTransaction();
}
System.exit(0);
}
Tom
---------------------------------------------------------------------
To unsubscribe, e-mail: ojb-user-unsubscribe@db.apache.org
For additional commands, e-mail: ojb-user-help@db.apache.org
Re: how to postinitialize...even with getCollectionByQuery
Posted by David Zejda <dv...@atlas.cz>.
> The PersistenceBrokerAware is a special case of listener, so you
> probably would get the same behaviour. But why is it not working for you
> ? Depending on what you're interested in, you override afterUpdate and
> afterLookup. The latter is called when you traverse the collection via
> the iterator's next() method.
> Tom
I really don't know, why it is not working for me and it's also a
reason, why I'm asking you for help..
Possibly, there is something smelling in my code?
On the first run, it behaves OK:
not in db
test.Thing
=> stored
On the second run, the exception is thrown to demonstrate,
that Sale is not materialized completely (~ with Thing):
test.Sale
afterLookup
java.lang.UnsupportedOperationException: not completely initialized!
at test.Sale.afterLookup(Unknown Source)
...
Before "afterLookup" there should be "test.Thing", informing about thing
instantiation.
If instanitated via getObjectByQuery instead of getCollectionByQuery,
it works OK.
There are the demonstration classes:
package test;
import java.util.*;
import org.apache.ojb.broker.query.*;
import org.apache.ojb.broker.*;
public class Main
{
private static PersistenceBroker broker =
PersistenceBrokerFactory.defaultPersistenceBroker();
public Main()
{
// on the second run should materialize 1 instance
Query query = new QueryByCriteria(Sale.class, null);
Collection c = broker.getCollectionByQuery(query);
// on first run stores the Sale with Thing
if (c.size() == 0)
{
System.out.println("not in db");
Thing t = new Thing();
Sale s = new Sale(t);
broker.beginTransaction();
broker.store(s);
System.out.println(" => stored");
broker.commitTransaction();
}
else
{
System.out.println(c.iterator().next());
}
System.exit(0);
}
public static void main(String[] args)
{
new Main();
}
}
package test;
import java.util.*;
/**
* @ojb.class include-inherited="true"
*/
public class Sale implements org.apache.ojb.broker.PersistenceBrokerAware
{
/**
* Artificial ID key holder
*
* @ojb.field nullable="false"
* autoincrement="ojb"
* primarykey="true"
*/
private Integer a_id;
/**
* Referential ID key holder
*
* @ojb.field
*/
private Integer thing_id;
/**
*
* @ojb.reference class-ref="test.Thing"
* foreignkey="thing_id"
* auto-retrieve="true"
* auto-update="true"
* auto-delete="true"
*/
protected Thing thing;
public Sale()
{
System.out.println(this.getClass().getName());
}
public Sale(Thing t)
{
this.thing = t;
}
public String toString()
{
return "s:"+thing.toString();
}
public void beforeDelete(org.apache.ojb.broker.PersistenceBroker
broker) throws org.apache.ojb.broker.PersistenceBrokerException { }
public void beforeInsert(org.apache.ojb.broker.PersistenceBroker
broker) throws org.apache.ojb.broker.PersistenceBrokerException { }
public void beforeUpdate(org.apache.ojb.broker.PersistenceBroker
broker) throws org.apache.ojb.broker.PersistenceBrokerException { }
public void afterUpdate(org.apache.ojb.broker.PersistenceBroker
broker) throws org.apache.ojb.broker.PersistenceBrokerException { }
public void afterDelete(org.apache.ojb.broker.PersistenceBroker
broker) throws org.apache.ojb.broker.PersistenceBrokerException { }
public void afterInsert(org.apache.ojb.broker.PersistenceBroker
broker) throws org.apache.ojb.broker.PersistenceBrokerException { }
public void afterLookup(org.apache.ojb.broker.PersistenceBroker
broker) throws org.apache.ojb.broker.PersistenceBrokerException
{
System.out.println("afterLookup");
if (thing == null)
throw new UnsupportedOperationException(
"not completely initialized!");
}
}
package test;
import java.util.*;
/**
* @ojb.class include-inherited="true"
*/
public class Thing
{
/**
* Artificial ID key holder
*
* @ojb.field nullable="false"
* autoincrement="ojb"
* primarykey="true"
*/
private Integer a_id;
public Thing()
{
System.out.println(this.getClass().getName());
}
}
---------------------------------------------------------------------
To unsubscribe, e-mail: ojb-user-unsubscribe@db.apache.org
For additional commands, e-mail: ojb-user-help@db.apache.org
Re: how to postinitialize...even with getCollectionByQuery
Posted by Thomas Dudziak <to...@first.fhg.de>.
David Zejda wrote:
> Ugh.
> The seeming of connection between the objectcache and the order of
> initialization steps was only a result of a bug in my testcase, so the
> problem is still open:
>
> How to postinitialize objects after being completely materialized even
> with getCollectionByQuery?
>
> Are there any possibilities, please?
> E.g. should I go the way of listening to broker?
The PersistenceBrokerAware is a special case of listener, so you
probably would get the same behaviour. But why is it not working for you
? Depending on what you're interested in, you override afterUpdate and
afterLookup. The latter is called when you traverse the collection via
the iterator's next() method.
Tom
---------------------------------------------------------------------
To unsubscribe, e-mail: ojb-user-unsubscribe@db.apache.org
For additional commands, e-mail: ojb-user-help@db.apache.org
Re: how to postinitialize...even with getCollectionByQuery
Posted by David Zejda <dv...@atlas.cz>.
> The solution was to replace
> org.apache.ojb.broker.cache.ObjectCacheDefaultImpl
> with
> org.apache.ojb.broker.cache.ObjectCacheEmptyImpl, so now it goes OK:
Ugh.
The seeming of connection between the objectcache and the order of
initialization steps was only a result of a bug in my testcase, so the
problem is still open:
How to postinitialize objects after being completely materialized even
with getCollectionByQuery?
Are there any possibilities, please?
E.g. should I go the way of listening to broker?
Btw I have no proxies set, that may cause the behavior..
Thanks once more
David
---------------------------------------------------------------------
To unsubscribe, e-mail: ojb-user-unsubscribe@db.apache.org
For additional commands, e-mail: ojb-user-help@db.apache.org
Re: how to postinitialize objects... [possibly solved]
Posted by David Zejda <dv...@atlas.cz>.
> The initialization-method is called from the
> RowReaderDefaultImpl#readObjectFrom method as the last step, right after
> the field values have been set. The references and collections are read
> later, so if you require them, you'll have to either supply a Listener
> for PB events to the PersistenceBroker, or implement the
> PersistenceBrokerAware interface.
>
> Tom
Finally, I had chosen the PersistenceBrokerAware.
It worked smoothly with broker.getObjectByQuery, but not with
broker.getCollectionByQuery:
test.Sale()
test.Sale.afterLookup()
test.ThingAndCost() [collection element]
test.ThingAndCostSet() [collection class]
test.Thing() [reference from collection element]
The solution was to replace
org.apache.ojb.broker.cache.ObjectCacheDefaultImpl
with
org.apache.ojb.broker.cache.ObjectCacheEmptyImpl, so now it goes OK:
test.Sale()
test.ThingAndCost() [collection element]
test.ThingAndCostSet() [collection class]
test.Thing() [reference from collection element]
test.Sale.afterLookup()
Please, how much this affects the performance?
Thanks
David
---------------------------------------------------------------------
To unsubscribe, e-mail: ojb-user-unsubscribe@db.apache.org
For additional commands, e-mail: ojb-user-help@db.apache.org
Re: how to postinitialize objects after being completely materialized
Posted by Thomas Dudziak <to...@first.fhg.de>.
David Zejda wrote:
> It seems, that the order of initialization steps is
>
> 1) constructor() (suprisingly :)
> 2) initialization-method
> 3) rowreader's readObjectFrom
> 4) constructors of references etc.
> 5) afterLookup
The initialization-method is called from the
RowReaderDefaultImpl#readObjectFrom method as the last step, right after
the field values have been set. The references and collections are read
later, so if you require them, you'll have to either supply a Listener
for PB events to the PersistenceBroker, or implement the
PersistenceBrokerAware interface.
Tom
---------------------------------------------------------------------
To unsubscribe, e-mail: ojb-user-unsubscribe@db.apache.org
For additional commands, e-mail: ojb-user-help@db.apache.org
Re: how to postinitialize objects after being completely materialized
Posted by David Zejda <dv...@atlas.cz>.
It seems, that the order of initialization steps is
1) constructor() (suprisingly :)
2) initialization-method
3) rowreader's readObjectFrom
4) constructors of references etc.
5) afterLookup
..so the only possibility is the fifth one.
I'm not so happy to implement 7 methods of PersistenceBrokerAware in
most of my objects only becouse one of them, but this is the only
operating solution I currently know, so get to it..
Thanks once more
David
Charles Anthony wrote:
> Hi David,
>
> Another approach : if you get your persistent objects to implement
> org.apache.ojb.broker.PersistenceBrokerAware interface, you could then
> implement the afterLookup method e.g.
>
> public void afterLookup(PersistenceBroker broker) {
> _postinit();
> };
>
> The afterLookup method is invoked after a persistence broker loads the
> object - and, I believe, after any of the references are loaded too.
>
> Some people would not like this approach (due to the persistency layer
> "invading" your domain objects) - I am not one of them.
>
> HTH,
>
> Chaeers,
>
>
>>-----Original Message-----
>>From: David Zejda [mailto:dvid@atlas.cz]
>>Sent: 26 July 2004 09:59
>>To: OJB Users List
>>Subject: how to postinitialize objects after being completely
>>materialized
>>
>>
>>I have a custom row reader able to initialize materialized objects by
>>their _postinit() methods. Essentially it works, but there is
>>a problem
>>- the _postinit() should be called after the object is materialized
>>completely, i.e. with all fields, but also references and collections
>>filled. So, I guess I have to override some other method of
>>rowreader or
>> something completely different - please, give a hint.
>>
>>public class RowReaderPostinitializingImpl extends
>>RowReaderDefaultImpl
>>{
>> public RowReaderPostinitializingImpl(ClassDescriptor c)
>> {
>> super(c);
>> }
>>
>> public Object readObjectFrom(Map row)
>> {
>> Object result = super.readObjectFrom(row);
>> if (result instanceof Postinitializable)
>> {
>> Postinitializable art = (Postinitializable) result;
>> art._postinit();
>> return art;
>> }
>> else
>> {
>> return result;
>> }
>> }
>>}
---------------------------------------------------------------------
To unsubscribe, e-mail: ojb-user-unsubscribe@db.apache.org
For additional commands, e-mail: ojb-user-help@db.apache.org
Re: how to postinitialize objects after being completely materialized
Posted by David Zejda <dv...@atlas.cz>.
Thanks, Thomas & Charles...
I'll try it out.
David
---------------------------------------------------------------------
To unsubscribe, e-mail: ojb-user-unsubscribe@db.apache.org
For additional commands, e-mail: ojb-user-help@db.apache.org