Java org.hibernate.PropertyValueException: 非空属性引用了空值或瞬态值:

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/20565400/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-13 02:51:37  来源:igfitidea点击:

org.hibernate.PropertyValueException: not-null property references a null or transient value:

javahibernate

提问by rahul

I want to save an object to my sub-class ArticleZoning whose super class Zoning contain a List of Class zoneData which also contain a class ZoneCoordinate. When I save the object of my sub-class ArticleZoning it gives an exception.

我想将一个对象保存到我的子类 ArticleZoning 中,其超类 Zoning 包含一个类 zoneData 列表,其中还包含一个类 ZoneCoordinate。当我保存我的子类 ArticleZoning 的对象时,它给出了一个例外。

org.hibernate.PropertyValueException: not-null property references a null or transient value: com.qait.cdl.eon.commons.domain.ZoneData._com.qait.cdl.eon.commons.domain.Zonning.zoneDatasBackref
at org.hibernate.engine.Nullability.checkNullability(Nullability.java:101)
at org.hibernate.event.def.AbstractSaveEventListener.performSaveOrReplicate(AbstractSaveEventListener.java:313)
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).....

Here is *Zonning hbm*Mapping file:-

这是 * Zonning hbm*映射文件:-

    <class name="Zonning" table="zoning">
<id name="id" column="id" type="long">
        <generator class="native" />
    </id>

<list name="zoneDatas" lazy="false" cascade="all-delete-orphan" >
        <key column="zoning_id" not-null="true"/>
        <list-index column="idx" base="1" />
        <one-to-many class="com.qait.cdl.eon.commons.domain.ZoneData" />
</list>

<many-to-one class="com.qait.cdl.eon.commons.domain.MagazineIssue" unique="true" column="issue_id" name="issue"/>

<property name="pageNumber" column="article_on_pageNumber" type="string" not-null="true" />


    <joined-subclass name="ArticleZoning" extends="Zonning" table="article_zoning">

        <key column="article_id"/>
        <property name="articleTitle" column="article_title" type="string" not-null="true" />
        <property name="articleOrder" column="article_order" type="int" not-null="true" />
        <property name="articleFileId" column="article_file_id" type="string" not-null="true" />


        <property name="articleType" column="article_type">
            <type name="org.hibernate.type.EnumType">
                <param name="type">12</param>
                <param name="enumClass">com.qait.cdl.eon.common.constants.ArticleType</param>
            </type>
        </property>

        <property name="articleSubTitle" column="article_sub_title" type="string" not-null="true" />
        <property name="articleGenre" column="article_genre">
            <type name="org.hibernate.type.EnumType">
                <param name="type">12</param>
                <param name="enumClass">com.qait.cdl.eon.common.constants.Genre</param>
            </type>
        </property>

    </joined-subclass>

    <joined-subclass name="AdvertisementZoning" extends="Zonning" table="advertisement_zoning">

        <key column="advertisement_id" />
        <property name="adVendor" column="ad_vendor" type="string" not-null="true" />
        <property name="vendorUrl" column="vendor_url" type="string" not-null="true" />
        <property name="adProduct" column="ad_product" type="string" not-null="true" />
        <list name="adKeywords" table="ad_keywords" lazy="false" cascade="all">
            <key column="ad_keywords_id" />
            <list-index base="0" column="idx"/>
            <element column="keywords" type="string" />
        </list>
    </joined-subclass>

</class>

Here is ZoneData Hbm

这是ZoneData Hbm

    <id name="id" column="id" type="long">
        <generator class="native" />
    </id>
    <property name = "zoneOrder"  column = "zone_order"   type = "int"    not-null="true"/>
    <property name = "zoneFileId" column = "zone_file_id" type = "string" not-null="true"/>
    <property name = "zoneShape"  column = "zone_shape"   type = "string" not-null="true" access="field"></property>

     <many-to-one  name="coordinates"  column="coordinates_id" lazy="false" class="com.qait.cdl.eon.commons.domain.ZoneCoordinates" 
     unique="true"  not-null="true" cascade="all-delete-orphan"/>

</class>

Here is ZoneCoordinate hbm

这是ZoneCoordinate hbm

<class name="ZoneCoordinates" table="zone_coordinates">

        <id name="id" column="id" type="long">
            <generator class="native" />
        </id>
        <property name = "leftTopX"     column = "left_top_x"     type = "float" not-null="true" />
        <property name = "leftTopY"     column = "left_top_y"     type = "float" not-null="true" />
        <property name = "rightBottomX" column = "right_bottom_x" type = "float" not-null="true" />
        <property name = "rightBottomY" column = "right_bottom_y" type = "float" not-null="true" />
    </class>

Here is Zoning pojo

这是Zoning pojo

  class Zoning{
    private List<ZoneData> zoneDatas =new ArrayList<>();
    private MagazineIssue issue;
    private String pageNumber;
    //getter and setter
    }

Here is ZoneData POJO

这是ZoneData POJO

class ZoneData{
    private int zoneOrder;
    private String zoneFileId ;
    private ZoneCoordinates coordinates;
    private final String zoneShape = "RECT";
    //getter and setter
}

Here is ArticleZoning POJO

这是文章Zoning POJO

class ArticleZoning extends Zoning{ 
private String articleTitle;
private String articleOrder;
private ArticleType articleType;
private String articleFileId;
private String articleSubTitle;
private Genre articleGenre;
//getter and setter
}

Here is ZoneCoordinate POJO

这是ZoneCoordinate POJO

class ZoneCoordinate{ 

    private float leftTopX;
    private float leftTopY;
    private float rightBottomX;
    private float rightBottomY;
    //getter and setter
}

采纳答案by gursahib.singh.sahni

First, ArticleZoningPOJO has articleOrder as String type. Your Zonning.hbmsays articleOrderis of int type. Secondly, as zoning table is unable to save, hence its foreign key is null.

首先,ArticleZoningPOJO 将 articleOrder 作为 String 类型。你Zonning.hbm说的articleOrder是 int 类型。其次,由于分区表无法保存,因此其外键为空。