UoS
UoS

Reputation: 93

EclipseLink uses incorrect table name in queries for subclasses if inheritance type is SingleTable

I have three entities using JPA/EclipseLink:

@Entity(name = "Sharing")
@Inheritance(strategy = InheritanceType.SINGLE_TABLE)
public class AbstractSharingEntity extends AbstractEntity {

@Entity(name = "InternalSharing")
public class InternalSharingEntity extends AbstractSharingEntity {

@Entity(name = "ExternalSharing")
public class ExternalSharingEntity extends AbstractSharingEntity {

If I create a typed query for AbstractSharingEntity ("... FROM Sharing ..."), EclipseLink creates the correct query using the specified entity name.

But if I create a typed query for one of the two subclasses (e. g. "... FROM InternalSharing ..."), EclipseLink creates a wrong query using the class name as table name instead of using the entity name. This results in the following error:

java.sql.SQLSyntaxErrorException: Table 'db.ABSTRACTSHARINGENTITY' doesn't exist

Did I make any mistake and this is the expected behaviour? How can I create valid queries for the subclasses without changing the class names/removing different entity names?

Upvotes: 0

Views: 327

Answers (1)

Neil Stockton
Neil Stockton

Reputation: 11531

Your expectations are correct IMHO. The default table name is "entityName" when no table name is specified, and since you overrode the entity name of the "Abstract" class then the table used should be SHARING. And since it has that inheritance then should use that for the subclasses too.

All of those queries should be generating a query of the form

"SELECT ... FROM SHARING ..."

Raise a bug on your JPA provider.

NOTE: This assumes that AbstractEntity is not a JPA entity.

You would, of course, require a discriminator on that model, and also your "Abstract" class likely ought to be abstract

Upvotes: 0

Related Questions