1

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:

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


}

My TaxonomyEntity class

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


}

This is the way I am trying to test:

    //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
    
    
    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)
    

Thanks in advance.

2 Answers 2

1

"attributeCd" was foreign key in ATTRIBUTE table but was not primary key in TAXONOMY table. Hibernate does not support that.

Sign up to request clarification or add additional context in comments.

Comments

0

This is one way of definning OneToMany in Hibernate

@OneToMany(mappedBy="entitie")
@OrderBy("theFieldYouWantOrder ASC")

Lists can be mapped in two different ways: as ordered lists, where the order is not materialized in the database as indexed lists, where the order is materialized in the database To order lists in memory, add @javax.persistence.OrderBy to your property. This annotation takes as parameter a list of comma separated properties (of the target entity) and orders the collection accordingly (eg firstname asc, age desc), if the string is empty, the collection will be ordered by the primary key of the target entity.

The documentation of Hibernate check Section 7.2.2 for the full documentation. And I think the error you are Trying to map like ManyToOne instead of OneToMany

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.