Hibernate试图序列化标记为@Transient的字段

时间:2011-12-09 05:43:47

标签: hibernate serialization jpa annotations transient

我正在尝试将一组我称之为相对简单的对象的数据库保留到数据库中。不幸的是,Hibernate似乎忽略了@Transient注释(或者它试图在我的几个类上序列化,甚至带有注释)。当我有无法序列化的类时,这会导致一个令人讨厌的错误。

如果需要,我可以使AxisEventHandler类可序列化,但是如果我不能(例如它是封闭源库中的最终类),那我该怎么办?

我可以提供更多关于GenericEventHandler类的信息,或者我为了简洁请求提供的任何代码(但我不认为它们特别重要......我可能错了)。

例外:

org.hibernate.type.SerializationException: could not serialize
    at org.hibernate.util.SerializationHelper.serialize(SerializationHelper.java:139)
    at org.hibernate.util.SerializationHelper.serialize(SerializationHelper.java:164)
    at org.hibernate.util.SerializationHelper.clone(SerializationHelper.java:95)
    at org.hibernate.type.descriptor.java.SerializableTypeDescriptor$SerializableMutabilityPlan.deepCopyNotNull(SerializableTypeDescriptor.java:54)
    at org.hibernate.type.descriptor.java.SerializableTypeDescriptor$SerializableMutabilityPlan.deepCopyNotNull(SerializableTypeDescriptor.java:42)
    at org.hibernate.type.descriptor.java.MutableMutabilityPlan.deepCopy(MutableMutabilityPlan.java:58)
    at org.hibernate.type.AbstractStandardBasicType.deepCopy(AbstractStandardBasicType.java:314)
    at org.hibernate.type.AbstractStandardBasicType.deepCopy(AbstractStandardBasicType.java:310)
    at org.hibernate.type.TypeHelper.deepCopy(TypeHelper.java:68)
    at org.hibernate.event.def.AbstractSaveEventListener.performSaveOrReplicate(AbstractSaveEventListener.java:302)
    at org.hibernate.event.def.AbstractSaveEventListener.performSave(AbstractSaveEventListener.java:203)
    at org.hibernate.event.def.AbstractSaveEventListener.saveWithGeneratedId(AbstractSaveEventListener.java:129)
    at org.hibernate.event.def.DefaultSaveOrUpdateEventListener.saveWithGeneratedOrRequestedId(DefaultSaveOrUpdateEventListener.java:210)
    at org.hibernate.event.def.DefaultSaveEventListener.saveWithGeneratedOrRequestedId(DefaultSaveEventListener.java:56)
    at org.hibernate.event.def.DefaultSaveOrUpdateEventListener.entityIsTransient(DefaultSaveOrUpdateEventListener.java:195)
    at org.hibernate.event.def.DefaultSaveEventListener.performSaveOrUpdate(DefaultSaveEventListener.java:50)
    at org.hibernate.event.def.DefaultSaveOrUpdateEventListener.onSaveOrUpdate(DefaultSaveOrUpdateEventListener.java:93)
    at org.hibernate.impl.SessionImpl.fireSave(SessionImpl.java:713)
    at org.hibernate.impl.SessionImpl.save(SessionImpl.java:701)
    at org.hibernate.impl.SessionImpl.save(SessionImpl.java:697)
    at com.mypandafinance.amino.gui.ccsimple.CCSimpleApp.initApplication(CCSimpleApp.java:55)
    at com.mypandafinance.amino.AminoBoot.main(AminoBoot.java:101)
Caused by: java.io.NotSerializableException: com.mypandafinance.chartcomponent.eventhandlers.AxisEventHandler
    at java.io.ObjectOutputStream.writeObject0(ObjectOutputStream.java:1180)
    at java.io.ObjectOutputStream.defaultWriteFields(ObjectOutputStream.java:1528)
    at java.io.ObjectOutputStream.writeSerialData(ObjectOutputStream.java:1493)
    at java.io.ObjectOutputStream.writeOrdinaryObject(ObjectOutputStream.java:1416)
    at java.io.ObjectOutputStream.writeObject0(ObjectOutputStream.java:1174)
    at java.io.ObjectOutputStream.defaultWriteFields(ObjectOutputStream.java:1528)
    at java.io.ObjectOutputStream.writeSerialData(ObjectOutputStream.java:1493)
    at java.io.ObjectOutputStream.writeOrdinaryObject(ObjectOutputStream.java:1416)
    at java.io.ObjectOutputStream.writeObject0(ObjectOutputStream.java:1174)
    at java.io.ObjectOutputStream.defaultWriteFields(ObjectOutputStream.java:1528)
    at java.io.ObjectOutputStream.writeSerialData(ObjectOutputStream.java:1493)
    at java.io.ObjectOutputStream.writeOrdinaryObject(ObjectOutputStream.java:1416)
    at java.io.ObjectOutputStream.writeObject0(ObjectOutputStream.java:1174)
    at java.io.ObjectOutputStream.writeObject(ObjectOutputStream.java:346)
    at org.hibernate.util.SerializationHelper.serialize(SerializationHelper.java:135)
    ... 21 more

主轴类:

@Entity
@Inheritance(strategy=InheritanceType.JOINED)
public abstract class Axis implements Serializable {
    private int axisId;

    @Id
    @GeneratedValue(strategy = GenerationType.AUTO)
    public int getAxisId() { return axisId; }
    protected void setAxisId(int id) { this.axisId = id; }

    @Transient
    public abstract long getMax();
    public abstract void setMax(long max);

    @Transient
    public abstract long getMin();
    public abstract void setMin(long min);

    /* ... helper methods here */

    @Transient
    public abstract AxisEventHandler getEventHandler();

    @Transient
    public abstract AxisFlagHandler getFlagHandler();

    @Transient
    public abstract TickMarkFormatter getTickMarkFormatter();
    public abstract void setTickMarkFormatter(TickMarkFormatter formatter);
}

基本实施:

@Entity
@SecondaryTable(name="BasicAxis")
public class BasicAxis extends Axis {
    private long min;
    private long max;

    // Event handler, used for firing off events to all listeners
    private AxisEventHandler eventHandler = new AxisEventHandler();

    // Flag handler, used for storing axis flags
    private SerializableAxisFlagHandler flagHandler;

    private TickMarkFormatter formatter;

    /* ... constructors */

    @Column(name="rangemax")
    @Override public long getMax() { return max; }
    @Override public void setMax(long max) { /* ... */ }

    @Column(name="rangemin")
    @Override public long getMin() { return min; }
    @Override public void setMin(long min) { /* ... */ }

    @Override public void setRange(long min, long max) { /* ... */ }
    @Override public void pan(long amount) { /* ... */ }

    @Transient
    @Override public AxisEventHandler getEventHandler() { /* ... */ }

    @Type(type="com.mypandafinance.chartcomponent.hibernateusertypes.AxisFlagHandlerUserType")
    @Column(name="flags")
    @Override public SerializableAxisFlagHandler getFlagHandler() { /* ... */ }
    protected void setFlagHandler(SerializableAxisFlagHandler flagHandler) { /* ... */ }

    @Column(name="formatter", table="BasicAxis")
    @Override public TickMarkFormatter getTickMarkFormatter() { /* ... */ }
    @Override public void setTickMarkFormatter(TickMarkFormatter formatter) { /* ... */ }

    /* ... helper methods */
}

AxisEventHandler类:

public class AxisEventHandler extends GenericEventHandler<AxisEventListener> {
    public AxisEventHandler() { super(new AxisEventListener[0]); }
}

2 个答案:

答案 0 :(得分:4)

没关系,我好像犯了一个错误,回想起来,我不应该花几个小时来弄明白。问题不在于hibernate,或与Hibernate有关(除了Hibernate使用的是标准的java序列化)。

这些字段不仅需要使用JPA @Transient注释进行标记,而且对于Serializable接口它们也需要是瞬态的(因为出于某种原因,Hibernate在持久化之前序列化对象?)

修正:

public class BasicAxis extends Axis {
    /* ... */
   private transient AxisEventHandler eventHandler = new AxisEventHandler();
    /* ... */
}

答案 1 :(得分:1)

这恰好发生在我身上,我也失去了几个小时。虽然添加transient Java修饰符摆脱了即时错误,但我不断得到越来越多奇怪的序列化错误,直到最终Hibernate抱怨它无法将我的对象的二进制表示形式放入字段中(因为它太长了:

Caused by: org.h2.jdbc.JdbcBatchUpdateException: Value too long for column

真正的问题?我偶然搞砸了JPA注释。我这样做了:

@Column(name = "val_foo")
private Foo val;

而不是:

@OneToOne(targetEntity = FooImpl.class, cascade = { PERSIST })
@JoinColumn(name = "val_foo", referencedColumnName = "uuid")
private Foo val;

希望有一天这会帮助某人......