Search code examples
mongodbmorphia

Mongo Morphia mapPackage doesn't map classes in package


I am using Morphia mapper for MongoDB/Java. I have successfully used the web application on GlassFish server. I am migrating my project to WildFly8.2.Final/JBoss. I am having issues with Morphia mapping packages. Morphia mapping/scanning packages doesnt work. It worked fine on GlassFish but doesnt work on WilfFly.


I thought that it was a classpath issue and did a small test. I experimented by individually mapping a class and it worked fine. Its just mapping a package doesn't work. I have the following code for Morphia.

Code :

public class MongoDataSource {
    private static final String IP = XXXXXX;
    private static final Integer PORT = XXXXXX;
    private static final String DB_NAME = XXXXXX;
    private static final String USERNAME = XXXXXX;
    private static final String PWD = XXXXXX;
    private static Morphia m;
    private static Datastore ds;
    private static DB db;
    private static MongoClient client;
    private static MongoDataSource INSTANCE = new MongoDataSource();

    private MongoDataSource() {        
        m = new Morphia();                
        m.mapPackage("xxxx.model.user");//Works on Glassfish but doesnt work on WildFly/JBoss

        m.map(xxxx.model.user.User.class);//My Experiment with loading a specific class in the package
        try {
            List<MongoCredential> credentials = new ArrayList<>();
            credentials.add(MongoCredential.createMongoCRCredential(USERNAME, DB_NAME, PWD.toCharArray()));
            ServerAddress servAddr = new ServerAddress(IP, PORT);
            client = new MongoClient(servAddr, credentials);
            db = client.getDB(DB_NAME);
            ds = m.createDatastore(client, DB_NAME);
        } catch (Exception e) {
            //Log
        }
    }

    public static Morphia getMorphia() {        
        return m;
    }

    public static Datastore getDatastore() {
       return ds;
    }

    public static DB getDataBase() throws Exception {
        return db;
    }
}

What I don't understand is, if the code was not able to find package, how is it able to find a class in a package. Is this is a bug in Morphia API or some classpath issue when running the application on WildFly/Jboss. I cannot convince myself that its a classpath issue.


Solution

  • There have been several bugs related with mapPackage in morphia. Two days ago, using the version 0.110 I have experienced the an error with that method and I added to a existing issue in their GitHub

    Check the related issues in GitHub with mapPackage and as a workaround you can just provide the classes directly using: morphia.map(ClassA.class, ClassB.class, ClassC.class);