-->
These old forums are deprecated now and set to read-only. We are waiting for you on our new forums!
More modern, Discourse-based and with GitHub/Google/Twitter authentication built-in.

All times are UTC - 5 hours [ DST ]



Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 4 posts ] 
Author Message
 Post subject: object is not an instance of declaring class
PostPosted: Fri Dec 09, 2016 6:22 pm 
Newbie

Joined: Fri Dec 09, 2016 6:11 pm
Posts: 2
I am using hibernate to handle database transactions. I am new in hibernate.

I have to entities :

- AttributeEntity
- TaxonomyEntity

AttributeEntity can have one or more taxonomyEntities.

AttributeEntity constraints:


- attributeId is the primary key
- attributeCd is unique

TaxonomyEntity constraints:

- taxonomyId is the primary key
- attributeCd is the foreign key to the table "Attribute"

What am I trying to do?

Create the relationships between these two entities so that if I can create/delete the whole tree structure ( Attribue and it's taxonomies). Like attributeEntity.save() or attributeEntity.delete()

The problem:

Using the test below, when I am trying to save the attributeEntity,hibernate saves the data. But when using a list of taxonomyEntity as part of AttributeEntity, I am seeing the error described below. **May I get any insight on the reason of the error?**



My AttributeEntity class:


Code:
   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;
      }

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

      public void setAttributeCd( String attributeCd )
      {
         this.attributeCd = attributeCd;
      }[/b]

      

   
      [b]@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;
      }[/b]


   }



My TaxonomyEntity class


Code:
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;
      }

      [b]@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;
      }[/b]

      @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;
      }

      
   }


This is the way I am trying to test:

Code:
      //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);



This is the error I am seeing :


- IllegalArgumentException occurred calling getter of com.myplace.online.attribute.api.entities.AttributeEntity.attributeCd
- and then towards the end : Caused by: java.lang.IllegalArgumentException: object is not an instance of declaring class


org.hibernate.PropertyAccessException: IllegalArgumentException occurred calling getter of com.myplace.online.attribute.api.entities.AttributeEntity.attributeCd


Code:
   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)
      [b] Caused by: java.lang.IllegalArgumentException: object is not an instance of declaring class[/b]
      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)


Top
 Profile  
 
 Post subject: Re: object is not an instance of declaring class
PostPosted: Sun Dec 11, 2016 2:30 am 
Hibernate Team
Hibernate Team

Joined: Thu Sep 11, 2014 2:50 am
Posts: 1628
Location: Romania
In ToxonomyEntity, change attributeCd property as follows:

Code:
     
      @ManyToOne
      @JoinColumn(name = "ATTRIBUTE_CD", length = 6,  nullable = false)
      public AttributeEntity getAttributeEntity()
      {
         return attributeEntity;
      }


In AttributeEntity, you need to:

1. Remove the attributeCd property
2. Change the taxonomyEntities property to:

Code:
     
      @OneToMany(cascade = CascadeType.ALL, fetch = FetchType.LAZY, orphanRemoval = true, mappedBy="attributeEntity")
      public List<TaxonomyEntity> getTaxonomyEntities()
      {
         return taxonomyEntities;
      }


The data access logic becomes

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

        //Create Taxomony
        taxonomyEntity = new TaxonomyEntity();
        taxonomyEntity.setDescription("A test taxonomy");
        taxonomyEntity.setTaxonomyCategory("123123123CategoryTest");
        taxonomyEntity.setTaxonomySubcategory("123123123SubcategoryTest");
        taxonomyEntity.setAttributeEntity(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);



Top
 Profile  
 
 Post subject: Re: object is not an instance of declaring class
PostPosted: Mon Dec 12, 2016 11:11 am 
Newbie

Joined: Fri Dec 09, 2016 6:11 pm
Posts: 2
It worked . Thanks so much vlad


Top
 Profile  
 
 Post subject: Re: object is not an instance of declaring class
PostPosted: Mon Dec 12, 2016 11:30 am 
Hibernate Team
Hibernate Team

Joined: Thu Sep 11, 2014 2:50 am
Posts: 1628
Location: Romania
You're welcome.


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 4 posts ] 

All times are UTC - 5 hours [ DST ]


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum

Search for:
© Copyright 2014, Red Hat Inc. All rights reserved. JBoss and Hibernate are registered trademarks and servicemarks of Red Hat, Inc.