0

我升级到 Spring 3.1.2、Neo4J 1.8.RC1 和 SpringData 2.1.0.RC3 并遇到以下问题。

在解释问题之前,这是我的应用程序上下文的一部分:

<!-- Neo4J -->
<neo4j:config storeDirectory="target/graph.db" entityManagerFactory="entityManagerFactory"/>
<neo4j:repositories base-package="***.repository"/>

<!-- H2 -->
<jdbc:embedded-database type="H2" id="accountDataSource">
    <jdbc:script location="classpath:scripts/schema.sql" separator=";"/>
</jdbc:embedded-database>


<bean id="entityManagerFactory" class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean">
    <property name="dataSource" ref="accountDataSource" />
    <property name="packagesToScan" value="***.domain" />
    <property name="jpaVendorAdapter">
        <bean class="org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter">
            <property name="generateDdl" value="false" />
            <property name="showSql" value="false" />
            <property name="databasePlatform" value="${database.dialect}" />
        </bean>
    </property>
</bean>

<!-- transactions -->
<bean class="org.springframework.orm.jpa.JpaTransactionManager" id="transactionManager">
    <property name="entityManagerFactory" ref="entityManagerFactory" />
</bean>

<tx:annotation-driven mode="aspectj" />

我运行了一个非常基本的测试:

@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration("classpath:devinlove-core.xml")
@Transactional
public class MascotRepositoryTest {

    @Autowired
    private MascotRepository mascotRepository;
    private Mascot mascot;

    @Before
    public void setup() {
        mascot = new Mascot();
        mascot.setName("Tux");
    }

    @Test
    public void when_inserting_then_retrievable() {
        assertThat(mascotRepository).isNotNull();
        mascotRepository.save(mascot);
        assertThat(mascot.getId()).isGreaterThan(0L);
    }
}

并且测试因 NPE 而失败。为什么保存操作后吉祥物ID仍然为空?

MascotRepository 定义如下:

public interface MascotRepository extends GraphRepository<Mascot> {}

Mascot 本身是一个非常基本的 NodeEntity:

@NodeEntity
@TypeAlias("mascot")
public class Mascot {
    @GraphId
    private Long id;

    @Indexed(indexType= FULLTEXT, indexName = "mascotSearch")
    private String name;

    public Long getId() {
        return id;
    }

    public String getName() {
        return name;
    }

    public void setName(final String name) {
        this.name = name;
    }

    @Override
    public String toString() {
        return "Mascot{" +
            "id=" + id +
            ", name='" + name + '\'' +
            '}';
    }
}

我不知道这是否与问题有关,但我的 NodeEntity 之一是部分的,因此被 AspectJ 修改(以添加基本的 ActiveRecord 方法)。您能否确认仅以这种方式修改了部分 NodeEntities 而不是纯 Neo4J 实体?如果不是,它会导致与存储库发生冲突吗?

如果您有任何想法,请提前致谢!罗尔夫

4

1 回答 1

0

我认为 transactionManager bean 的正确定义是这样的:

<bean id="jpaTransactionManager"
        class="org.springframework.orm.jpa.JpaTransactionManager">
    <property name="entityManagerFactory" ref="entityManagerFactory"/>
</bean>
<bean id="jtaTransactionManager"
        class="org.springframework.transaction.jta.JtaTransactionManager">
    <property name="transactionManager">
        <bean class="org.neo4j.kernel.impl.transaction.SpringTransactionManager">
            <constructor-arg ref="graphDatabaseService" />
        </bean>
    </property>
    <property name="userTransaction">
        <bean  class="org.neo4j.kernel.impl.transaction.UserTransactionImpl">
            <constructor-arg ref="graphDatabaseService" />
        </bean>
    </property>
</bean>
<bean id="transactionManager"
        class="org.springframework.data.neo4j.transaction.ChainedTransactionManager">
    <constructor-arg>
        <list>
            <ref bean="jpaTransactionManager"/>
            <ref bean="jtaTransactionManager"/>
        </list>
    </constructor-arg>
</bean>

<tx:annotation-driven mode="aspectj" transaction-manager="transactionManager"/>
于 2012-09-27T13:02:59.600 回答