/** * Creates a Morphia instance with the given Mapper and class set. * * @param mapper the Mapper to use * @param classesToMap the classes to map */ public Morphia(final Mapper mapper, final Set<Class> classesToMap) { this.mapper = mapper; for (final Class c : classesToMap) { map(c); } }
Morphia morphia = new Morphia(); morphia.map(Person.class); /* You can reuse this datastore */ Datastore datastore = morphia.createDatastore(mongoClient, "myDatabase"); /* * Jackson's ObjectMapper, which is reusable, too, * does all the magic. */ ObjectMapper mapper = new ObjectMapper();
import com.mongodb.MongoClient; import org.mongodb.morphia.Datastore; import org.mongodb.morphia.DatastoreImpl; import org.mongodb.morphia.Morphia; import java.net.UnknownHostException; ..... private Datastore createDataStore() throws UnknownHostException { MongoClient client = new MongoClient("localhost", 27017); // create morphia and map classes Morphia morphia = new Morphia(); morphia.map(FooBar.class); return new DatastoreImpl(morphia, client, "testmongo"); } ...... //with the Datastore from above you can save any mapped class to mongo Datastore datastore; final FooBar fb = new FooBar("hello", "world"); datastore.save(fb);
MongoClient mongoClient = new MongoClient(new MongoClientURI("mongodb://localhost")); Morphia morphia = new Morphia(); morphia.map(LookupData.class); //lookupdata collection is under my local db "tutorials" in this case Datastore datastore = morphia.createDatastore(mongoClient, "tutorials"); Map<String,ArrayList> categotyLookUpMap = new HashMap<String,ArrayList>(); LookupData lookupData = datastore.find(LookupData.class).get(); categotyLookUpMap.put(lookupData.getKey(), lookupData.getValue());
@Configuration public class MorphiaAutoConfiguration { @Autowired private MongoClient mongoClient; // created from MongoAutoConfiguration @Bean public Datastore datastore() { Morphia morphia = new Morphia(); // map entities, there is maybe a better way to find and map all entities ClassPathScanningCandidateComponentProvider entityScanner = new ClassPathScanningCandidateComponentProvider(true); entityScanner.addIncludeFilter(new AnnotationTypeFilter(Entity.class)); for (BeanDefinition candidate : scanner.findCandidateComponents("your.basepackage")) { // from properties? morphia.map(Class.forName(candidate.getBeanClassName())); } return morphia.createDatastore(mongoClient, "dataStoreInstanceId"); // "dataStoreInstanceId" may come from properties? } }
try { debug("mapping class: %1$s", clz.getName()); morphia_.map(clz); } catch (ConstraintViolationException e) { throw new RuntimeException(e);
/** * Tries to map all classes in the package specified. * * @param packageName the name of the package to process * @param ignoreInvalidClasses specifies whether to ignore classes in the package that cannot be mapped * @return the Morphia instance */ public synchronized Morphia mapPackage(final String packageName, final boolean ignoreInvalidClasses) { try { for (final Class clazz : ReflectionUtils.getClasses(packageName, mapper.getOptions().isMapSubPackages())) { try { final Embedded embeddedAnn = ReflectionUtils.getClassEmbeddedAnnotation(clazz); final Entity entityAnn = ReflectionUtils.getClassEntityAnnotation(clazz); final boolean isAbstract = Modifier.isAbstract(clazz.getModifiers()); if ((entityAnn != null || embeddedAnn != null) && !isAbstract) { map(clazz); } } catch (final MappingException ex) { if (!ignoreInvalidClasses) { throw ex; } } } return this; } catch (IOException e) { throw new MappingException("Could not get map classes from package " + packageName, e); } catch (ClassNotFoundException e) { throw new MappingException("Could not get map classes from package " + packageName, e); } }
public class Temp { @Id String _id; @Embedded // CHANGE HERE List<MapProxy> strings; // CHANGE HERE public Temp(){ strings=new LinkedList<MapProxy>(); // CHANGE HERE } public static void main(String...args) throws UnknownHostException, MongoException{ Mongo mongo=null; Morphia morphia=null; Datastore ds=null; mongo = new Mongo(); morphia = new Morphia(); morphia.map(Temp.class); ds = morphia.createDatastore(mongo, "test2"); Temp t = new Temp(); t._id ="hi"; MapProxy mp = new MapProxy(); // CHANGE HERE mp.m.put("Hi","1"); // CHANGE HERE mp.m.put("Hi2",2); // CHANGE HERE t.strings.add(mp); // CHANGE HERE ds.save(t); t=ds.get(t); ds.ensureIndexes(); } }