I have trouble with a polymorphic unidirectional relation with single tabled
inheritance and believe it is a bug but I am not quite sure as I not yet
have that much experiences from JPA programming. I use OpenJPA
2.1.0-SNAPSHOT but have also tried 2.1.1-SNAPSHOT and i have a class Depot
which have a unidirectional one to many relation to transactions that
implements DepotTransactions where the class AbstractDepotTransaction is an
entity and superclass with subclasses that implements different type of
transaction types. This is how the relation is defined in the Depot class:

    @OneToMany(fetch = FetchType.EAGER, cascade = CascadeType.PERSIST)
    @JoinColumn(name = "depotId")
    public List<AbstractDepotTransaction> transactionsList;

Persist of concrete transactions through this list works fine but when the
Depot instances are queried is the returned list always empty. The problem
seems to be that the query actually executed always queries for transaction
types of a specific type and as there is no instances for the class
AbstractDepotTransaction, only for its subclasses, will never anyone be
found:

SELECT t0.id, t1.id, t1.TX_TYPE, t1.txDate, t1.note, t1.amount, t1.courtage,
t1.vpcFee, t1.foreignTax, t1.tax, t1.quantity, t1.securityName FROM Depot t0
INNER JOIN DepotTransaction t1 ON t0.id = t1.depotId WHERE t1.TX_TYPE = ?
ORDER BY t0.id ASC [params=?]

The part "WHERE t1.TX_TYPE = ?" is what seems to cause the problem. The
TX_TYPE is the discriminator column. Compare this to the query executed for
a polymorphic query:

select t from AbstractDepotTransaction t order by t.id

executing prepstmnt 2060417743 SELECT t0.id, t0.TX_TYPE, t0.txDate, t0.note,
t1.id, t1.hasLiquidAccount, t1.name, t0.amount, t0.courtage, t0.vpcFee,
t0.foreignTax, t0.tax, t0.quantity, t0.securityName FROM DepotTransaction t0
LEFT OUTER JOIN Depot t1 ON t0.depotId = t1.id WHERE t0.TX_TYPE IN (?, ?, ?,
?, ?, ?, ?, ?) ORDER BY t0.id ASC [params=?, ?, ?, ?, ?, ?, ?, ?]

The part "t0.TX_TYPE IN (?, ?, ?, ?, ?, ?, ?, ?)" above shows why the
polymorphic query works and this is what the query executed for the
unidirectional relation also should look like.

I believe this is a bug. Does anyone agree in that? Or have anyone got
something like this to work before?

A lot of thanks for any comments to this!


-----
Effective Java 2nd ed. by Josh Bloch:

"Think in terms of exported APIs because it tends to
improve the quality of the software you write"
-- 
View this message in context: 
http://openjpa.208410.n2.nabble.com/BUG-Unidirectional-one-to-many-polymorphic-relation-not-works-tp6036782p6036782.html
Sent from the OpenJPA Users mailing list archive at Nabble.com.

Reply via email to