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

I am using Java,Hibernate and MySQL 5.1.52-log version.

My interpretation of default is if I don't supply value for the column, Hibernate will insert the default value when I try to save this object.

HBM file,

<property name="isActive" type="java.lang.Short">
        <column name="IsActive" not-null="true" default="1"/>
</property>

3 Answers 3

1

According to the documentation of PropertyValueException, if you set a null value in a property declared not-null="true" and then try to persist/update it, then you will get this exception.

See http://docs.jboss.org/hibernate/orm/3.5/javadoc/org/hibernate/PropertyValueException.html

You should remove the not-null="true" attribute from the config (because the default value of not-null is false) and then the database should insert the default value that you specified in the configuration for you (in this case '1').

[UPDATE]

I would have preferred to put this as a comment in fujy's answer but I am not allowed to comment on other answers yet. Does it work as expected if you remove the 'not-null' attribute (As I am showing below)? I believe that is what is causing the exception that you are getting.

 <property name="isActive" type="java.lang.Short">
     <meta attribute="default-value">1</meta>
     <column name="IsActive" />
 </property>
Sign up to request clarification or add additional context in comments.

1 Comment

I recommend you to read fujy's answer first since I've never used hibernate xml configuration before. I could be wrong :)
0

You could simply set it on your model directly

 private Short isActive = 1;

Or you could try this in your hbm file

 <property name="isActive" type="java.lang.Short">
     <meta attribute="default-value">1</meta>
     <column name="IsActive" not-null="true"/>
 </property>

2 Comments

Do you have any annotations ( or other settings ) on isActive field?
Nope, I am not using annotations instead I am using HBM file. and I mentioned in my original post how I have defined the 'isActive' field.
0

If anyone else is wondering an answer for this question, by using the @Column annotations, you can do the following as in the question above.

@Column(name = "IsActive", nullable = false, insertable = false)

private Short isActive;

the insertable property will not include this field in INSERT queries. Then the server will automatically put the default value assigned in the database itself.

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.