Note that there are some explanatory texts on larger screens.

plurals
  1. PODatanucleus JDO Mongodb - Child of abstract in map value not persisted
    text
    copied!<p>I am using Datanucleus/JDO to persist objects in a MongoDB DB. I try to persist an object containing a Map, which value type is an abstract class.</p> <p>When I try to persist an instance of that object, fields of the abstract class are persisted, but not those of the child class.</p> <p>Below is some code as an example.</p> <p><strong>Zoo.java</strong></p> <pre><code> @PersistenceCapable public class Zoo { @Persistent private String fieldZoo; @Persistent private Map&lt;String, Animal&gt; mapStringAnimal; // etc... basic constructor... } </code></pre> <p><strong>Animal.java</strong></p> <pre><code> @PersistenceCapable(embeddedOnly = "true") public abstract class Animal { @Persistent private String fieldAnimal; } </code></pre> <p><strong>Dog.java</strong></p> <pre><code> @PersistenceCapable(embeddedOnly = "true") public class Dog extends Animal { @Persistent private String fieldDog; } </code></pre> <p><strong>Test.java</strong></p> <pre><code> public static void main(String[] args) { Map&lt;String, Animal&gt; mapStringAnimal = new HashMap&lt;String, Animal&gt;(); Dog dog = new Dog("valueFieldAnimal", "valueFieldDog"); mapStringAnimal.put("dogKey", dog); Zoo zoo = new Zoo("valueFieldZoo", mapStringAnimal); Properties properties = new Properties(); properties.setProperty("javax.jdo.PersistenceManagerFactoryClass", "org.datanucleus.api.jdo.JDOPersistenceManagerFactory"); properties.setProperty("javax.jdo.option.ConnectionURL", "mongodb:/dbtest"); properties.setProperty("javax.jdo.option.Mapping", "mongodb"); properties.setProperty("datanucleus.autoCreateSchema", "true"); PersistenceManagerFactory pmf = JDOHelper.getPersistenceManagerFactory(properties); PersistenceManager pm = pmf.getPersistenceManager(); pm.makePersistent(zoo); pm.close(); } </code></pre> <p>And when I look at MongoDB:</p> <pre><code> &gt; db.Zoo.find().pretty(); { "_id" : ObjectId("50d2f5f7e4b0cae285990b2d"), "fieldZoo" : "valueFieldZoo", "mapStringAnimal" : [ { "key" : "dogKey", "value" : { "fieldAnimal" : "valueFieldAnimal" } } ] } </code></pre>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload