gpt4 book ai didi

hibernate 尝试序列化标记为@Transient 的字段

转载 作者:行者123 更新时间:2023-12-05 01:34:44 28 4
gpt4 key购买 nike

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

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

我可以提供有关 GenericEventHandler 类的更多信息或我为简洁起见而注释掉的任何代码(但我认为它们并不特别重要......我可能是错的)。

异常(exception):

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]); }
}

最佳答案

没关系,我似乎犯了一个错误,回想起来,我不应该花几个小时来弄清楚。问题不在于 hibernate ,也不在于 hibernate (除了 hibernate 使用标准 java 序列化)。

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

使固定:

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

关于 hibernate 尝试序列化标记为@Transient 的字段,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8441552/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com