Hi,
not sure that I understand the problem.
Each User has two Address attributes ('home' and 'work'), both are of the same type 'Address' (both 1:1 references), you don't use extension classes AddressHome and AddressWork objects. Address use a composite PK ('login' and 'type').
In this case each reference has to use a composite FK key too.
in User (assume the 'login' datatype is INTEGER and User use an autoincrement PK, only show mapping for the 'home' reference, 'work' will be the same):
You need a FK field for each PK value of the referenced object, if you use 'anonymous' fields, you don't need a "real" fields in your persistent object class (User) - more info see docs.
http://db.apache.org/ojb/docu/guides/advanced-technique.html#How+do+
<field-descriptor
name="fkLoginHome"
column="FK_LOGIN_HOME"
jdbc-type="INTEGER"
access="anonymous"
/>
<field-descriptor
name="fkTypeHome"
column="FK_TYPE_HOME"
jdbc-type="VARCHAR"
access="anonymous"
/><reference-descriptor name="homeAddress" class-ref="Address" auto-retrieve="true" auto-update="true" auto-delete="true"> <foreignkey field-ref="fkLoginHome" /> <foreignkey field-ref="fkTypeHome" /> </reference-descriptor>
In Address you use the composite PK and both attributes has to be set by hand (no PK auto-generation was used) and the 'login' field is a FK from User table. This will make things complicated, because on insert OJB will first store the 1:1 references, but in your case we need the 'login' PK first. So we need a workaround when insert User objects:
broker.beginTransaction();
User user = new User();
broker.store(user);
// now the PK ('login' field in User) is specified
Address home = new Address();
home.setLogin(user.getLogin());
home.setType("home");
user.setHome(home);
//
broker.store(user);
broker.commitTransaction();Things will be easier when you use an single autoincremented PK field in Address. In this case all will be handled by OJB and you only need single FK field-descriptor in User:
broker.beginTransaction();
User user = new User();
Address home = new Address();
home.setType("home");
user.setHome(home);
broker.store(user);
broker.commitTransaction();regards, Armin
G�nther Wieser wrote:
hi,
as far as i understand this, you try to differentiate by the value of "adress.type" whether an adress is a home wor work adress? well, i have no solution for that, but i tried it here with a similair data structur, and it didn't work either. to be honest i'm not sure if this is the intended use for <attribute> in <collection-descriptor> or <reference-descriptor>, because i didn't find anything in the docs. all i found was an example in the ojb test cases in repository_junit.xml, but the corresponding source code didn't tell me anything i could understand.
so, long speach, short meaning: i'm also interested in a solution for that!
kr, guenther
-----Original Message-----
From: Georg M�ller [mailto:[EMAIL PROTECTED] Sent: Wednesday, March 02, 2005 9:24 PM
To: [email protected]
Subject: Mapping question
Hi,
I have an object "User" who has 2 addresses, home and work.
User is mapped to a table user (no problem here).
Both home and work address are mapped to a table address. This table has a composite primary key: login and type - login from table user as a foreign key - type is a string (or enum) "home" and "work"
I have tried the following without success:
in User description:
<reference-descriptor name="homeAddress" class-ref="Address" auto-update="true" auto-delete="true"> <attribute attribute-name="type" attribute-value="heimat"/> <foreignkey field-ref="login" /> </reference-descriptor>
and in Address description:
<field-descriptor name="login" column="login" primarykey="true" />
<field-descriptor name="type" column="type" primarykey="true" />
Is it possible to make it run anyhow?
Regards, Georg
P.S.: Please CC me
--------------------------------------------------------------------- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED]
--------------------------------------------------------------------- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED]
--------------------------------------------------------------------- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED]
