AxonFramework / AxonFramework

Framework for Evolutionary Message-Driven Microservices on the JVM
https://axoniq.io/
Apache License 2.0
3.25k stars 784 forks source link

AggregateSnapshot needs no-arg constructor #65

Closed mingfang closed 11 years ago

mingfang commented 11 years ago

A no-arg constructor is needed for the Serializable interface.

abuijze commented 11 years ago

Ming,

can you please elaborate? What is the error you see? The Java Serialization specification doesn't prescribe a no-arg constructor, other than for the first non-serializable superclass of a Serializable instance. Since the AggregateSnapshot doesn't have any superclass, no no-arg constructor is required.

Cheers,

Allard

mingfang commented 11 years ago

Allard

I'm working on adding snapshot capability to the CassandraEventStore and am getting this error.

java.lang.InstantiationException: org.axonframework.eventsourcing.AggregateSnapshot at java.lang.Class.newInstance0(Class.java:340) at java.lang.Class.newInstance(Class.java:308) at axon.cassandra.ProtostuffSerializer.deserialize(ProtostuffSerializer.java:109) at org.axonframework.serializer.LazyDeserializingObject.getObject(LazyDeserializingObject.java:81) at org.axonframework.serializer.SerializedMessage.getPayload(SerializedMessage.java:63) at org.axonframework.serializer.SerializedEventMessage.getPayload(SerializedEventMessage.java:66) at org.axonframework.serializer.SerializedDomainEventMessage.getPayload(SerializedDomainEventMessage.java:124) at org.axonframework.eventsourcing.EventSourcingRepository.createAggregate(EventSourcingRepository.java:187) at org.axonframework.eventsourcing.EventSourcingRepository.doLoad(EventSourcingRepository.java:156) at org.axonframework.eventsourcing.CachingEventSourcingRepository.doLoad(CachingEventSourcingRepository.java:94) at org.axonframework.eventsourcing.CachingEventSourcingRepository.doLoad(CachingEventSourcingRepository.java:37) at org.axonframework.repository.AbstractRepository.load(AbstractRepository.java:64) at org.axonframework.repository.LockingRepository.load(LockingRepository.java:118) at org.axonframework.commandhandling.annotation.AggregateAnnotationCommandHandler.loadAggregate(AggregateAnnotationCommandHandler.java:154) at org.axonframework.commandhandling.annotation.AggregateAnnotationCommandHandler.access$000(AggregateAnnotationCommandHandler.java:45) at org.axonframework.commandhandling.annotation.AggregateAnnotationCommandHandler$1.handle(AggregateAnnotationCommandHandler.java:139) at org.axonframework.commandhandling.DefaultInterceptorChain.proceed(DefaultInterceptorChain.java:62) at org.axonframework.commandhandling.DefaultInterceptorChain.proceed(DefaultInterceptorChain.java:68) at org.axonframework.commandhandling.SimpleCommandBus.doDispatch(SimpleCommandBus.java:118) at org.axonframework.commandhandling.SimpleCommandBus.dispatch(SimpleCommandBus.java:79)

Note that I'm using my own Serializer and it is getting the error when trying to instantiate a new AggregateSnapshot using reflection. It is surprising that I don't get this error using the XStream Serializer.

On Jul 6, 2012, at 3:41 AM, Allard Buijze reply@reply.github.com wrote:

Ming,

can you please elaborate? What is the error you see? The Java Serialization specification doesn't prescribe a no-arg constructor, other than for the first non-serializable superclass of a Serializable instance. Since the AggregateSnapshot doesn't have any superclass, no no-arg constructor is required.

Cheers,

Allard

On Fri, Jul 6, 2012 at 7:42 AM, mingfang < reply@reply.github.com

wrote:

A no-arg constructor is needed for the Serializable interface.


Reply to this email directly or view it on GitHub: https://github.com/AxonFramework/AxonFramework/issues/65


Reply to this email directly or view it on GitHub: https://github.com/AxonFramework/AxonFramework/issues/65#issuecomment-6799129

abuijze commented 11 years ago

Ok, so the problem is not so much that a no-arg constructor is needed for serialization, but that the specific serializer you are using requires no-arg constructors. XStream uses the Serialization capabilities to create new instances of objects. So it doesn't need no-arg constructors. And on a Oracle JVM, it will use some proprietary method to create an object instance without calling any construction mechanism at all.

Honestly, I am not really sure whether it is a good idea to create a no-arg constructor on that object. It just doesn't really seem in place there.