Hibernate : java.lang.IllegalArgumentException: 对象不是声明 class 的实例

Hibernate : java.lang.IllegalArgumentException: object is not an instance of declaring class

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

我要实体:

AttributeEntity 可以有一个或多个 taxonomyEntities。

属性实体约束:

TaxonomyEntity 约束:

我想做什么?

创建这两个实体之间的关系,以便我可以 create/delete 整个树结构(Attribue 及其分类法)。喜欢 attributeEntity.save() 或 attributeEntity.delete()

问题:

使用下面的测试,当我试图保存 attributeEntity 时,hibernate 保存了数据。但是当使用 taxonomyEntity 列表作为 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;
    }


}

我的分类实体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;
    }


}

这是我尝试测试的方式:

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

这是我看到的错误:

提前致谢。

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

@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 查看第 7.2.2 节以获得完整文档。而且我认为您尝试映射的错误是 ManyToOne 而不是 OneToMany

"attributeCd" 是 ATTRIBUTE table 中的外键,但不是 TAXONOMY table 中的主键。 Hibernate 不支持那个。