1
votes

I ran the getting started guide (http://www.axonframework.org/axon-2-quickstart-guide/) and it works fine.

I tried to replace the FileSystemEventStore with JpaEventStore, but it doesn't store any event.

enter image description here

Here is my configuration:

public static void main(String[] args) {
    CommandBus commandBus = new SimpleCommandBus();
    CommandGateway commandGateway = new DefaultCommandGateway(commandBus);
    //EventStore eventStore = new FileSystemEventStore(new SimpleEventFileResolver(new File("./events")));

    EntityManagerFactory entityManagerFactory = Persistence.createEntityManagerFactory( "cqrsworkshop" );
    EntityManager entityManager = entityManagerFactory.createEntityManager();
    SimpleEntityManagerProvider entityManagerProvider = new SimpleEntityManagerProvider(entityManager);
    EventStore eventStore = new JpaEventStore(entityManagerProvider);

    EventBus eventBus = new SimpleEventBus();
    EventSourcingRepository<ToDoItem> repository = new EventSourcingRepository<ToDoItem>(ToDoItem.class, eventStore);

    repository.setEventBus(eventBus);

    AggregateAnnotationCommandHandler.subscribe(ToDoItem.class, repository, commandBus);
    AnnotationEventListenerAdapter.subscribe(new ToDoEventHandler(), eventBus);

    final String itemId = UUID.randomUUID().toString();
    commandGateway.send(new CreateToDoItemCommand(itemId, "Need to do this"));
    commandGateway.send(new MarkCompletedCommand(itemId));

    entityManagerFactory.close();
}

   <persistence-unit name="cqrsworkshop">
            <provider>org.hibernate.ejb.HibernatePersistence</provider>
            <!-- JPA Event Store -->
            <class>org.axonframework.eventstore.jpa.DomainEventEntry</class>
            <class>org.axonframework.eventstore.jpa.SnapshotEventEntry</class>

            <properties>

                <property name="javax.persistence.jdbc.driver" value="org.h2.Driver" />

                <property name="javax.persistence.jdbc.url" value="jdbc:h2:~/test" />
                <property name="javax.persistence.jdbc.user" value="sa" />
                <property name="javax.persistence.jdbc.password" value="" />

                <property name="hibernate.dialect" value="org.hibernate.dialect.H2Dialect " />
                <property name="hibernate.show_sql" value="true" />
                <property name="hibernate.hbm2ddl.auto" value="update" />
            </properties>
        </persistence-unit>
1
Could you provide more information? Like the error logs you got?dade

1 Answers

4
votes

ATM to use JPA with Axon requires Spring. The only implementation provided for Axon TransactionManager is SpringTransactionManager which requires an instance of org.springframework.orm.jpa.JpaTransactionManager. See JpaEventStore and resource local transactions: quite old but seems still valid.

By Spring I mean not only Spring-orm but also Spring-context and Spring-aspects. I haven't find out how to use JpaTransactionManager without all the Spring magic.

Two exemples: