Re: [morphia] Bulk insert of JavaObjects

775 views
Skip to first unread message

Justin Lee

unread,
Feb 23, 2016, 9:19:53 AM2/23/16
to mor...@googlegroups.com

--------------------------------

name     : "Justin Lee", 
  title    : "Software Engineer",
  twitter  : "@evanchooly",
  web      : [ "mongodb.com", "antwerkz.com" ],
  location : "New York, NY" }

On Tue, Feb 23, 2016 at 8:27 AM, Andy Voigt <a.voi...@gmail.com> wrote:
Hello,

i'am trying to make a bulk insert with java objects. We are using the "@Version" and "@Id" annotation. Here is my code to it:

    public <E extends BasicEntity> void persist(List<E> entities) throws GeoJsonException {
        if (entities != null && !entities.isEmpty()) {
                String collectionName = entities.get(0).getClass().getAnnotation(Entity.class).value();
                BulkWriteOperation bulkWrite = mongoDatastore.getDB().getCollection(collectionName).initializeUnorderedBulkOperation();
                List<E> savedEntities = new ArrayList<>(entities.size());
                for (E ee : entities) {
                    DBObject dbObject = morphia.toDBObject(ee);
                    bulkWrite.insert(dbObject);
                    savedEntities.add((E) morphia.fromDBObject(ee.getClass(), dbObject));
                }
                try {
                    bulkWrite.execute();
                } catch (WriteConcernException e) {
                    handleWriteConcernExcpetion(e);
                }
                entities.clear(); // we have to do it "pass-by-reference-value" :(
                entities.addAll(savedEntities);
        }
    }

If I execute the code i got a "Modification" exception. However if we delete the "@Version" annotation everything themes to work. Is there a way to insert the Java Object directly (without using "morphia.toDBObject") 

Regards, Andy

--
You received this message because you are subscribed to the Google Groups "Morphia" group.
To unsubscribe from this group and stop receiving emails from it, send an email to morphia+u...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Andy Voigt

unread,
Feb 23, 2016, 10:32:04 AM2/23/16
to Morphia
Damn haven't seen it. Thanks :)

Justin Lee

unread,
Feb 23, 2016, 10:43:35 AM2/23/16
to mor...@googlegroups.com
Any time. 

--------------------------------

name     : "Justin Lee", 
  title    : "Software Engineer",
  twitter  : "@evanchooly",
  web      : [ "mongodb.com", "antwerkz.com" ],
  location : "New York, NY" }

On Tue, Feb 23, 2016 at 10:32 AM, Andy Voigt <a.voi...@gmail.com> wrote:
Damn haven't seen it. Thanks :)

--
Reply all
Reply to author
Forward
0 new messages