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 Łukasz Korzybski <na...@xtr.net.pl> on 2004/11/09 23:03:41 UTC

ODMG questions

Hello,

I'm currently learning OJB, we are going to use it for our web app. I've got 
some unresolved questions

1.  in Basic O/R mapping I've read that in ODMG default auto-update is set to 
false which is link. I read that in this case ODMG will not insert nor update 
my collection objects if i do write lock on master object. But it does both!

	    Transaction tx = odmg.newTransaction();
            tx.begin();
            
            person = new Person("Michal", "Korzybski", new Integer(22));
            Address a = new Address();
            a.setCity("Rumia");
            a.setStreet("Dunikowskiego 2");
            person.addAddress(a);
            
            tx.lock(osoba, tx.WRITE);
            tx.commit();

After this code I've person and address stored in DB. I'm confused.

2. I'm not sure if I can safely begin odmg transaction in one servlet then 
pass it to other servlet and commit it or rollback after a while. Does 
transaction return db-connection to the pool after every OQL query and later 
on commit it borrows it once again? 

3. I can't commit object with null FK and no idea why, probably i'm doing 
something wrong.

I'm thankful for any help. (sorry for my english)

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


Re: ODMG questions

Posted by Gerhard Grosse <ge...@lex-com.net>.
On Wed, 10 Nov 2004 16:15:16 +0100, £ukasz Korzybski <na...@xtr.net.pl>
wrote:

>Dnia ¶roda, 10 listopada 2004 10:02, Gerhard Grosse napisa³:
>> On Wed, 10 Nov 2004 01:51:59 +0100, £ukasz Korzybski <na...@xtr.net.pl>
>>
>> wrote:
>> >Thanks for reply,
>> >
>> >Dnia ¶roda, 10 listopada 2004 00:19, Justin Stanczak napisa³:
>> >> Justin Stanczak, Web Manager
>> >> Vincennes University
>
>> >
>> >OK, but then why this code doesn't work as the previous one:
>> >
>> >            Address address = new Address();
>> >            adres.setCity("Rumia1");
>> >            adres.setStreet("Dunikowskiego 10");
>> >
>> >
>> >            OQLQuery query = odmg.newOQLQuery();
>> >            query.create("select osoba from " + Osoba.class.getName() +
>> > "where id=1");
>> >            DList res = (DList) query.execute();
>> >            person = (Person) res.iterator().next();
>> >
>> >            person.setName("Adam");
>> >            person.setAddress(adres);
>> >
>> >            tx.lock(person, tx.WRITE);
>> >            tx.commit();
>> >
>> >I thought that it would store both person and address, but unless I
>> > explicitly write tx.lock(address, tx.WRITE) I get a key violation:
>> >
>> >ERROR: SQLException during the execution of the update (for a
>> > ojbtest.Person): ERROR: insert or update on table "person" violates
>> > foreign key constraint "person_fk_1"
>> >
>> >The only difference is I retrieve person from db rather than create a new
>> > one, I'm lost and haven't got ideas how to use odmg properly.
>>
>> You need to call tx.lock(persion, Transaction.WRITE) _before_
>> modifying the persion object. Otherwise OJB will not recognize that
>> the object has been modified.
>>
>> Gerhard
>>
>
>It doesn't help in this particular case, it seems that I have to do write lock 
>on new object even when I link it as a dependent to some other persistent 
>object.

AFAIK the 'normal' way to manipulate objects in ODMG is: make new
objects persistent by calling Database.makePersistent(newOjbect) and
call tx.lock(existingObject, Transaction.WRITE) before modifying it.
Depending on the implicitLocking setting in OJB.properties, the latter
will automatically write-lock all referenced objects as well.

HTH
Gerhard


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


Re: ODMG questions

Posted by Łukasz Korzybski <na...@xtr.net.pl>.
Dnia środa, 10 listopada 2004 10:02, Gerhard Grosse napisał:
> On Wed, 10 Nov 2004 01:51:59 +0100, Łukasz Korzybski <na...@xtr.net.pl>
>
> wrote:
> >Thanks for reply,
> >
> >Dnia środa, 10 listopada 2004 00:19, Justin Stanczak napisał:
> >> Justin Stanczak, Web Manager
> >> Vincennes University

> >
> >OK, but then why this code doesn't work as the previous one:
> >
> >            Address address = new Address();
> >            adres.setCity("Rumia1");
> >            adres.setStreet("Dunikowskiego 10");
> >
> >
> >            OQLQuery query = odmg.newOQLQuery();
> >            query.create("select osoba from " + Osoba.class.getName() +
> > "where id=1");
> >            DList res = (DList) query.execute();
> >            person = (Person) res.iterator().next();
> >
> >            person.setName("Adam");
> >            person.setAddress(adres);
> >
> >            tx.lock(person, tx.WRITE);
> >            tx.commit();
> >
> >I thought that it would store both person and address, but unless I
> > explicitly write tx.lock(address, tx.WRITE) I get a key violation:
> >
> >ERROR: SQLException during the execution of the update (for a
> > ojbtest.Person): ERROR: insert or update on table "person" violates
> > foreign key constraint "person_fk_1"
> >
> >The only difference is I retrieve person from db rather than create a new
> > one, I'm lost and haven't got ideas how to use odmg properly.
>
> You need to call tx.lock(persion, Transaction.WRITE) _before_
> modifying the persion object. Otherwise OJB will not recognize that
> the object has been modified.
>
> Gerhard
>

It doesn't help in this particular case, it seems that I have to do write lock 
on new object even when I link it as a dependent to some other persistent 
object.

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


Re: ODMG questions

Posted by Gerhard Grosse <ge...@lex-com.net>.
On Wed, 10 Nov 2004 01:51:59 +0100, £ukasz Korzybski <na...@xtr.net.pl>
wrote:

>
>Thanks for reply,
>
>Dnia ¶roda, 10 listopada 2004 00:19, Justin Stanczak napisa³:
>> Justin Stanczak, Web Manager
>> Vincennes University
>> 812-888-5813
>>
>>
>> "All that is necessary for the triumph of evil is that good men do
>> nothing." Edmund Burke
>>
>
>
>>>1.  in Basic O/R mapping I've read that in ODMG default auto-update is set 
>>>to 
>>>false which is link. I read that in this case ODMG will not insert nor 
>>>update 
>>>my collection objects if i do write lock on master object. But it does both!
>>>
>>>           Transaction tx = odmg.newTransaction();
>>>            tx.begin();
>>>            
>>>            person = new Person("Michal", "Korzybski", new Integer(22));
>>>            Address a = new Address();
>>>            a.setCity("Rumia");
>>>            a.setStreet("Dunikowskiego 2");
>>>            person.addAddress(a);
>>>            
>>>            tx.lock(osoba, tx.WRITE);
>>>            tx.commit();
>>>
>>>After this code I've person and address stored in DB. I'm confused.
>>>  
>>>
>>Yes, it does by default. I don't think the docs are perfect.
>
>OK, but then why this code doesn't work as the previous one:
>
>            Address address = new Address();
>            adres.setCity("Rumia1");
>            adres.setStreet("Dunikowskiego 10");
>            
>            
>            OQLQuery query = odmg.newOQLQuery();
>            query.create("select osoba from " + Osoba.class.getName() + "where  
>id=1");
>            DList res = (DList) query.execute();
>            person = (Person) res.iterator().next();
>            
>            person.setName("Adam");
>            person.setAddress(adres);          
>            
>            tx.lock(person, tx.WRITE);
>            tx.commit();
>
>I thought that it would store both person and address, but unless I explicitly 
>write tx.lock(address, tx.WRITE) I get a key violation:
>
>ERROR: SQLException during the execution of the update (for a ojbtest.Person): 
>ERROR: insert or update on table "person" violates foreign key constraint 
>"person_fk_1"
>
>The only difference is I retrieve person from db rather than create a new one, 
>I'm lost and haven't got ideas how to use odmg properly.

You need to call tx.lock(persion, Transaction.WRITE) _before_
modifying the persion object. Otherwise OJB will not recognize that
the object has been modified.

Gerhard

>>
>> >3. I can't commit object with null FK and no idea why, probably i'm doing
>> >something wrong.
>>
>> I'm not sure here. You should be able to, or maybe I'm not understanding
>> what you're doing.
>
>The problem was that I had integer as PK/FK rather than Integer so OJB was 
>trying to save null as 0 and it caused key violation, my mistake but it took 
>me almost a day.
>
>regadrs, naos



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


Re: ODMG questions

Posted by Łukasz Korzybski <na...@xtr.net.pl>.
Thanks for reply,

Dnia środa, 10 listopada 2004 00:19, Justin Stanczak napisał:
> Justin Stanczak, Web Manager
> Vincennes University
> 812-888-5813
>
>
> "All that is necessary for the triumph of evil is that good men do
> nothing." Edmund Burke
>


>>1.  in Basic O/R mapping I've read that in ODMG default auto-update is set 
>>to 
>>false which is link. I read that in this case ODMG will not insert nor 
>>update 
>>my collection objects if i do write lock on master object. But it does both!
>>
>>           Transaction tx = odmg.newTransaction();
>>            tx.begin();
>>            
>>            person = new Person("Michal", "Korzybski", new Integer(22));
>>            Address a = new Address();
>>            a.setCity("Rumia");
>>            a.setStreet("Dunikowskiego 2");
>>            person.addAddress(a);
>>            
>>            tx.lock(osoba, tx.WRITE);
>>            tx.commit();
>>
>>After this code I've person and address stored in DB. I'm confused.
>>  
>>
>Yes, it does by default. I don't think the docs are perfect.

OK, but then why this code doesn't work as the previous one:

            Address address = new Address();
            adres.setCity("Rumia1");
            adres.setStreet("Dunikowskiego 10");
            
            
            OQLQuery query = odmg.newOQLQuery();
            query.create("select osoba from " + Osoba.class.getName() + "where  
id=1");
            DList res = (DList) query.execute();
            person = (Person) res.iterator().next();
            
            person.setName("Adam");
            person.setAddress(adres);          
            
            tx.lock(person, tx.WRITE);
            tx.commit();

I thought that it would store both person and address, but unless I explicitly 
write tx.lock(address, tx.WRITE) I get a key violation:

ERROR: SQLException during the execution of the update (for a ojbtest.Person): 
ERROR: insert or update on table "person" violates foreign key constraint 
"person_fk_1"

The only difference is I retrieve person from db rather than create a new one, 
I'm lost and haven't got ideas how to use odmg properly.
>
> >3. I can't commit object with null FK and no idea why, probably i'm doing
> >something wrong.
>
> I'm not sure here. You should be able to, or maybe I'm not understanding
> what you're doing.

The problem was that I had integer as PK/FK rather than Integer so OJB was 
trying to save null as 0 and it caused key violation, my mistake but it took 
me almost a day.

regadrs, naos

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


Re: ODMG questions

Posted by Justin Stanczak <js...@vinu.edu>.
Justin Stanczak, Web Manager
Vincennes University
812-888-5813


"All that is necessary for the triumph of evil is that good men do nothing." Edmund Burke



Łukasz Korzybski wrote:

>Hello,
>
>I'm currently learning OJB, we are going to use it for our web app. I've got 
>some unresolved questions
>
>1.  in Basic O/R mapping I've read that in ODMG default auto-update is set to 
>false which is link. I read that in this case ODMG will not insert nor update 
>my collection objects if i do write lock on master object. But it does both!
>
>	    Transaction tx = odmg.newTransaction();
>            tx.begin();
>            
>            person = new Person("Michal", "Korzybski", new Integer(22));
>            Address a = new Address();
>            a.setCity("Rumia");
>            a.setStreet("Dunikowskiego 2");
>            person.addAddress(a);
>            
>            tx.lock(osoba, tx.WRITE);
>            tx.commit();
>
>After this code I've person and address stored in DB. I'm confused.
>  
>
Yes, it does by default. I don't think the docs are perfect.

>2. I'm not sure if I can safely begin odmg transaction in one servlet then 
>pass it to other servlet and commit it or rollback after a while. Does 
>transaction return db-connection to the pool after every OQL query and later 
>on commit it borrows it once again? 
>  
>
I'm not sure when it finally does release the connection, but you should 
be able to as long as your working in the same transaction. I've not 
done from servlet to servlet, but I have Servlet that start one and pass 
it to different handlers in the same servlet before commit.

>3. I can't commit object with null FK and no idea why, probably i'm doing 
>something wrong.
>  
>
I'm not sure here. You should be able to, or maybe I'm not understanding 
what you're doing.

>I'm thankful for any help. (sorry for my english)
>
>---------------------------------------------------------------------
>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