Hibernate not-null属性引用null或transient值异常

时间:2011-12-26 11:47:39

标签: hibernate hibernate-mapping

我是hibernate的新手,并尝试为此表创建一个hibernate文件:

brand_id | brand_name | status
int,PK,AI  varchar(100) | bool

我的hibernate文件是:

<hibernate-mapping>
  <class name="Brand" table="brand" catalog="aCatalog">
    <id name="brandId" type="java.lang.Integer">
        <column name="brand_id" />
        <generator class="identity" />
    </id>
    <property name="brandName" type="string">
        <column name="brand_name" length="100" not-null="true"/>
    </property>
    <property name="status" type="boolean">
            <column name="status"/>
    </property>
  </class>
</hibernate-mapping>

当brandName的not-null属性设置为true时,我在异常后得到异常:

org.hibernate.PropertyValueException: not-null property references a null or transient value: com.bla.pojos.Brand.status
    at org.hibernate.engine.Nullability.checkNullability(Nullability.java:72)
    at org.hibernate.event.def.AbstractSaveEventListener.performSaveOrReplicate(AbstractSaveEventListener.java:290)
    at org.hibernate.event.def.AbstractSaveEventListener.performSave(AbstractSaveEventListener.java:181)
    at org.hibernate.event.def.AbstractSaveEventListener.saveWithGeneratedId(AbstractSaveEventListener.java:107)
    at org.hibernate.event.def.DefaultSaveOrUpdateEventListener.saveWithGeneratedOrRequestedId(DefaultSaveOrUpdateEventListener.java:187)
    at org.hibernate.event.def.DefaultSaveEventListener.saveWithGeneratedOrRequestedId(DefaultSaveEventListener.java:33)
    at org.hibernate.event.def.DefaultSaveOrUpdateEventListener.entityIsTransient(DefaultSaveOrUpdateEventListener.java:172)
    at org.hibernate.event.def.DefaultSaveEventListener.performSaveOrUpdate(DefaultSaveEventListener.java:27)
    at org.hibernate.event.def.DefaultSaveOrUpdateEventListener.onSaveOrUpdate(DefaultSaveOrUpdateEventListener.java:70)
    at org.hibernate.impl.SessionImpl.fireSave(SessionImpl.java:535)
    at org.hibernate.impl.SessionImpl.save(SessionImpl.java:523)
    at org.hibernate.impl.SessionImpl.save(SessionImpl.java:519)

当我将其设置为false或在那里没有写任何内容时,我将使用空值保存我的数据。我做错了什么?

编辑:

支持Bean:

@ManagedBean(name="brandSettingsBean")
@ViewScoped
public class BrandSettingsBean extends EBSBean implements Serializable {

    BrandSettingsService bsService = new BrandSettingsService();

    private Brand brand=new Brand();
    private String brandName;
    private Brand selectedBrand;
    private List<Brand> BrandList;

    public Brand getSelectedBrand() {
        return selectedBrand;
    }

public void setSelectedBrand(Brand selectedBrand) {
        this.selectedBrand = selectedBrand;
    }
public BrandSettingsBean() throws Exception {

        brandList=bsService.getBrands(null);
    }


 public void brandKaydet() {
        EBSResponse er = kiService.brandKaydet(brand, sessionUser, ipAdresi);

        if (er.getExceptionValue() == null) {
            this.brandList = (List<Brand>)er.getObjectValue();
            FacesMessage message = new FacesMessage(FacesMessage.SEVERITY_INFO, localeBean.lang.getString(er.getStringValue()), "");
            FacesContext.getCurrentInstance().addMessage("sysMsg", message);
        } else {
            FacesMessage message = new FacesMessage(EBSUtils.getHataTipi(er.getExceptionValue().getHataTipi()), localeBean.lang.getString(er.getExceptionValue().getHataMesaji()), "");
            FacesContext.getCurrentInstance().addMessage("sysMsg", message);
        }

        brand=new Brand();
    }

XHTML:

<p:inputText size="30" maxlength="50" required="true" requiredMessage="#{lang.brand_req_txt}" value="#{BrandSettingsBean.selectedBrand.brandName}"/> 

1 个答案:

答案 0 :(得分:3)

听起来你告诉Hibernate brandName不能为null,然后在保存之前不设置它。我们能看到生成附加堆栈跟踪的代码来确认吗?