Datanucleus/JDO, persisting and retrieving java.util.Set attribute
I have a attribute of type Set in my persistable class. I am persisting to a H2 database. The class persists correctly and I can view the data using the H2 console and see the Set attribute has been serialized for storage, that's OK until I try to retrieve the object, my attribute is always null. I have tried adding annotations to stop it being serialized and to add it to the default fetch group but with no success. I guess I don't really understand which ones I need to add to stop it being serialized. I don't actually care if it get serialized so long as I can reconstruct the Set and access it's members.
Can anyone point me to a simple example of a Set being persisted using Datanucleus with or without serialization and any special code I need to de-serialized the data.
EDIT: Here is some code, first the class I want to persist,
@PersistenceCapable
public class Track {
private Set<String> fts;
private String name="";
public void setName(String name) {
this.name = name;
}
public String getName() {
return name;
}
public void setFts(Set<String> fts) {
this.fts = fts;
}
public Set<String> getFts() {
return fts;
}
}
An extract from my orm file
<class name="Track" identity-type="datastore" table="JDO_TRACK">
<inheritance strategy="new-table"/>
<field name="name" primary-key="true">
<column jdbc-type="VARCHAR" name="name"/>
</field>
<field name="fts" default-fetch-group="true" persistence-modifier="persistent">
<collection element-type="java.lang.String" dependent-element="true" serialised-element="false"/>
<element column="fts"/>
</field>
</class>
and my method to query the datastore, I want get the Track objects with specific values in the fts attribute. This sample queries on one string but in real life I'll need to do it on more than one with an AND (&&) (NB This works nicely on GAE/J so for my local test environment I am usng Datanucleus 1.1.5)
pri开发者_如何学编程vate Track retrieveFTS(String criteria) {
PersistenceManagerFactory pmf = JDOHelper
.getPersistenceManagerFactory("datanucleus.properties");
PersistenceManager pm = pmf.getPersistenceManager();
Track res = null;
try {
System.out.println("Querying with criteria:" + criteria);
Query q = pm.newQuery(mseries.routes.gpx.Track.class);
q.setFilter("fts.toUpperCase() == criteria ");
q.declareParameters("String criteria");
q.setUnique(true);
q.setOrdering("name ascending");
res = (Track) q.execute(criteria.toUpperCase());
} finally {
pm.close();
}
return res;
}
and the exception that I receive
javax.jdo.JDOFatalUserException: Impossible to query a collection/map field ("mseries.routes.gpx.Track.fts") when it is serialised. Either change your query, or change the field to not be serialised. at org.datanucleus.jdo.NucleusJDOHelper.getJDOExceptionForNucleusException(NucleusJDOHelper.java:354)
If using in GAE/J environment then you'd best tag your question as that. If using outside of GAE/J then you have no reason to use such an ancient unsupported version of DataNucleus i.e use v3.0
If you don't want a Set to be Serialised then you'd be best advised to include <join/> since otherwise you aren't allowing it a join table, hence it has to serialise it
精彩评论