Hibernate:java.lang.IllegalArgumentException:object不是声明类

时间:2016-12-09 17:45:49

标签: java hibernate hibernate-mapping

我正在使用hibernate来处理数据库事务。我是休眠的新手。

我必须要实体:

  • AttributeEntity
  • TaxonomyEntity

AttributeEntity可以有一个或多个taxonomyEntities。

AttributeEntity约束:

  • attributeId是主键
  • attributeCd是唯一的

TaxonomyEntity约束:

  • taxonomyId是主键
  • attributeCd是表的外键"属性"

我想做什么?

创建这两个实体之间的关系,以便我可以创建/删除整个树结构(Attribue和它的分类法)。与attributeEntity.save()或attributeEntity.delete()

类似

问题:

使用下面的测试,当我尝试保存attributeEntity时,hibernate会保存数据。但是当使用taxonomyEntity列表作为AttributeEntity的一部分时,我看到下面描述的错误。 我是否可以了解错误原因?

我的AttributeEntity类:

package com.myplace.online.attribute.api.entities;

import java.io.Serializable;
import java.sql.Time;
import java.util.ArrayList;
import java.util.Collection;
import java.util.List;

import javax.persistence.*;


@Entity
@SequenceGenerator( name = "attribute_seq", sequenceName = "OAP_META_NEW.ATTRIBUTE_SEQ" )
@Table(name = "ATTRIBUTE", schema = "OAP_META_NEW")
public class AttributeEntity implements Serializable
{
    private int attributeId;
    private Integer attributeCategoryId;
    private String attributeName;
    private String attributeCd;

    private List<TaxonomyEntity> taxonomyEntities;


    @Id
    @Column(name = "ATTRIBUTE_ID", nullable = false, unique = true)
    @GeneratedValue( strategy = GenerationType.AUTO, generator = "attribute_seq" )
    public int getAttributeId()
    {
        return attributeId;
    }

    public void setAttributeId( int attributeId )
    {
        this.attributeId = attributeId;
    }

    @Basic
    @Column(name = "ATTRIBUTE_CATEGORY_ID", nullable = false, precision = 0)
    @JoinColumn(name = "ATTRIBUTE_CATEGORY_ID", referencedColumnName = "ATTRIBUTE_CATEGORY_ID")
    public Integer getAttributeCategoryId()
    {
        return attributeCategoryId;
    }

    public void setAttributeCategoryId( Integer attributeCategoryId )
    {
        this.attributeCategoryId = attributeCategoryId;
    }

    @Basic
    @Column(name = "ATTRIBUTE_NAME", nullable = false, length = 70)
    public String getAttributeName()
    {
        return attributeName;
    }

    public void setAttributeName( String attributeName )
    {
        this.attributeName = attributeName;
    }


    @Basic
    @Column(name = "ATTRIBUTE_CD", nullable = false, length = 6)
    public String getAttributeCd()
    {
        return attributeCd;
    }

    public void setAttributeCd( String attributeCd )
    {
        this.attributeCd = attributeCd;
    }




    @OneToMany(cascade = CascadeType.ALL, fetch = FetchType.LAZY, orphanRemoval = true)
    @JoinColumn( name = "ATTRIBUTE_CD", referencedColumnName = "ATTRIBUTE_CD", nullable = false )
    public List<TaxonomyEntity> getTaxonomyEntities()
    {
        return taxonomyEntities;
    }

    public void setTaxonomyEntities(List<TaxonomyEntity> taxonomyEntities)
    {
        this.taxonomyEntities = taxonomyEntities;
    }


}

我的TaxonomyEntity类

package com.myplace.online.attribute.api.entities;

import java.io.Serializable;
import java.util.List;

import javax.persistence.*;


@Entity
@SequenceGenerator( name = "taxonomy_seq", sequenceName = "OAP_META_NEW.TAXONOMY_SEQ" )
@Table(name = "TAXONOMY", schema = "OAP_META_NEW")
public class TaxonomyEntity implements Serializable
{
    private int taxonomyId;
    private String attributeCd;
    private String taxonomyCategory;
    private String taxonomySubcategory;
    private String taxonomyAttributeName;


    @Id
    @Column(name = "TAXONOMY_ID", nullable = false, unique = true)
    @GeneratedValue( strategy = GenerationType.AUTO, generator = "taxonomy_seq" )
    public int getTaxonomyId()
    {
        return taxonomyId;
    }

    public void setTaxonomyId( int taxonomyId )
    {
        this.taxonomyId = taxonomyId;
    }

    @Basic
    @Column(name = "ATTRIBUTE_CD", length = 6,  nullable = false, insertable = false ,updatable = false)
    public String getAttributeCd() 
    {
        return attributeCd;
    }

    public void setAttributeCd( String attributeCd ) 
    {
        this.attributeCd = attributeCd;
    }

    @Basic
    @Column(name = "TAXONOMY_CATEGORY", nullable = true, length = 100)
    public String getTaxonomyCategory() 
    {
        return taxonomyCategory;
    }

    public void setTaxonomyCategory( String taxonomyCategory ) 
    {
        this.taxonomyCategory = taxonomyCategory;
    }

    @Basic
    @Column(name = "TAXONOMY_SUBCATEGORY", nullable = true, length = 100)
    public String getTaxonomySubcategory() 
    {
        return taxonomySubcategory;
    }

    public void setTaxonomySubcategory( String taxonomySubcategory ) 
    {
        this.taxonomySubcategory = taxonomySubcategory;
    }

    @Basic
    @Column(name = "TAXONOMY_ATTRIBUTE_NAME", nullable = false, length = 100)
    public String getTaxonomyAttributeName() 
    {
        return taxonomyAttributeName;
    }

    public void setTaxonomyAttributeName( String taxonomyAttributeName ) 
    {
        this.taxonomyAttributeName = taxonomyAttributeName;
    }


}

这是我尝试测试的方式:

    //create attribute
    attributeEntity = new AttributeEntity();
    attributeEntity.setAttributeCategoryId(0);
    attributeEntity.setAttributeCd("E_TEST");
    attributeEntity.setAttributeId(0);
    attributeEntity.setAttributeName("TEST_ATTRIBUTE");

    //Create Taxomony
    taxonomyEntity = new TaxonomyEntity();
   // taxonomyEntity.setTaxonomyId(0); -- expecting this to be created by the sequence
    taxonomyEntity.setDescription("A test taxonomy");
    taxonomyEntity.setTaxonomyCategory("123123123CategoryTest");
    taxonomyEntity.setTaxonomySubcategory("123123123SubcategoryTest");
   // taxonomyEntity.setAttributeCd(attributeEntity.getAttributeCd()); -- Expecting this to be created from AttributeEntity
    taxonomyEntity.setTaxonomyAttributeName("TEST_TAXONOMY_ATTRIBUTE");


    List<TaxonomyEntity> taxonomyList = new ArrayList<TaxonomyEntity>();
    taxonomyList.add(taxonomyEntity);

    attributeEntity.setTaxonomyEntities(taxonomyList);

    attributeService = context.getBean( AttributeService.class );
    attributeService.saveAttributeEntity(attributeEntity);

这是我看到的错误:

  • 调用getter of com.myplace.online.attribute.api.entities.AttributeEntity.attributeCd
  • 时发生IllegalArgumentException
  • 然后到最后:引起:java.lang.IllegalArgumentException:object不是声明类的实例

    org.hibernate.PropertyAccessException: IllegalArgumentException occurred calling getter of com.myplace.online.attribute.api.entities.AttributeEntity.attributeCd
    
    
    at org.hibernate.property.BasicPropertyAccessor$BasicGetter.get(BasicPropertyAccessor.java:192)
    at org.hibernate.tuple.component.AbstractComponentTuplizer.getPropertyValue(AbstractComponentTuplizer.java:76)
    at org.hibernate.tuple.component.AbstractComponentTuplizer.getPropertyValues(AbstractComponentTuplizer.java:82)
    at org.hibernate.tuple.component.PojoComponentTuplizer.getPropertyValues(PojoComponentTuplizer.java:107)
    at org.hibernate.type.ComponentType.getPropertyValues(ComponentType.java:433)
    at org.hibernate.type.ComponentType.getPropertyValues(ComponentType.java:421)
    at org.hibernate.event.internal.WrapVisitor.processComponent(WrapVisitor.java:135)
    at org.hibernate.event.internal.AbstractVisitor.processValue(AbstractVisitor.java:127)
    at org.hibernate.event.internal.WrapVisitor.processValue(WrapVisitor.java:125)
    at org.hibernate.event.internal.AbstractVisitor.processEntityPropertyValues(AbstractVisitor.java:76)
    at org.hibernate.event.internal.AbstractSaveEventListener.visitCollectionsBeforeSave(AbstractSaveEventListener.java:372)
    at org.hibernate.event.internal.AbstractSaveEventListener.performSaveOrReplicate(AbstractSaveEventListener.java:273)
    at org.hibernate.event.internal.AbstractSaveEventListener.performSave(AbstractSaveEventListener.java:194)
    at org.hibernate.event.internal.AbstractSaveEventListener.saveWithGeneratedId(AbstractSaveEventListener.java:137)
    at org.hibernate.event.internal.DefaultSaveOrUpdateEventListener.saveWithGeneratedOrRequestedId(DefaultSaveOrUpdateEventListener.java:209)
    at org.hibernate.event.internal.DefaultSaveOrUpdateEventListener.entityIsTransient(DefaultSaveOrUpdateEventListener.java:194)
    at org.hibernate.event.internal.DefaultSaveOrUpdateEventListener.performSaveOrUpdate(DefaultSaveOrUpdateEventListener.java:114)
    at org.hibernate.event.internal.DefaultSaveOrUpdateEventListener.onSaveOrUpdate(DefaultSaveOrUpdateEventListener.java:90)
    at org.hibernate.internal.SessionImpl.fireSaveOrUpdate(SessionImpl.java:684)
    at org.hibernate.internal.SessionImpl.saveOrUpdate(SessionImpl.java:676)
    at org.hibernate.engine.spi.CascadingActions$5.cascade(CascadingActions.java:235)
    at org.hibernate.engine.internal.Cascade.cascadeToOne(Cascade.java:350)
    at org.hibernate.engine.internal.Cascade.cascadeAssociation(Cascade.java:293)
    at org.hibernate.engine.internal.Cascade.cascadeProperty(Cascade.java:161)
    at org.hibernate.engine.internal.Cascade.cascadeCollectionElements(Cascade.java:379)
    at org.hibernate.engine.internal.Cascade.cascadeCollection(Cascade.java:319)
    at org.hibernate.engine.internal.Cascade.cascadeAssociation(Cascade.java:296)
    at org.hibernate.engine.internal.Cascade.cascadeProperty(Cascade.java:161)
    at org.hibernate.engine.internal.Cascade.cascade(Cascade.java:118)
    at org.hibernate.event.internal.AbstractSaveEventListener.cascadeAfterSave(AbstractSaveEventListener.java:460)
    at org.hibernate.event.internal.AbstractSaveEventListener.performSaveOrReplicate(AbstractSaveEventListener.java:294)
    at org.hibernate.event.internal.AbstractSaveEventListener.performSave(AbstractSaveEventListener.java:194)
    at org.hibernate.event.internal.AbstractSaveEventListener.saveWithGeneratedId(AbstractSaveEventListener.java:137)
    at org.hibernate.event.internal.DefaultSaveOrUpdateEventListener.saveWithGeneratedOrRequestedId(DefaultSaveOrUpdateEventListener.java:209)
    at org.hibernate.event.internal.DefaultSaveEventListener.saveWithGeneratedOrRequestedId(DefaultSaveEventListener.java:55)
    at org.hibernate.event.internal.DefaultSaveOrUpdateEventListener.entityIsTransient(DefaultSaveOrUpdateEventListener.java:194)
    at org.hibernate.event.internal.DefaultSaveEventListener.performSaveOrUpdate(DefaultSaveEventListener.java:49)
    at org.hibernate.event.internal.DefaultSaveOrUpdateEventListener.onSaveOrUpdate(DefaultSaveOrUpdateEventListener.java:90)
    at org.hibernate.internal.SessionImpl.fireSave(SessionImpl.java:715)
    at org.hibernate.internal.SessionImpl.save(SessionImpl.java:707)
    at org.hibernate.internal.SessionImpl.save(SessionImpl.java:702)
    at com.myplace.online.attribute.api.dao.GenericDAOImpl.save(GenericDAOImpl.java:85)
    at com.myplace.online.attribute.api.services.AttributeServiceImpl.saveAttributeEntity(AttributeServiceImpl.java:41)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke(Method.java:497)
    at org.springframework.aop.support.AopUtils.invokeJoinpointUsingReflection(AopUtils.java:302)
    at org.springframework.aop.framework.ReflectiveMethodInvocation.invokeJoinpoint(ReflectiveMethodInvocation.java:190)
    at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:157)
    at org.springframework.transaction.interceptor.TransactionInterceptor$1.proceedWithInvocation(TransactionInterceptor.java:99)
    at org.springframework.transaction.interceptor.TransactionAspectSupport.invokeWithinTransaction(TransactionAspectSupport.java:281)
    at org.springframework.transaction.interceptor.TransactionInterceptor.invoke(TransactionInterceptor.java:96)
    at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:179)
    at org.springframework.aop.framework.JdkDynamicAopProxy.invoke(JdkDynamicAopProxy.java:208)
    at com.sun.proxy.$Proxy39.saveAttributeEntity(Unknown Source)
    at com.myplace.online.attribute.api.TaxonomyTest.setUpEntity(TaxonomyTest.java:97)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke(Method.java:497)
    at org.testng.internal.MethodInvocationHelper.invokeMethod(MethodInvocationHelper.java:80)
    at org.testng.internal.Invoker.invokeConfigurationMethod(Invoker.java:564)
    at org.testng.internal.Invoker.invokeConfigurations(Invoker.java:213)
    at org.testng.internal.Invoker.invokeConfigurations(Invoker.java:138)
    at org.testng.internal.TestMethodWorker.invokeBeforeClassMethods(TestMethodWorker.java:175)
    at org.testng.internal.TestMethodWorker.run(TestMethodWorker.java:107)
    at org.testng.TestRunner.privateRun(TestRunner.java:767)
    at org.testng.TestRunner.run(TestRunner.java:617)
    at org.testng.SuiteRunner.runTest(SuiteRunner.java:334)
    at org.testng.SuiteRunner.runSequentially(SuiteRunner.java:329)
    at org.testng.SuiteRunner.privateRun(SuiteRunner.java:291)
    at org.testng.SuiteRunner.run(SuiteRunner.java:240)
    at org.testng.SuiteRunnerWorker.runSuite(SuiteRunnerWorker.java:52)
    at org.testng.SuiteRunnerWorker.run(SuiteRunnerWorker.java:86)
    at org.testng.TestNG.runSuitesSequentially(TestNG.java:1198)
    at org.testng.TestNG.runSuitesLocally(TestNG.java:1123)
    at org.testng.TestNG.run(TestNG.java:1031)
    at org.testng.IDEARemoteTestNG.run(IDEARemoteTestNG.java:72)
    at org.testng.RemoteTestNGStarter.main(RemoteTestNGStarter.java:124)
    **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:62)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke(Method.java:497)
    at org.hibernate.property.BasicPropertyAccessor$BasicGetter.get(BasicPropertyAccessor.java:169)
    

提前致谢。

2 个答案:

答案 0 :(得分:0)

这是在Hibernate中定义OneToMany的一种方法

$transactionRequestType->addToTransactionSettings($duplicateWindowSetting);
$transactionRequestType->addToTransactionSettings($allowPartialAuthSetting);
  

列表可以通过两种不同的方式进行映射:   作为有序列表,其中订单未在数据库中实现   作为索引列表,其中订单在数据库中实现   要在内存中订购列表,请将@ javax.persistence.OrderBy添加到您的   属性。此批注将逗号分隔的列表作为参数   属性(目标实体)并对集合进行排序   相应的(例如,firstname asc,age desc),如果字符串为空,则为   集合将按目标实体的主键排序。

The documentation of Hibernate查看第7.2.2节的完整文档。我认为您尝试映射的错误像ManyToOne而不是OneToMany

答案 1 :(得分:0)

“attributeCd”是ATTRIBUTE表中的外键,但不是TAXONOMY表中的主键。 Hibernate不支持这一点。