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 Sy...@swisscom.com on 2002/12/10 10:13:11 UTC

Newbie question: associations

Hello everybody,

I'm new to this kind of mapping tool and I have a (maybe stupid) question:

I have read the Tutorial 3 (Mapping techniques) and I have seen that we can create 1:1, 1:n or m:n associations with OJB.

But ,in practice, What's happen when you retrieve a class (a relationnal table) which contains an association (for example 1:1) with another class (table)?
Are the data mapped automatically retrieved?
How do you do that?

And in the case where you store something?
I suppose that we could store an object in a class (a table) and this object could be mapped in another table (with an id)?


Thank you for any comments
Regards
Sylvain



-----Message d'origine-----
De: Kevin Viet [mailto:vietk@activia.net]
Date: vendredi, 29. novembre 2002 11:32
À: OJB Users List
Cc: Benoit Mahe
Objet: design 2


Hello all 

There's something in Ojb that I can understand in updating an existing
object

For broker API a call to : broker.save(obj); is only necessary to update
the db.

For ODMG Api there's no such method and updating an object is done by
using some setXXX() methods between transaction bounds:

tx.begin ()
tx.lock(customer, WRITE) 
customer.setName(name);
...
tx.commit();


I want to create an interface on top of OJB layer (for hiding ojb), if I
use the broker API it's easy to design a method to update an object in
the repository :
	public void update(Object object) throws DBException;
The object in arguments has already been modified by application code
and then application request that the object have to be updated in the
repository.

Using the same considerations, things getting harder if I want to use
the ODMG Api because of the transaction things.
In fact the update method can't be so generic because of the call of
particular model setXXX(), I will have to define an update method for
every model objects and pass the values to be modified to this method

public void udpateCustomer(Customer cust, String firstName, String
lastName, int age)
{
	tx.begin();
	cust.setFirstName(firstName);
	cust.setLastName(lastName);
	tx.commit();
}

Writing such methods can be painfull but not impossible ...

Anyone got a better idea to simplify my life ... ?
Thx


-- 
Kevin Viet <vi...@activia.net>
ActiVia Networks




--
To unsubscribe, e-mail:   <ma...@jakarta.apache.org>
For additional commands, e-mail: <ma...@jakarta.apache.org>


Re: Newbie question: associations

Posted by Thomas Mahler <th...@apache.org>.
Hi Sylvain,

Sylvain.Thevoz@swisscom.com wrote:
 > Hello everybody,
 >
 > I'm new to this kind of mapping tool and I have a (maybe stupid)
 > question:
 >
 > I have read the Tutorial 3 (Mapping techniques) and I have seen that
 > we can create 1:1, 1:n or m:n associations with OJB.
 >
 > But ,in practice, What's happen when you retrieve a class (a
 > relationnal table) which contains an association (for example 1:1)
 > with another class (table)? Are the data mapped automatically
 > retrieved?
 > How do you do that?

It depends! In tutorial3 there is also a section cascading operations 
that tells how to configure the exact behaviour.

there are several options possible:
1. fill reference attributes with fully loaded referenced objects 
(auto-retrieve="true")
2. fill reference attributes with proxy objects that defer the loading 
of the referenced objects until they are accessed in user code. 
(auto-retrieve="true" and proxy="true")
3. don't fill reference attributes (auto-retrieve="false"). In this case 
the PersistenceBroker can be used to explicitely load a reference 
attribute later.

It depends your application scenario which of these options is the best fit.

 > And in the case where you store something? I suppose that we could
 > store an object in a class (a table) and this object could be mapped
 > in another table (with an id)?

The storing process can be configured in the same way! you can use 
auto-update="true" to force OJB to store references, and use 
auto-update="false" to prevent cascading update or insert operations.

(You can also configure delete operations in the same way.)

cheers,
Thomas

 >
 > Thank you for any comments Regards Sylvain
 >
 >
 >
 > -----Message d'origine----- De: Kevin Viet [mailto:vietk@activia.net]
 >  Date: vendredi, 29. novembre 2002 11:32 À: OJB Users List Cc: Benoit
 > Mahe Objet: design 2
 >
 >
 > Hello all
 >
 > There's something in Ojb that I can understand in updating an
 > existing object
 >
 > For broker API a call to : broker.save(obj); is only necessary to
 > update the db.
 >
 > For ODMG Api there's no such method and updating an object is done by
 >  using some setXXX() methods between transaction bounds:
 >
 > tx.begin () tx.lock(customer, WRITE) customer.setName(name); ...
 > tx.commit();
 >
 >
 > I want to create an interface on top of OJB layer (for hiding ojb),
 > if I use the broker API it's easy to design a method to update an
 > object in the repository : public void update(Object object) throws
 > DBException; The object in arguments has already been modified by
 > application code and then application request that the object have to
 > be updated in the repository.
 >
 > Using the same considerations, things getting harder if I want to use
 >  the ODMG Api because of the transaction things. In fact the update
 > method can't be so generic because of the call of particular model
 > setXXX(), I will have to define an update method for every model
 > objects and pass the values to be modified to this method
 >
 > public void udpateCustomer(Customer cust, String firstName, String
 > lastName, int age) { tx.begin(); cust.setFirstName(firstName);
 > cust.setLastName(lastName); tx.commit(); }
 >
 > Writing such methods can be painfull but not impossible ...
 >
 > Anyone got a better idea to simplify my life ... ? Thx
 >
 >