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

Spring data mongo crossstore can not work when I use the latest Spring and Spring Dat

$
0
0
Hi Springer,

I am trying to write a simple example to experience the crossstore between Mongo and JPA, but it does not work.

Spring 3.2.2
Spring Data Mongo 1.2.1
Spring Data JPA 1.3.2

Source codes are hosted on my github: https://github.com/hantsy/conference...ata-crossstore

JPA Configuration:

Code:

<jdbc:embedded-database id="dataSource">
                <!-- <jdbc:script location="classpath:/schema.sql" /> -->
        </jdbc:embedded-database>

        <bean
                class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean"
                id="entityManagerFactory">
                <property name="persistenceUnitName" value="persistenceUnit" />
                <property name="dataSource" ref="dataSource" />
                <property name="persistenceProviderClass" value="org.hibernate.ejb.HibernatePersistence"></property>
                <property name="packagesToScan">
                        <array>
                                <value>com.hantsylabs.example.conference.model</value>
                        </array>
                </property>
                <property name="jpaProperties">
                        <value>
                                hibernate.format_sql=true
                                hibernate.show_sql=true
                                hibernate.hbm2ddl.auto=create
                        </value>
                </property>
        </bean>

        <bean class="org.springframework.orm.jpa.JpaTransactionManager"
                id="transactionManager">
                <property name="entityManagerFactory" ref="entityManagerFactory" />
        </bean>
       
        <jpa:repositories base-package="com.hantsylabs.example.conference.jpa"></jpa:repositories>
        <context:component-scan base-package="com.hantsylabs.example.conference"></context:component-scan>

        <!-- Adds transaparent exception translation to the DAOs -->
        <bean
                class="org.springframework.dao.annotation.PersistenceExceptionTranslationPostProcessor" />

Mongo Configration.

Code:

<mongo:repositories base-package="com.hantsylabs.example.conference.mongo" />

        <!-- Mongo config -->
        <mongo:db-factory id="mongoDbFactory" host="localhost"
                port="27017" dbname="conference-db" />

        <bean id="mongoTemplate" class="org.springframework.data.mongodb.core.MongoTemplate">
                <constructor-arg ref="mongoDbFactory" />
        </bean>

        <bean class="org.springframework.data.mongodb.core.MongoExceptionTranslator" />

        <bean
                class="org.springframework.data.mongodb.core.mapping.event.LoggingEventListener" />

       
        <!-- Mongo cross-store aspect config -->
        <bean
                class="org.springframework.data.mongodb.crossstore.MongoDocumentBacking"
                factory-method="aspectOf">
                <property name="changeSetPersister" ref="mongoChangeSetPersister" />
        </bean>
       
        <bean id="mongoChangeSetPersister"
                class="org.springframework.data.mongodb.crossstore.MongoChangeSetPersister">
                <property name="mongoTemplate" ref="mongoTemplate" />
                <property name="entityManagerFactory" ref="entityManagerFactory" />
        </bean>

Ascpectj maven plugin are also configed in parent pom.xml.
Code:

<plugin>
                                        <groupId>org.codehaus.mojo</groupId>
                                        <artifactId>aspectj-maven-plugin</artifactId>
                                        <version>1.2</version>
                                        <!-- NB: do not use 1.3 or 1.3.x due to MASPECTJ-90 and do not use 1.4
                                                due to declare parents issue -->
                                        <dependencies>
                                                <!-- NB: You must use Maven 2.0.9 or above or these are ignored (see
                                                        MNG-2972) -->
                                                <dependency>
                                                        <groupId>org.aspectj</groupId>
                                                        <artifactId>aspectjrt</artifactId>
                                                        <version>${aspectj.version}</version>
                                                </dependency>
                                                <dependency>
                                                        <groupId>org.aspectj</groupId>
                                                        <artifactId>aspectjtools</artifactId>
                                                        <version>${aspectj.version}</version>
                                                </dependency>
                                        </dependencies>
                                        <executions>
                                                <execution>
                                                        <goals>
                                                                <goal>compile</goal>
                                                                <goal>test-compile</goal>
                                                        </goals>
                                                </execution>
                                        </executions>
                                        <configuration>
                                                <outxml>true</outxml>
                                                <aspectLibraries>
                                                        <aspectLibrary>
                                                                <groupId>org.springframework</groupId>
                                                                <artifactId>spring-aspects</artifactId>
                                                        </aspectLibrary>
                                                        <aspectLibrary>
                                                                <groupId>org.springframework.data</groupId>
                                                                <artifactId>spring-data-mongodb-cross-store</artifactId>
                                                        </aspectLibrary>
                                                </aspectLibraries>
                                                <source>${java.version}</source>
                                                <target>${java.version}</target>
                                        </configuration>
                                </plugin>

4. Entity class, Conference is JPA entity, and Contact and Signup are Mongo @Document entity
Code:

@Entity
public class Conference {
        @Id
        @GeneratedValue(strategy = GenerationType.AUTO)
        @Column(name = "id")
        private Long id;

        @Version
        @Column(name = "version")
        private Integer version;

        @RelatedDocument
        //@Transient
        private Contact contact;

        @RelatedDocument
        //@Transient
        private Set<Signup> signups = new HashSet<Signup>();


Test codes
Code:

@Test
        @Transactional(propagation = Propagation.REQUIRED)
        public void retrieveConference() {
                log.debug("==================enter retrieveConference=========================");
                Conference conference = newConference();
                conference.setSlug("test-jud");
                conference.setName("Test JUD");
                Signup signup1 = newSignup();
                Signup signup2 = newSignup();

                signup2.setEmail("testanother@tom.com");

                conference.addSignup(signup1);
                conference.addSignup(signup2);
                conference.setContact(new Contact("Hantsy"));
                conference = conferenceRepository.save(conference);
                em.flush();

                Long id = conference.getId();

                em.clear();

                assertTrue(null != id);

                Conference conf = conferenceRepository.findOne(id);

                log.debug("conf@@@" + conf);
                assertTrue(null != conf);

                // log.debug("contact id @@@" + conf.getContact().getId());
                // assertTrue(null!=conf.getContact().getId());

                /**
                * Id is not assigned by Spring data mongo....why@
                */
                assertTrue("Hantsy".equals(conf.getContact().getName()));

                log.debug("signup size @" + conf.getSignups().size());
                assertTrue(2 == conf.getSignups().size());
        }

The test codes can not be passed in IDE, I can not see any Mongo CRUD operations.

Viewing all articles
Browse latest Browse all 128

Trending Articles