Eclipse Community Forums
Forum Search:

Search      Help    Register    Login    Home
Home » Eclipse Projects » EclipseLink » JPQL passing binary enum instead of enum's ordinal as SQL param value for key in Map<enum, String
JPQL passing binary enum instead of enum's ordinal as SQL param value for key in Map<enum, String [message #1396813] Tue, 08 July 2014 20:42
Ross Goldberg is currently offline Ross GoldbergFriend
Messages: 7
Registered: July 2014
Junior Member
I am using my Provider enum as the key in a JPA 2.1 Map<Provider, String> field in my Schedule @Entity that uses field access.

I am using EclipseLink in GlassFish 4.0. I have tried both EclipseLink 2.5.0 (built-in to GlassFish) & 2.5.2 (the latest EclipseLink release)*.

For some reason, for the following JPQL, while EclipseLink generates the correct SQL, it passes the :p Provider parameter as a binary Provider enum instance, rather than as the Provider#ordinal() int that I had expected from having specified @MapKeyEnumerated.

select
    s
from
    Schedule s
join
    s.externalIdByProvider px
where
    key(px) = :p and
    value(px) = :xid


I've verified from the mysql logs that :p is being passed as a binary Provider enum.

While debugging, I also found the line in EclispeLink 2.5.2 where PreparedStatement#setObject() is called with the value arg (the 2nd arg to the method) being the Provider set as the Query's :p param, rather than using the .ordinal() from :p (it's line 2441 of DatabasePlatform, at the end of setParameterValueInDatabaseCall()).

From what I know, @MapKeyEnumerated is supposed to default to @MapKeyEnumerated(ORDINAL), and using the more verbose form didn't make a difference.

From the Java EE 7 JavaDocs, it even looks like @MapKeyEnumerated should be unnecessary, because the docs imply that JPA should figure out that the key is an enum, and implicitly behave as if a @MapKeyEnumerated(ORDINAL) was specified (it looks like @MapKeyEnumerated is only needed if you want to use the .name() of the enum by specifying @MapKeyEnumerated(STRING), but I could be wrong).

Are my JPA annotations incorrect?

Is my JQPL incorrect?

Is there a bug (known or unknown) in EclipseLink?

If it's a bug, is there any workaround other than using SQL in a NativeQuery?

Can I somehow access the :p Provider's ordinal in JPQL?

FYI: While there is a Provider table in mysql, the Provider enum is not an @Entity; the table & the enum are manually synchronized since new Providers are incredibly rare.

FYI: The Provider enum has worked perfectly fine as a normal, non-collection-type @Enumerated JPA field, both in Schedule & in other @Entitys, with the .ordinal() being correctly used as the parameter value for JPQL with @Enumerated Provider JPA fields.

Code:

public enum Provider {
    ...
}

@Entity
@Table(name = "Schedule")
public class Schedule {

    @ElementCollection(fetch = EAGER)
    @MapKeyColumn(name = "provider", nullable = false)
    @MapKeyEnumerated
    @Column(name = "externalId", nullable = false, length = 45)
    @CollectionTable(
        name = "ScheduleProviderExternalId",
        joinColumns = @JoinColumn(name = "schedule", nullable = false)
    )
    @JoinFetch(OUTER)
    private Map<Provider, String> externalIdByProvider = new EnumMap<>(Provider.class);

    ...
}


mysql schema:

create table Provider (
  id int not null auto_increment,
  name varchar(45) not null,
  primary key (id)
);

create table Schedule (
  id bigint not null auto_increment,
  /*...*/
  primary key (id)
);

create table ScheduleProviderExternalId (
    provider int not null,
    externalId varchar(45) not null,
    schedule bigint unsigned not null,
    primary key (provider, externalId),
    unique (schedule, provider),
    foreign key (schedule) references Schedule (id),
    foreign key (provider) references Provider (id)
);




*: To use EclipseLink 2.5.2 in GlassFish 4.0, I: undeployed my application, then stopped GlassFish, then replaced the built-in 2.5.0 OSGi jars with the 2.5.2 OSGi jars, then deleted the OSGi cache, then redeployed my application, as per instructions online).

[Updated on: Wed, 09 July 2014 15:38]

Report message to a moderator

Previous Topic:Tricky custom ClassLoader problem
Next Topic:SecondaryTable causes invalid join (Bug)
Goto Forum:
  


Current Time: Fri Feb 27 13:14:09 GMT 2015

Powered by FUDForum. Page generated in 0.01542 seconds
.:: Contact :: Home ::.

Powered by: FUDforum 3.0.2.
Copyright ©2001-2010 FUDforum Bulletin Board Software