Hi Josef:
I've done something very similar to what you describe in which several
objects (each of which implemented the interface "Loggable") all had
collections of "memo" objects (I called them LogEntry objects). The
LogEntries were stored in one table with both a foreign key column to a
Loggable primary key and an additional column which held a value which
mapped the LogEntry to the applicable "Loggable" type.
Similar to yourself, I used the <query-customizer> to map the
<collection-descriptor> of each Loggable object back to the correct
LogEntry records.
However, I had need to map the 1:1 relationship between a LogEntry and the
owning Loggable object. I accomplished this by using the OJB callback
interface PersistenceBrokerAware on LogEntry. Note that the
"afterLookup(...)" method can be used to materialize the corresponding
Loggable object like this:
public class LogEntry.java {
private int fObjectType; // holds the value from the column which
maps between the LogEntry and the type of its parent Loggable.
private int fObjectId; // holds the value of the primary key of
the Loggable Object.
private Loggable fLoggedObject;
// Property getters/setters here
public void afterLookup(PersistenceBroker broker)
throws PersistenceBrokerException{
try{
switch (fObjectType) {
case Plan.LOGGABLE_OBJECT_KEY:
fLoggedObject =
FisDaoFactory.getInstance().getPlanDao().getForId(new
Integer(fObjectId));
break;
case Population.LOGGABLE_OBJECT_KEY:
fLoggedObject =
FisDaoFactory.getInstance().getPopulationDao().getForId(fObjectId);
break;
case Task.LOGGABLE_OBJECT_KEY:
fLoggedObject =
FisDaoFactory.getInstance().getTaskDao().getForId(fObjectId);
break;
default:
throw new IllegalStateException("No logged object
identified.");
}
} catch(DataAccessObjectException e) {
throw new PersistenceBrokerException(e);
}
}
}
Jon French
Programmer
ECOS Development Team
[EMAIL PROTECTED]
970-226-9290
Armin Waibel <[EMAIL PROTECTED]>
07/27/2005 02:56 AM
Please respond to
"OJB Users List" <[email protected]>
To
OJB Users List <[email protected]>
cc
Subject
Re: Customizing 1:1 querys like customized collection queries | second
trail
Hi Josef,
think this is not possible, because OJB handle 1:1 references different
from 1:n. 1:1 references are identity based (the main object knows the
PK values of the referenced object), so there is no need to use a
QueryCustomizer.
The 1:1 reference point from named parts, driver... to Memo - correct?
In your Memo table an entry/row is only unique when the PK is composed
of the PK of the main table (driver, ...) and a String field ("driv",
...)? If this is true, you have to declare both fields as PK in the
class-descriptor of the Memo class and the 1:1 reference in e.g. Driver
class look like this
<reference-descriptor
name="memo"
class-ref="Memo"
proxy="false"
auto-retrieve="true"
auto-update="true"
auto-delete="false">
<foreignkey field-ref="fkMemoId"/>
<foreignkey field-ref="fkMemoString"/>
</reference-descriptor>
Both FK fields are needed in Driver,...
If I it get it wrong, could you describe more detailed with
class-descriptor examples?
regards,
Armin
[EMAIL PROTECTED] wrote:
> Hello OJB-Users,
> I haven't get any feedback on this email yet, so I try it once more.
> The problem is that the Informations from the Foreign key are
Primary-Key
> + hardcoded String.
> How can I add the hardcoded String to my 1:1 reference?
> For 1:n, it's no Problem, because I can add the String to the Query in
an
> QueryCustomizer
>
>
----------------------------------------------------------------------------------------------------------------------
>
> I have even started using OJB and so far all works fine :o) (1:1,
1:n...)
> I would be pleased when you can help me with my first Problem.
>
> I'm looking for an solution like 'Customizing collection queries'
> descriped in the Guide 'advanced O/R technique' for 1:1 references.
>
> A short example:
>
> I have multible tables (named parts, driver...) and for each entry of a
> table i can reference to ONE entry of a table named memo across the
> primary-key as foreign key in the memo-table.
> To have a unique key, I musst save a second value in the memo-table to
> know from which table the foreign-key comes. This value is a String and
is
> hardcoded in the software.
> For table parts i save = "part" in the table Memo field parentTable
> For table driver i save = "driv" in the table Memo field parentTable
>
> Now, when I'm loading the memo to an item of the parts-table, I want'
only
> load the memo where in table Memo parentTable="part"
>
> With a 1:n, thats no Problem, because I can work with the
query-customizer
> like this
> ################################
> <inverse-foreignkey field-ref="parentObjId"/>
> <query-customizer
> class="de.on_ergy.lakon.data.QueryCustomizerLakonImpl">
> <attribute
> attribute-name="parentTable"
> attribute-value="TEILSTAM"
> />
> </query-customizer>
> ################################
>
> There I can add the value "TEILSTAM" to the Query with the written
> QueryCustomizerLakonImpl
>
> But how can I realize this with 1:1 ??
>
> Thank you very very much for your help.
>
> Best Wishes
>
> Josef Wagner
> /Germany
---------------------------------------------------------------------
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]