Converter not working with datanucleus-mongodb


Andy
 

Use of JDO converters to convert a String to a Long is not supported, and is minority interest. Get the code if you require such things


qquantum@...
 

Here's a bug report: https://github.com/datanucleus/datanucleus-mongodb/issues/55


qquantum@...
 

Adding 

```
@PersistenceCapable
@EmbeddedOnly
public class Availability {
  private String day;
  @Convert(LocalTimeConverter.class)
  private String opens;
  @Convert(LocalTimeConverter.class)
  private String closes;
}
```
Solved one part of the problem that is it now stored as a JSON document (embedded) but the converter still fails to be called. 


Andy
 

Looks totally normal to me since you selected to have Availability stored as its own type, hence it has a reference to the object "id" that it relates to.
Obviously you could store them as embedded if you wanted (see the docs).
Or if you expected to see a "DBRef" stored then you can contribute to https://github.com/datanucleus/datanucleus-mongodb/issues/6


qquantum@...
 

When persisting this model with datanucleus-mongodb 

@PersistenceCapable
public class Room implements Serializable {
  @PrimaryKey
  @Persistent(valueStrategy= IdGeneratorStrategy.IDENTITY)
  private String uid;
  private List<Availability> availabilities;
}


This Room model is stored as this document:

{

  "_id": {

    "$oid": "5fe619bde06a02dd8d35a93d"

  },

  "roomName": "Sample Room Name",

  "isPublished": false,

  "availabilities": [

    "5fe619bde06a02dd8d35a93e[OID]com.mycompany.models.Availability",

    "5fe619bde06a02dd8d35a93f[OID]com.mycompany.models.Availability"

  ]

}

Is it normal that the array items is stored like this? It does not look like a normal MongoDB document. 

The Availability model:

@PersistenceCapable
public class Availability implements Serializable {

  private String day;
  @Convert(LocalTimeConverter.class)
  private String opens;
  @Convert(LocalTimeConverter.class)
  private String closes;
}

And the Availability as such: 

{

  "_id": {

    "$oid": "5fe619bde06a02dd8d35a93e"

  },

  "opens": "07:00",

  "day": "SUNDAY",

  "closes": "11:00"

}

Although having a converter:

public class LocalTimeConverter implements AttributeConverter<String, Long> {
@Override
public Long convertToDatastore(String value) {
LocalTime localTime = LocalTime.parse(value);
return localTime.getLong(ChronoField.NANO_OF_DAY);
}

@Override
public String convertToAttribute(Long value) {
LocalTime localTime = LocalTime.ofNanoOfDay(value);
return localTime.toString();
}
}

I've tried debugging this, and from what I find, the converter is not even called despite annotated properly.