hibernate“调用getter时出现IllegalArgumentException”

时间:2010-08-02 19:04:08

标签: java hibernate orm

这是我的类层次结构(为了简洁省略了lombok生成的构造函数/ getters / setter):

public class A
{
    ... other variables ...

    @OneToMany(fetch=FetchType.LAZY, cascade = {CascadeType.ALL})
    private Set<B> bSet = new HashSet<B>();
}

public class B
{
    ... other variables ...

    @CollectionOfElements
    @IndexColumn(name="index")
    private List<C> cList = new ArrayList<C>();
}

public class C
{
    ... other variables ...

    @Embedded
    private D[] dArray;
}

@Embeddable
public class D
{
    ... other variables ...

    private float f1;

    private float f2;
}

问题是当我保存一个在bSet中有东西的新实例时,我得到一个IllegalArgumentException。这是堆栈跟踪的相关部分:

 org.hibernate.PropertyAccessException: IllegalArgumentException occurred calling getter of path.to.D.f1
    at path.to.dao.commitTransaction(PersistableDAO.java:32)
    at path.to.daoCaller(daoCaller.java:79)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
    at java.lang.reflect.Method.invoke(Method.java:597)
    ... omitted for brevity ...
Caused by: org.hibernate.PropertyAccessException: IllegalArgumentException occurred calling getter of path.to.D.f1
    at org.hibernate.property.BasicPropertyAccessor$BasicGetter.get(BasicPropertyAccessor.java:195)
    at org.hibernate.tuple.component.AbstractComponentTuplizer.getPropertyValue(AbstractComponentTuplizer.java:87)
    at org.hibernate.tuple.component.AbstractComponentTuplizer.getPropertyValues(AbstractComponentTuplizer.java:93)
    at org.hibernate.tuple.component.PojoComponentTuplizer.getPropertyValues(PojoComponentTuplizer.java:109)
    at org.hibernate.type.ComponentType.getPropertyValues(ComponentType.java:376)
    at org.hibernate.type.ComponentType.deepCopy(ComponentType.java:419)
    at org.hibernate.type.ComponentType.deepCopy(ComponentType.java:421)
    at org.hibernate.collection.PersistentList.getSnapshot(PersistentList.java:61)
    at org.hibernate.engine.CollectionEntry.<init>(CollectionEntry.java:91)
    at org.hibernate.engine.StatefulPersistenceContext.addCollection(StatefulPersistenceContext.java:807)
    at org.hibernate.engine.StatefulPersistenceContext.addNewCollection(StatefulPersistenceContext.java:774)
    at org.hibernate.event.def.WrapVisitor.processArrayOrNewCollection(WrapVisitor.java:108)
    at org.hibernate.event.def.WrapVisitor.processCollection(WrapVisitor.java:74)
    at org.hibernate.event.def.AbstractVisitor.processValue(AbstractVisitor.java:124)
    at org.hibernate.event.def.WrapVisitor.processValue(WrapVisitor.java:121)
    at org.hibernate.event.def.AbstractVisitor.processEntityPropertyValues(AbstractVisitor.java:78)
    at org.hibernate.event.def.AbstractSaveEventListener.visitCollectionsBeforeSave(AbstractSaveEventListener.java:394)
    at org.hibernate.event.def.AbstractSaveEventListener.performSaveOrReplicate(AbstractSaveEventListener.java:296)
    at org.hibernate.event.def.AbstractSaveEventListener.performSave(AbstractSaveEventListener.java:204)
    at org.hibernate.event.def.AbstractSaveEventListener.saveWithGeneratedId(AbstractSaveEventListener.java:130)
    at org.hibernate.event.def.DefaultSaveOrUpdateEventListener.saveWithGeneratedOrRequestedId(DefaultSaveOrUpdateEventListener.java:210)
    at org.hibernate.event.def.DefaultSaveOrUpdateEventListener.entityIsTransient(DefaultSaveOrUpdateEventListener.java:195)
    at org.hibernate.event.def.DefaultSaveOrUpdateEventListener.performSaveOrUpdate(DefaultSaveOrUpdateEventListener.java:117)
    at org.hibernate.event.def.DefaultSaveOrUpdateEventListener.onSaveOrUpdate(DefaultSaveOrUpdateEventListener.java:93)
    at org.hibernate.impl.SessionImpl.fireSaveOrUpdate(SessionImpl.java:535)
    at org.hibernate.impl.SessionImpl.saveOrUpdate(SessionImpl.java:527)
    at org.hibernate.engine.CascadingAction$5.cascade(CascadingAction.java:241)
    at org.hibernate.engine.Cascade.cascadeToOne(Cascade.java:292)
    at org.hibernate.engine.Cascade.cascadeAssociation(Cascade.java:240)
    at org.hibernate.engine.Cascade.cascadeProperty(Cascade.java:193)
    at org.hibernate.engine.Cascade.cascadeCollectionElements(Cascade.java:320)
    at org.hibernate.engine.Cascade.cascadeCollection(Cascade.java:266)
    at org.hibernate.engine.Cascade.cascadeAssociation(Cascade.java:243)
    at org.hibernate.engine.Cascade.cascadeProperty(Cascade.java:193)
    at org.hibernate.engine.Cascade.cascade(Cascade.java:154)
    at org.hibernate.event.def.AbstractSaveEventListener.cascadeAfterSave(AbstractSaveEventListener.java:479)
    at org.hibernate.event.def.AbstractSaveEventListener.performSaveOrReplicate(AbstractSaveEventListener.java:357)
    at org.hibernate.event.def.AbstractSaveEventListener.performSave(AbstractSaveEventListener.java:204)
    at org.hibernate.event.def.AbstractSaveEventListener.saveWithGeneratedId(AbstractSaveEventListener.java:130)
    at org.hibernate.event.def.DefaultSaveOrUpdateEventListener.saveWithGeneratedOrRequestedId(DefaultSaveOrUpdateEventListener.java:210)
    at org.hibernate.event.def.DefaultSaveOrUpdateEventListener.entityIsTransient(DefaultSaveOrUpdateEventListener.java:195)
    at org.hibernate.event.def.DefaultSaveOrUpdateEventListener.performSaveOrUpdate(DefaultSaveOrUpdateEventListener.java:117)
    at org.hibernate.event.def.DefaultSaveOrUpdateEventListener.onSaveOrUpdate(DefaultSaveOrUpdateEventListener.java:93)
    at org.hibernate.impl.SessionImpl.fireSaveOrUpdate(SessionImpl.java:535)
    at org.hibernate.impl.SessionImpl.saveOrUpdate(SessionImpl.java:527)
    at org.hibernate.engine.CascadingAction$5.cascade(CascadingAction.java:241)
    at org.hibernate.engine.Cascade.cascadeToOne(Cascade.java:292)
    at org.hibernate.engine.Cascade.cascadeAssociation(Cascade.java:240)
    at org.hibernate.engine.Cascade.cascadeProperty(Cascade.java:193)
    at org.hibernate.engine.Cascade.cascadeCollectionElements(Cascade.java:320)
    at org.hibernate.engine.Cascade.cascadeCollection(Cascade.java:266)
    at org.hibernate.engine.Cascade.cascadeAssociation(Cascade.java:243)
    at org.hibernate.engine.Cascade.cascadeProperty(Cascade.java:193)
    at org.hibernate.engine.Cascade.cascade(Cascade.java:154)
    at org.hibernate.event.def.AbstractFlushingEventListener.cascadeOnFlush(AbstractFlushingEventListener.java:154)
    at org.hibernate.event.def.AbstractFlushingEventListener.prepareEntityFlushes(AbstractFlushingEventListener.java:145)
    at org.hibernate.event.def.AbstractFlushingEventListener.flushEverythingToExecutions(AbstractFlushingEventListener.java:88)
    at org.hibernate.event.def.DefaultFlushEventListener.onFlush(DefaultFlushEventListener.java:49)
    at org.hibernate.impl.SessionImpl.flush(SessionImpl.java:1028)
    at org.hibernate.impl.SessionImpl.managedFlush(SessionImpl.java:366)
    at org.hibernate.transaction.JDBCTransaction.commit(JDBCTransaction.java:137)
    at path.to.dao.commitTransaction(PersistableDAO.java:28)
    ... 52 more
Caused by: java.lang.IllegalArgumentException: object is not an instance of declaring class
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
    at java.lang.reflect.Method.invoke(Method.java:597)
    at org.hibernate.property.BasicPropertyAccessor$BasicGetter.get(BasicPropertyAccessor.java:169)
    ... 113 more

谁能告诉我发生了什么?

谢谢!

1 个答案:

答案 0 :(得分:1)

AFAIK,你不能坚持一系列的Embeddable。换句话说,不支持以下内容:

public class C {
    ... other variables ...

    @Embedded
    private D[] dArray;
}

从JPA 1.0规范:

  

2.1.5可嵌入类

     

实体可以使用其他细粒度   用于表示实体状态的类。   与这些类的实例不同   实体实例本身,不   有持久的身份。代替,   它们只作为嵌入对象存在   他们所属的实体。这样   嵌入对象严格属于   他们拥有的实体,而不是   可以跨持久实体共享。   试图共享嵌入对象   跨实体未定义   语义。 因为这些对象   没有持久的身份,他们是   通常与...一起映射   他们的实体实例   属于

     

可嵌入的类必须遵守   第2.1节中规定的要求   对于除例外的实体   embeddable类没有注释   为Entity。必须嵌入类   注释为Embeddable或   在XML描述符中表示为这样。   嵌入对象的访问类型   由访问类型决定   嵌入它的实体。   仅支持一个级别   这需要嵌入   说明书

     

可嵌入的附加要求   类在本节中描述   34年9月1日。

JPA 2.0增加了对嵌入对象集合的支持:

  

2.6可嵌入类和基本类型的集合

     

持久字段或属性   实体或可嵌入的类可以   对应一个基本的集合   类型或可嵌入类(“元素   采集”)。这样的集合何时   如此指定   ElementCollection注释是   通过集合表映射,   如第11.1.8节中所定义。如果   ElementCollection注释(或XML   等效的)没有指定   收藏价值的领域或财产,   第2.8节的规则适用。

     

一个可嵌入的类(包括一个   另一个可嵌入的类   包含的可嵌入类)   在元素集合中不得   包含元素集合,也可以   它包含与实体的关系   除了多对一或一对一   关系。可嵌入的类   必须是这样一个人的拥有方   关系和关系必须   由外键映射映射。   (见2.9节)

但我仍然认为你不能映射一个Embeddable数组。