简体   繁体   中英

Mongo/Morphia StackOverflow error?

I have the following entity:

@Entity("platform_defs")
class PlatformDef {
    @Id
    Long id

    @Property("name")
    String name

    @Property("abbreviation")
    String abbreviation

    @Property("type")
    String type

    @Property("hg_repo")
    String hgRepo

    @Property("port")
    Long port

    @Property("status")
    String status
}

And the following Morphia code trying to persist an instance of that entity:

public void savePlatformDef(PlatformDef pDef) {
    try {
        AppConfig config = getConfig();
        String dbName = config.getMongo().getDatabaseName();
        Mongo mongo = new Mongo(config.getMongo().getHost(), config.getMongo().getPort());
        morphia = new Morphia();
        datastore = morphia.createDatastore(mongo, dbName);
        morphia.map(PlatformDef.class);
        datastore.save(pDef);
    } catch(Throwable t) {
        System.out.println(t.getMessage());
    }
}

When I run this I get:

java.lang.StackOverflowError

Here is the recursive list of calls being made:

at org.mongodb.morphia.mapping.EmbeddedMapper(toDBObject:40)
at org.mongodb.morphia.mapping.Mapper(writeMappedField:642)
at org.mongodb.morphia.mapping.Mapper(toDBObject:544)
at org.mongodb.morphia.mapping.Mapper(toDBObject:526)

This pattern repeats over and over again... What's wrong with my setup?

Probably too late to help you, but I ran into this issue as well. I can't tell from what you provided code wise, but most likely your using an anonymous inner class at some point in the construction of the platformDef you pass to your savePlatformDef method. Apparently morphia does not support anonymous inner classes, they are known to cause stack overflows just like this. See this bug for detail:

https://github.com/mongodb/morphia/issues/402

Hopefully this answer will help others like me in the future :)

  1. Use an ObjectId for your @Id
  2. You don't need to annotate attributes if you don't require a different name than the one from the variable
  3. Use MongoClient instead of Mongo , which is deprecated / already removed depending on your version of Mongo Java driver
  4. Why are you opening a new database connection for every save operation? This is pretty expensive and unnecessary, since the Java driver will automatically pool them for you

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM