在JUnit测试期间尝试持久保存实体时,我收到以下异常。我正在使用Eclipselink 2.5.2。我找不到可能导致此异常的原因。
Exception in thread "Thread-3" javax.persistence.RollbackException: Exception [EclipseLink-32] (Eclipse Persistence Services - 2.5.2.v20140319-9ad6abd): org.eclipse.persistence.exceptions.DescriptorException
Exception Description: Trying to set value [165.0] for instance variable [doubleValue] of type [java.lang.Double] in the object. The specified object is not an instance of the class or interface declaring the underlying field, or an unwrapping conversion has failed.
Internal Exception: java.lang.IllegalArgumentException: Can not set java.lang.Double field ar.asimov.acumar.ema.model.DoubleListValue.doubleValue to ar.asimov.acumar.ema.model.ExtraHumidity
Mapping: org.eclipse.persistence.mappings.DirectToFieldMapping[doubleValue-->ta_ams_weather_list_parameters.double_value]
Descriptor: RelationalDescriptor(ar.asimov.acumar.ema.model.ExtraTemperature --> [DatabaseTable(ta_ams_weather_list_parameters)])
at org.eclipse.persistence.internal.jpa.transaction.EntityTransactionImpl.commit(EntityTransactionImpl.java:157)
at ar.asimov.acumar.ema.model.helper.EntityManagerHelper.commitTransaction(EntityManagerHelper.java:41)
at ar.asimov.acumar.ema.services.WeatherDataConsumer.run(WeatherDataConsumer.java:51)
at java.lang.Thread.run(Thread.java:745)
Caused by: Exception [EclipseLink-32] (Eclipse Persistence Services - 2.5.2.v20140319-9ad6abd): org.eclipse.persistence.exceptions.DescriptorException
Exception Description: Trying to set value [165.0] for instance variable [doubleValue] of type [java.lang.Double] in the object. The specified object is not an instance of the class or interface declaring the underlying field, or an unwrapping conversion has failed.
Internal Exception: java.lang.IllegalArgumentException: Can not set java.lang.Double field ar.asimov.acumar.ema.model.DoubleListValue.doubleValue to ar.asimov.acumar.ema.model.ExtraHumidity
Mapping: org.eclipse.persistence.mappings.DirectToFieldMapping[doubleValue-->ta_ams_weather_list_parameters.double_value]
Descriptor: RelationalDescriptor(ar.asimov.acumar.ema.model.ExtraTemperature --> [DatabaseTable(ta_ams_weather_list_parameters)])
at org.eclipse.persistence.exceptions.DescriptorException.illegalArgumentWhileSettingValueThruInstanceVariableAccessor(DescriptorException.java:703)
at org.eclipse.persistence.internal.descriptors.InstanceVariableAttributeAccessor.setAttributeValueInObject(InstanceVariableAttributeAccessor.java:188)
at org.eclipse.persistence.mappings.DatabaseMapping.setAttributeValueInObject(DatabaseMapping.java:1625)
at org.eclipse.persistence.mappings.foundation.AbstractDirectMapping.mergeIntoObject(AbstractDirectMapping.java:1040)
at org.eclipse.persistence.internal.descriptors.ObjectBuilder.mergeIntoObject(ObjectBuilder.java:4132)
at org.eclipse.persistence.internal.descriptors.ObjectBuilder.mergeChangesIntoObject(ObjectBuilder.java:4065)
at org.eclipse.persistence.internal.sessions.MergeManager.mergeChangesOfWorkingCopyIntoOriginal(MergeManager.java:884)
at org.eclipse.persistence.internal.sessions.MergeManager.mergeChangesOfWorkingCopyIntoOriginal(MergeManager.java:698)
at org.eclipse.persistence.internal.sessions.MergeManager.mergeChanges(MergeManager.java:309)
at org.eclipse.persistence.mappings.CollectionMapping.mergeIntoObject(CollectionMapping.java:1638)
at org.eclipse.persistence.internal.descriptors.ObjectBuilder.mergeIntoObject(ObjectBuilder.java:4132)
at org.eclipse.persistence.internal.descriptors.ObjectBuilder.mergeChangesIntoObject(ObjectBuilder.java:4065)
at org.eclipse.persistence.internal.sessions.MergeManager.mergeChangesOfWorkingCopyIntoOriginal(MergeManager.java:839)
at org.eclipse.persistence.internal.sessions.MergeManager.mergeChangesOfWorkingCopyIntoOriginal(MergeManager.java:698)
at org.eclipse.persistence.internal.sessions.MergeManager.mergeChanges(MergeManager.java:309)
at org.eclipse.persistence.internal.sessions.UnitOfWorkImpl.mergeChangesIntoParent(UnitOfWorkImpl.java:3280)
at org.eclipse.persistence.internal.sessions.RepeatableWriteUnitOfWork.mergeChangesIntoParent(RepeatableWriteUnitOfWork.java:376)
at org.eclipse.persistence.internal.sessions.RepeatableWriteUnitOfWork.commitRootUnitOfWork(RepeatableWriteUnitOfWork.java:290)
at org.eclipse.persistence.internal.sessions.UnitOfWorkImpl.commitAndResume(UnitOfWorkImpl.java:1169)
at org.eclipse.persistence.internal.jpa.transaction.EntityTransactionImpl.commit(EntityTransactionImpl.java:132)
... 3 more
Caused by: java.lang.IllegalArgumentException: Can not set java.lang.Double field ar.asimov.acumar.ema.model.DoubleListValue.doubleValue to ar.asimov.acumar.ema.model.ExtraHumidity
at sun.reflect.UnsafeFieldAccessorImpl.throwSetIllegalArgumentException(UnsafeFieldAccessorImpl.java:167)
at sun.reflect.UnsafeFieldAccessorImpl.throwSetIllegalArgumentException(UnsafeFieldAccessorImpl.java:171)
at sun.reflect.UnsafeFieldAccessorImpl.ensureObj(UnsafeFieldAccessorImpl.java:58)
at sun.reflect.UnsafeObjectFieldAccessorImpl.set(UnsafeObjectFieldAccessorImpl.java:75)
at java.lang.reflect.Field.set(Field.java:764)
at org.eclipse.persistence.internal.descriptors.InstanceVariableAttributeAccessor.setAttributeValueInObject(InstanceVariableAttributeAccessor.java:141)
... 21 more
我的模型中有以下类(我省略了getter和setter 为简单起见)
@IdClass(ListParameterValue.PrimaryKey.class)
@Entity
@Inheritance(strategy=InheritanceType.SINGLE_TABLE)
@DiscriminatorColumn(discriminatorType=DiscriminatorType.STRING, name="parameter_type",length=20)
@Table(name="ta_ams_weather_list_parameters")
@Access(AccessType.FIELD)
public abstract class ListParameterValue implements Serializable {
private static final long serialVersionUID = 1L;
public static class PrimaryKey implements Serializable{...}
@Id
@ManyToOne
@JoinColumn(name="station_id",referencedColumnName="station_id")
protected Station station;
@Id
@Column(name="date")
protected LocalDate date;
@Id
@Column(name="start_time")
protected LocalTime startTime;
@Id
@Column(name="order_index")
protected Integer order;
@Id
@Column(name="parameter_type")
protected String type;
}
@MappedSuperclass
@Access(AccessType.FIELD)
public abstract class DoubleListValue extends ListParameterValue implements Serializable {
private static final long serialVersionUID = 1L;
@Column(name="double_value", nullable = true, columnDefinition="default null")
protected Double doubleValue;
}
@MappedSuperclass
@Access(AccessType.FIELD)
public abstract class ShortListValue extends ListParameterValue implements Serializable {
private static final long serialVersionUID = 1L;
@Column(name="short_value")
protected Short shortValue;
}
@Entity
@DiscriminatorValue("extra_humidity")
public class ExtraHumidity extends ShortListValue implements Serializable{
/**
*
*/
private static final long serialVersionUID = 1L;
public ExtraHumidity() {
super();
}
}
我真的很感激帮助。 提前致谢
答案 0 :(得分:0)
我猜测你的测试有一个错误,它试图创建一个扩展DoubleListValue并将其合并的实例,但是给它和已经被ExtraHumidity实例使用的ID。这是非法的,因为如果没有先删除然后重新插入实体,就无法更改实体的类型。
我们不能说如果没有看到你的测试就会发生这种情况。