Quantcast
Channel: Spring Community Forums - NoSQL
Viewing all articles
Browse latest Browse all 128

Can not query the database when @TypeAlias is in use

$
0
0
Hi,

I'm using spring-mongo-data 1.2.1, spring 3.2.2,spring-data-commons 1.5.1, mongo-java-driver 2.10.1.

I have a wierd use case.

I have two entities
AImpl:

Code:

@Document(collection = "A2")
@TypeAlias("A")
public class AImpl {

        @Field
        public String name;

        @Reference
        public BInterface b;

        public AImpl(String name, BInterface b) {
                this.name = name;
                this.b = b;
        }
}

and BImpl :
Code:

@Document
@TypeAlias("B")
public class BImpl implements BInterface {

        String name;

        public BImpl(String name) {
                this.name = name;
        }

        @Override
        public String getName() {
                return name;
        }
}

Notice that the b member on AImpl is of type interface that BImpl is implementing.

I have an 'AImpl' record on my mongo instance .
you can creat on by executing:
Code:

db.A.insert({"_class" : "A", "name" : "805f8", "b" : { "_class" : "B", "name" : "805f8" }})
When I'm quering the db:
Code:

List<AImpl> lst = mongoOperations.findAll(AImpl.class);
from a test, the mongo type mapper, gets in action and everything goes smoothly.

But(!!!) when I'm performing the same query from an init method of one of the beans, it fails with this:

Code:

Caused by: org.springframework.beans.BeanInstantiationException: Could not instantiate bean class [com.shunra.mongodb_typemapping.BInterface]: Specified class is an interface
        at org.springframework.beans.BeanUtils.instantiateClass(BeanUtils.java:102)
        at org.springframework.data.convert.ReflectionEntityInstantiator.createInstance(ReflectionEntityInstantiator.java:61)
        ... 64 more

Here is the mongo related beans configuration:

Code:

        <mongo:mapping-converter id="mappingMongoConverter" base-package="com.shunra" />

        <mongo:mongo host="${mongo.host}" port="${mongo.port}" write-concern="JOURNAL_SAFE">
            <mongo:options
                                    connections-per-host="${mongo.connectionsPerHost}"
                                        threads-allowed-to-block-for-connection-  multiplier="${mongo.threadsAllowedToBlockForConnectionMultiplier}"
                                    connect-timeout="${mongo.connectTimeout}"
                                    max-wait-time="${mongo.maxWaitTime}"
                                    auto-connect-retry="${mongo.autoConnectRetry}"
                                    socket-keep-alive="${mongo.socketKeepAlive}"
                                    socket-timeout="${mongo.socketTimeout}"
                                    slave-ok="${mongo.slaveOk}"
                                    write-number="${mongo.write.number}"
                                    write-timeout="${mongo.write.timeout}"
                                    write-fsync="${mongo.write.fsync}"/>
        </mongo:mongo>
       
        <bean id="mongoTemplate" class="org.springframework.data.mongodb.core.MongoTemplate" >
          <constructor-arg name="mongoDbFactory" ref="mongoDbFactory"/>
          <constructor-arg name="mongoConverter" ref="mappingMongoConverter"/>
        </bean>

Code:

@Configuration
public class ContextConfiguration {
        Log log = LogFactory.getLog(ContextConfiguration.class);
        public static final String dbName = "cae68";// UUID.randomUUID().toString().substring(0, 5);

        @Bean
        @Autowired
        public MongoDbFactory mongoDbFactory() throws Exception {
                SimpleMongoDbFactory retVal = new SimpleMongoDbFactory(new Mongo(), dbName);
                retVal.setWriteConcern(WriteConcern.ACKNOWLEDGED);
                return retVal;
        }

        @Bean(initMethod = "init")
        MyBean myBean() {
                return new MyBean();
        }

}

In addition I pushed the entire maven project to GitHub so that you can pull/review the sources.

Thanks in advance.

Viewing all articles
Browse latest Browse all 128

Trending Articles