Hello, folks; OpenJPA has impressed me so far for thinking in most cases
waaay ahead of the curve. I'm hoping that perhaps this use case is handled
natively.
I am mapping a 30-year-old relational schema by hand (since it has no
foreign key or primary key information).
One of the cases I've run across is a Document object (doc_table) that has a
logical @ManyToMany relationship with a GLAccount (gla_rec).
Now, obviously I know how to mark such a thing up in JPA. But here's the
wrinkle.
In the database, there is no join table. Instead, the doc_table has 4/5 of
what would be needed for a true foreign key back to the gla_rec table (a
gla_rec is identified by five primary keys). The fifth component is a
fiscal year, which in the old application the user always has in hand.
Using straight JPA, I have no choice: I will have to annotate all four
columns in the doc_table as being just simple old String fields, and then I
will just have to know internally that when I want to trace back the
relationship to a GLAccount, I need to run a query with an entity manager,
supplying the fiscal year to the WHERE clause. This is tedious and
annoying.
What I'd LIKE to do is somehow have it so that I can designate a @Transient
property (or maybe some other bit of information) on the Document class as
being that fifth component.
So, for example, I might notate things like this (this is all hypothetical):
@Transient
private int fiscalYear;
@ManyToOne
@JoinColumns({
@JoinColumn(name = "glapk1", /* ... and so on ... */),
/* ...and so on for the other three ... */
})
@some.annotation.here.AdditionalJoinInfo(field = "fiscalYear",
referencedColumnName = "fscl_yr")
The idea is that we augment the @JoinColumns information--or replace it, or
something--in such a way that the remaining property necessary to make this
a true @ManyToOne relationship is supplied by looking up its value in an
otherwise @Transient property. It has the effect of supplying a parameter
to a where clause automatically, which means that I could then simply have
an accessor called getGeneralLedgerAccount(), which, if the fiscalYear
property was set, would return me exactly one (or zero, I suppose). If the
fiscalYear property was NOT set, it would return me null. No queries to
write; just straight old relationship navigation.
I am seeing this all over the schemas and am wondering if this was some sort
of odd pattern that used to be followed back in the day. Anyhow, I was
given hope too by reading the javadocs of the Joinable class in OpenJPA,
which says, in part, "This allows us to support joins to only some of the
columns of a mapping...." This made me hope that maybe someone has already
thought of something similar.
Thanks,
Laird