具有复合主键的Hibernate实体的Infinispan编组错误

时间:2014-09-30 00:28:02

标签: java hibernate infinispan hibernate-4.x

我已将Hibernate缓存从EHCache切换到Infinispan,我现在在使用复合主键修改实体时遇到异常。该实体如下:

@Entity
@Table(name="Company_Message_Type")
@Cache(usage=CacheConcurrencyStrategy.TRANSACTIONAL)
public class CompanyMessageType implements Serializable
{
    private static final long serialVersionUID = 1L;

    @EmbeddedId private final CompanyMessageTypeId id = new CompanyMessageTypeId();

    @Column(name="FL_EXTERNAL_ACCESS", nullable=false)
    private boolean externalAccess;

    @Column(name="FL_VIEW_CONTENT", nullable=false)
    private boolean viewContent;

    @MapsId("companyId")
    @ManyToOne(fetch=FetchType.LAZY)
    @JoinColumn(name="ID_COMPANY", nullable=false)
    private Company company;

    @MapsId("messageTypeId")
    @ManyToOne(fetch=FetchType.LAZY)
    @JoinColumn(name="ID_MESSAGE_TYPE", nullable=false)
    private MessageType messageType;
}

嵌入式Id类如下:

@Embeddable
public class CompanyMessageTypeId implements Serializable
{
    private static final long serialVersionUID = 1L;

    @Column(name="ID_COMPANY", nullable=false)
    private Long companyId;

    @Column(name="ID_MESSAGE_TYPE", nullable=false)
    private Long messageTypeId;
}

这两个类都有合理的equals()hashCode()方法。

仅当群集中有多个服务器时才会发生异常。正在更新的服务器上出现异常。堆栈跟踪如下:

ISPN000220: Problems un-marshalling remote command from byte buffer
java.io.InvalidObjectException: Could not find a SessionFactory [uuid=0d0cdf26-dfe6-4285-9725-dfaa4821ecba,name=null]
    at org.hibernate.internal.SessionFactoryImpl.locateSessionFactoryOnDeserialization(SessionFactoryImpl.java:1781)
    at org.hibernate.internal.SessionFactoryImpl.readResolve(SessionFactoryImpl.java:1761)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke(Method.java:606)
    at org.jboss.marshalling.reflect.SerializableClass.callReadResolve(SerializableClass.java:413)
    at org.jboss.marshalling.river.RiverUnmarshaller.doReadNewObject(RiverUnmarshaller.java:1270)
    at org.jboss.marshalling.river.RiverUnmarshaller.doReadObject(RiverUnmarshaller.java:272)
    at org.jboss.marshalling.river.RiverUnmarshaller.doReadObject(RiverUnmarshaller.java:209)
    at org.jboss.marshalling.river.RiverUnmarshaller.readFields(RiverUnmarshaller.java:1711)
    at org.jboss.marshalling.river.RiverUnmarshaller.doInitSerializable(RiverUnmarshaller.java:1627)
    at org.jboss.marshalling.river.RiverUnmarshaller.doReadNewObject(RiverUnmarshaller.java:1269)
    at org.jboss.marshalling.river.RiverUnmarshaller.doReadObject(RiverUnmarshaller.java:272)
    at org.jboss.marshalling.river.RiverUnmarshaller.doReadObject(RiverUnmarshaller.java:209)
    at org.jboss.marshalling.river.RiverUnmarshaller.readFields(RiverUnmarshaller.java:1711)
    at org.jboss.marshalling.river.RiverUnmarshaller.doInitSerializable(RiverUnmarshaller.java:1627)
    at org.jboss.marshalling.river.RiverUnmarshaller.doInitSerializable(RiverUnmarshaller.java:1591)
    at org.jboss.marshalling.river.RiverUnmarshaller.doReadNewObject(RiverUnmarshaller.java:1269)
    at org.jboss.marshalling.river.RiverUnmarshaller.doReadObject(RiverUnmarshaller.java:272)
    at org.jboss.marshalling.river.RiverUnmarshaller.doReadObject(RiverUnmarshaller.java:209)
    at org.jboss.marshalling.river.RiverUnmarshaller.readFields(RiverUnmarshaller.java:1711)
    at org.jboss.marshalling.river.RiverUnmarshaller.doInitSerializable(RiverUnmarshaller.java:1627)
    at org.jboss.marshalling.river.RiverUnmarshaller.doReadNewObject(RiverUnmarshaller.java:1269)
    at org.jboss.marshalling.river.RiverUnmarshaller.doReadObject(RiverUnmarshaller.java:272)
    at org.jboss.marshalling.river.RiverUnmarshaller.doReadObject(RiverUnmarshaller.java:209)
    at org.jboss.marshalling.AbstractObjectInput.readObject(AbstractObjectInput.java:41)
    at org.infinispan.marshall.exts.ReplicableCommandExternalizer.readParameters(ReplicableCommandExternalizer.java:100)
    at org.infinispan.marshall.exts.ReplicableCommandExternalizer.readObject(ReplicableCommandExternalizer.java:83)
    at org.infinispan.marshall.exts.ReplicableCommandExternalizer.readObject(ReplicableCommandExternalizer.java:30)
    at org.infinispan.marshall.core.ExternalizerTable$ExternalizerAdapter.readObject(ExternalizerTable.java:389)
    at org.infinispan.marshall.core.ExternalizerTable.readObject(ExternalizerTable.java:205)
    at org.infinispan.marshall.core.JBossMarshaller$ExternalizerTableProxy.readObject(JBossMarshaller.java:152)
    at org.jboss.marshalling.river.RiverUnmarshaller.doReadObject(RiverUnmarshaller.java:351)
    at org.jboss.marshalling.river.RiverUnmarshaller.doReadObject(RiverUnmarshaller.java:209)
    at org.jboss.marshalling.AbstractObjectInput.readObject(AbstractObjectInput.java:41)
    at org.infinispan.marshall.exts.ReplicableCommandExternalizer.readParameters(ReplicableCommandExternalizer.java:100)
    at org.infinispan.marshall.exts.CacheRpcCommandExternalizer.readObject(CacheRpcCommandExternalizer.java:146)
    at org.infinispan.marshall.exts.CacheRpcCommandExternalizer.readObject(CacheRpcCommandExternalizer.java:59)
    at org.infinispan.marshall.core.ExternalizerTable$ExternalizerAdapter.readObject(ExternalizerTable.java:389)
    at org.infinispan.marshall.core.ExternalizerTable.readObject(ExternalizerTable.java:205)
    at org.infinispan.marshall.core.JBossMarshaller$ExternalizerTableProxy.readObject(JBossMarshaller.java:152)
    at org.jboss.marshalling.river.RiverUnmarshaller.doReadObject(RiverUnmarshaller.java:351)
    at org.jboss.marshalling.river.RiverUnmarshaller.doReadObject(RiverUnmarshaller.java:209)
    at org.jboss.marshalling.AbstractObjectInput.readObject(AbstractObjectInput.java:41)
    at org.infinispan.commons.marshall.jboss.AbstractJBossMarshaller.objectFromObjectStream(AbstractJBossMarshaller.java:136)
    at org.infinispan.marshall.core.VersionAwareMarshaller.objectFromByteBuffer(VersionAwareMarshaller.java:101)
    at org.infinispan.commons.marshall.AbstractDelegatingMarshaller.objectFromByteBuffer(AbstractDelegatingMarshaller.java:80)
    at org.infinispan.remoting.transport.jgroups.MarshallerAdapter.objectFromBuffer(MarshallerAdapter.java:28)
    at org.infinispan.remoting.transport.jgroups.CommandAwareRpcDispatcher.handle(CommandAwareRpcDispatcher.java:206)
    at org.jgroups.blocks.RequestCorrelator.handleRequest(RequestCorrelator.java:460)
    at org.jgroups.blocks.RequestCorrelator.receiveMessage(RequestCorrelator.java:377)
    at org.jgroups.blocks.RequestCorrelator.receive(RequestCorrelator.java:247)
    at org.jgroups.blocks.MessageDispatcher$ProtocolAdapter.up(MessageDispatcher.java:665)
    at org.jgroups.JChannel.up(JChannel.java:708)
    at org.jgroups.stack.ProtocolStack.up(ProtocolStack.java:1015)
    at org.jgroups.protocols.RSVP.up(RSVP.java:187)
    at org.jgroups.protocols.FRAG2.up(FRAG2.java:165)
    at org.jgroups.protocols.FlowControl.up(FlowControl.java:381)
    at org.jgroups.protocols.FlowControl.up(FlowControl.java:370)
    at org.jgroups.protocols.tom.TOA.up(TOA.java:121)
    at org.jgroups.protocols.pbcast.GMS.up(GMS.java:1010)
    at org.jgroups.protocols.pbcast.STABLE.up(STABLE.java:234)
    at org.jgroups.protocols.AUTH.up(AUTH.java:118)
    at org.jgroups.protocols.UNICAST3.handleDataReceived(UNICAST3.java:694)
    at org.jgroups.protocols.UNICAST3.up(UNICAST3.java:381)
    at org.jgroups.protocols.pbcast.NAKACK2.up(NAKACK2.java:600)
    at org.jgroups.protocols.VERIFY_SUSPECT.up(VERIFY_SUSPECT.java:147)
    at org.jgroups.protocols.FD.up(FD.java:255)
    at org.jgroups.protocols.FD_SOCK.up(FD_SOCK.java:301)
    at org.jgroups.protocols.MERGE2.up(MERGE2.java:209)
    at org.jgroups.protocols.Discovery.up(Discovery.java:379)
    at org.jgroups.protocols.MPING.up(MPING.java:181)
    at org.jgroups.protocols.TP.passMessageUp(TP.java:1399)
    at org.jgroups.protocols.TP$MyHandler.run(TP.java:1585)
    at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1145)
    at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:615)
    at java.lang.Thread.run(Thread.java:745)
Caused by: an exception which occurred:
    in object of type org.hibernate.internal.SessionFactoryImpl
    in field factory
    in object of type org.hibernate.type.TypeFactory$TypeScopeImpl
    in field typeScope
    in object of type org.hibernate.type.EmbeddedComponentType
    in field type
    in object of type org.hibernate.cache.spi.CacheKey

之前有没有人见过这个错误?看起来infinispan可能无法正确地序列化嵌入式id类。

我使用的是Hibernate 4.3.5和Infinispan 6.0.2

修改

问题在于Infinispan正在通过网络发送CacheKey的序列化版本。此密钥间接引用启动计算机上的SessionFactoryImpl,但远程计算机无法反序列化SessionFactoryImpl,因此会抛出此异常。我认为该问题特定于具有复合主要内容的实体,因为如果密钥只是Long,则type的{​​{1}}将是其他不会间接引用的内容CacheKey

总之,我不确定它是否是Infinispan问题或Hibernate问题。

1 个答案:

答案 0 :(得分:2)

正如pull request所讨论的,显然SessionFactory可以在JVM上进行序列化,只要它被命名(参见HHH-6822)。为了解决我的问题,我只需要在会话工厂上设置以下Hibernate属性:

hibernate.session_factory_name = MySessionFactory
hibernate.session_factory_name_is_jndi = false