Persisting object and foreign keys

View: New views
3 Messages — Rating Filter:   Alert me  

Persisting object and foreign keys

by LFS :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

Hi

This is my situation: I have an Address object that I
persist/retrieve from the DB. It contains a City object, reperesenting
the city on which that address is. City contains State, and State
contains Country object (a long N-1 chain).
When I'm creating a new
address from user input, the user selected its city from a combo box
which I populated from DB, then I'll have the city's ID.
When I try
to persist the new address, the most I can do is put a City object on
it, containing only the city ID. The problem is that persisting this
will erase the City record for that ID on the DB, because other fields
like Name and State will be null. I want simply to set the foreign id
on the Address object, nothing else.

I believe there is some
good solution for this... my intention is not updating the City object,
and retrieving it to put it back on the address object before being
persisted sounds very strange and a performance nightmare.

What's the proper way to do this?

Thanks!

Note: I'm using application identity and DataNucleus JDO.


      ____________________________________________________________________________________
Veja quais são os assuntos do momento no Yahoo! +Buscados
http://br.maisbuscados.yahoo.com

Re: Persisting object and foreign keys

by Craig L Russell :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

Hi,

If the combo box is populated from the database, perhaps you could  
keep a detached City object around (as a value of a Map corresponding  
to the name of the city as the key) and when the new Address instance  
is created, use the detached City instance as the value of the City  
field in the new Address instance. When you do makePersistent on the  
Address, the detached City instance will be reconnected to the database.

Craig

On Jun 29, 2009, at 12:54 PM, LFS wrote:

> Hi
>
> This is my situation: I have an Address object that I
> persist/retrieve from the DB. It contains a City object, reperesenting
> the city on which that address is. City contains State, and State
> contains Country object (a long N-1 chain).
> When I'm creating a new
> address from user input, the user selected its city from a combo box
> which I populated from DB, then I'll have the city's ID.
> When I try
> to persist the new address, the most I can do is put a City object on
> it, containing only the city ID. The problem is that persisting this
> will erase the City record for that ID on the DB, because other fields
> like Name and State will be null. I want simply to set the foreign id
> on the Address object, nothing else.
>
> I believe there is some
> good solution for this... my intention is not updating the City  
> object,
> and retrieving it to put it back on the address object before being
> persisted sounds very strange and a performance nightmare.
>
> What's the proper way to do this?
>
> Thanks!
>
> Note: I'm using application identity and DataNucleus JDO.
>
>
>      
> ____________________________________________________________________________________
> Veja quais são os assuntos do momento no Yahoo! +Buscados
> http://br.maisbuscados.yahoo.com
Craig L Russell
Architect, Sun Java Enterprise System http://db.apache.org/jdo
408 276-5638 mailto:Craig.Russell@...
P.S. A good JDO? O, Gasp!



smime.p7s (3K) Download Attachment

Parent Message unknown Re: Persisting object and foreign keys

by LFS :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

Hi,

I have thought of this, but it sounded like an awful memory waste... it can rise to very high memory consumption, just to keep this data around, like in the user session.
I also thought of using getObjectById to get the city object before persisting the address, but that is another query hitting the DB (cache may help, but does not solve it).

Neither of these solutions sound nice... this is some pretty trivial operation, and there should be a better solution for it. How can I do it? Could database identity help it somehow?

Thanks again!

--- Em seg, 29/6/09, Craig L Russell <Craig.Russell@...> escreveu:

De: Craig L Russell <Craig.Russell@...>
Assunto: Re: Persisting object and foreign keys
Para: jdo-user@...
Data: Segunda-feira, 29 de Junho de 2009, 18:43

Hi,

If the combo box is populated from the database, perhaps you could keep a detached City object around (as a value of a Map corresponding to the name of the city as the key) and when the new Address instance is created, use the detached City instance as the value of the City field in the new Address instance. When you do makePersistent on the Address, the detached City instance will be reconnected to the database.

Craig

On Jun 29, 2009, at 12:54 PM, LFS wrote:

> Hi
>
> This is my situation: I have an Address object that I
> persist/retrieve from the DB. It contains a City object, reperesenting
> the city on which that address is. City contains State, and State
> contains Country object (a long N-1 chain).
> When I'm creating a new
> address from user input, the user selected its city from a combo box
> which I populated from DB, then I'll have the city's ID.
> When I try
> to persist the new address, the most I can do is put a City object on
> it, containing only the city ID. The problem is that persisting this
> will erase the City record for that ID on the DB, because other fields
> like Name and State will be null. I want simply to set the foreign id
> on the Address object, nothing else.
>
> I believe there is some
> good solution for this... my intention is not updating the City object,
> and retrieving it to put it back on the address object before being
> persisted sounds very strange and a performance nightmare.
>
> What's the proper way to do this?
>
> Thanks!
>
> Note: I'm using application identity and DataNucleus JDO.
>
>
>      ____________________________________________________________________________________
> Veja quais são os assuntos do momento no Yahoo! +Buscados
> http://br.maisbuscados.yahoo.com

Craig L Russell
Architect, Sun Java Enterprise System http://db.apache.org/jdo
408 276-5638 mailto:Craig.Russell@...
P.S. A good JDO? O, Gasp!




      ____________________________________________________________________________________
Veja quais são os assuntos do momento no Yahoo! +Buscados
http://br.maisbuscados.yahoo.com