Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support use of @Converter on String field #55

Closed
cyberquarks opened this issue Dec 26, 2020 · 3 comments
Closed

Support use of @Converter on String field #55

cyberquarks opened this issue Dec 26, 2020 · 3 comments
Milestone

Comments

@cyberquarks
Copy link

With these classes:

@PersistenceCapable
public class Hub implements Serializable {
    private String name;
    private List<Schedule> schedules;
}

And

@PersistenceCapable
@EmbeddedOnly
public class Schedule implements Serializable {
    private String day;
    @Convert(LocalTimeConverter.class)
    private String opens;
    @Convert(LocalTimeConverter.class)
    private String closes;
}

The converter does not work, the stored document should be a number/Long and not the String field value since there is a converter. The converter code can be found here: https://github.com/cyberquarks/test-jdo-converter/blob/master/src/main/java/mydomain/model/converter/LocalTimeConverter.java

Here's the test case: https://github.com/cyberquarks/test-jdo-converter
The issue affects the latest version of Datanucleus/Datanucleus JDO

@andyjefferson
Copy link
Member

andyjefferson commented Dec 26, 2020

Converting a String to a Long is frankly a strange thing to do, @Convert is for allowing otherwise non-persistable things to be persisted by converting to an acceptable type. Clearly it could be possible to support such a thing with effort but I personally won't be doing so. You could contribute this if it is important to you

FYI, to persist you would need to update https://github.com/datanucleus/datanucleus-mongodb/blob/master/src/main/java/org/datanucleus/store/mongodb/fieldmanager/StoreFieldManager.java#L169 and to retrieve you would need to update https://github.com/datanucleus/datanucleus-mongodb/blob/master/src/main/java/org/datanucleus/store/mongodb/fieldmanager/FetchFieldManager.java#L246 but that would be just for String fields. Then you have Long, Short, Integer, Double, etc.

The fields that are supported for converters are "Object" field types (except the primitive wrappers).

@andyjefferson andyjefferson changed the title @Converter not working @Converter not working when trying to convert a String to a Long Dec 26, 2020
@cyberquarks
Copy link
Author

cyberquarks commented Dec 26, 2020

@andyjefferson okay I will check the code, so at the surface now the problem is not with the core or JDO module but rather the mongodb database module?

Yes, in fact it is strange, but there's a concrete use-case for this which is to query local time ranges (not date)
So one could query for a LocalTime within a range of LocalTimes (hence opens, and closes field)

And do, something like:
SELECT FROM mydomain.Hub WHERE this.schedules.contains(sch) && (sch.day == :schDay && sch.opens <= :startTime && sch.closes >= :endTime)

@datanucleus datanucleus deleted a comment from cyberquarks Dec 27, 2020
@datanucleus datanucleus deleted a comment from cyberquarks Dec 27, 2020
@datanucleus datanucleus deleted a comment from cyberquarks Dec 27, 2020
@datanucleus datanucleus deleted a comment from cyberquarks Dec 27, 2020
@datanucleus datanucleus deleted a comment from cyberquarks Dec 27, 2020
@andyjefferson andyjefferson changed the title @Converter not working when trying to convert a String to a Long Support use of @Converter on String field Aug 26, 2021
@andyjefferson
Copy link
Member

See https://github.com/andyjefferson/mongodb-55 for updated testcase that strips out irrelevant code. Note that it could be simplified further and just have a class with a String field, rather than embedding a collection of some subtype with a String field!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants