org.hibernate.type.SerializationException and java.io.StreamCorruptedException: invalid stream header

Today, I encountered the following error when developing a few JPA entities.

Originally thrown as : java.io.StreamCorruptedException: invalid stream header: 52657175
	at java.io.ObjectInputStream.readStreamHeader(ObjectInputStream.java:782)
	at java.io.ObjectInputStream.(ObjectInputStream.java:279)
	at org.hibernate.util.SerializationHelper$CustomObjectInputStream.(SerializationHelper.java:252)
	at org.hibernate.util.SerializationHelper.deserialize(SerializationHelper.java:209)
Rethrown as : org.hibernate.type.SerializationException: could not deserialize
	at org.hibernate.util.SerializationHelper.deserialize(SerializationHelper.java:217)
	at org.hibernate.util.SerializationHelper.deserialize(SerializationHelper.java:240)
	at org.hibernate.type.SerializableType.fromBytes(SerializableType.java:82)
	at org.hibernate.type.SerializableType.get(SerializableType.java:39)
	at org.hibernate.type.NullableType.nullSafeGet(NullableType.java:163)
	at org.hibernate.type.NullableType.nullSafeGet(NullableType.java:154)
	at org.hibernate.loader.Loader.getKeyFromResultSet(Loader.java:1097)
	at org.hibernate.loader.Loader.getRowFromResultSet(Loader.java:565)
	at org.hibernate.loader.Loader.doQuery(Loader.java:701)
	at org.hibernate.loader.Loader.doQueryAndInitializeNonLazyCollections(Loader.java:236)
	at org.hibernate.loader.Loader.loadCollection(Loader.java:1994)
	at org.hibernate.loader.collection.CollectionLoader.initialize(CollectionLoader.java:37)
	at org.hibernate.persister.collection.AbstractCollectionPersister.initialize(AbstractCollectionPersister.java:566)
	at org.hibernate.event.def.DefaultInitializeCollectionEventListener.onInitializeCollection(DefaultInitializeCollectionEventListener.java:64)
	at org.hibernate.impl.SessionImpl.initializeCollection(SessionImpl.java:1716)
	at org.hibernate.collection.AbstractPersistentCollection.initialize(AbstractPersistentCollection.java:344)
	at org.hibernate.collection.AbstractPersistentCollection.read(AbstractPersistentCollection.java:87)
	at org.hibernate.collection.AbstractPersistentCollection.readElementExistence(AbstractPersistentCollection.java:143)
	at org.hibernate.collection.PersistentSet.contains(PersistentSet.java:153)
	at com.yarris.consol.messagelistener.RequeueToIntegrationHandler.isAppropriateHandler(RequeueToIntegrationHandler.java:69)
	at com.yarris.consol.messagelistener.OrderBAMMessageListener.onMessage(OrderBAMMessageListener.java:90)
	at org.springframework.jms.listener.AbstractMessageListenerContainer.doInvokeListener(AbstractMessageListenerContainer.java:561)
	at org.springframework.jms.listener.AbstractMessageListenerContainer.invokeListener(AbstractMessageListenerContainer.java:498)
	at org.springframework.jms.listener.AbstractMessageListenerContainer.doExecuteListener(AbstractMessageListenerContainer.java:467)
	at org.springframework.jms.listener.AbstractPollingMessageListenerContainer.doReceiveAndExecute(AbstractPollingMessageListenerContainer.java:325)
	at org.springframework.jms.listener.AbstractPollingMessageListenerContainer.receiveAndExecute(AbstractPollingMessageListenerContainer.java:243)
	at org.springframework.jms.listener.DefaultMessageListenerContainer$AsyncMessageListenerInvoker.invokeListener(DefaultMessageListenerContainer.java:1058)
	at org.springframework.jms.listener.DefaultMessageListenerContainer$AsyncMessageListenerInvoker.executeOngoingLoop(DefaultMessageListenerContainer.java:1050)
	at org.springframework.jms.listener.DefaultMessageListenerContainer$AsyncMessageListenerInvoker.run(DefaultMessageListenerContainer.java:947)
	at java.lang.Thread.run(Thread.java:662)

Basically, I have two JPA entities, and they have @ManyToMany relationship.

First JPA entity:

public class Person implements Serializable {
    @Id
    @Column(name = "person_no")
    private Long personNo;

    @ManyToMany(fetch = FetchType.LAZY)
    @JoinTable(
        name = "person_specialisation",
        joinColumns = 
            @JoinColumn(name = "person_no",
                referencedColumnName = "person_no"),
        inverseJoinColumns = 
            @JoinColumn(name = "specialisation_type", 
                referencedColumnName = "specialisation_type"))
    private Set<Specialisation> specs = new HashSet<Specialisation>();

    // ... rest of the code omitted
}

Second JPA entity:

public class Specialisation implements Serializable {
    @Id
    @Column(name = "specialisation_type")
    @Enumerated(EnumType.STRING)
    private SpecialisationType specialisationType;

    @ManyToMany(
        fetch = FetchType.LAZY,
        mappedBy = "specialisations")
    private Set<Person> persons = new HashSet<Person>

    // ... rest of the code omitted
}

SO, basically the finding is that enumeration cannot be used as Id. Some posts also suggest this cannot be done.

References:
Hibernate, 2010, ‘JPA Support of Enum as Primary Key’, accessed on 09 February 2012.
Bozho, 2010, ‘Using enum as id’, accessed on 09 February 2012.

Advertisements