-->
These old forums are deprecated now and set to read-only. We are waiting for you on our new forums!
More modern, Discourse-based and with GitHub/Google/Twitter authentication built-in.

All times are UTC - 5 hours [ DST ]



Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 14 posts ] 
Author Message
 Post subject: Pls Help: "Null value was assigned to a property of pri
PostPosted: Sun Sep 25, 2005 11:02 pm 
Beginner
Beginner

Joined: Sun Jul 31, 2005 1:23 pm
Posts: 35
I have a class that is generating this Hibernate exception. If anyone could spot my error, I would greatly appreciate it. This is with Hibernate 3.0.5

Exception trace:
Code:
Caused by: org.hibernate.PropertyAccessException: Null value was assigned to a property of primitive type setter of com.auenrec.data.Rating.enabled
   at org.hibernate.property.BasicPropertyAccessor$BasicSetter.set(BasicPropertyAccessor.java:59)
   at org.hibernate.tuple.AbstractTuplizer.setPropertyValues(AbstractTuplizer.java:207)
   at org.hibernate.tuple.PojoTuplizer.setPropertyValues(PojoTuplizer.java:176)
   at org.hibernate.persister.entity.BasicEntityPersister.setPropertyValues(BasicEntityPersister.java:2919)
   at org.hibernate.engine.TwoPhaseLoad.initializeEntity(TwoPhaseLoad.java:113)
   at org.hibernate.loader.Loader.initializeEntitiesAndCollections(Loader.java:530)
   at org.hibernate.loader.Loader.doQuery(Loader.java:436)
   at org.hibernate.loader.Loader.doQueryAndInitializeNonLazyCollections(Loader.java:218)
   at org.hibernate.loader.Loader.loadEntity(Loader.java:1345)
(snip)
   ... 54 more
Caused by: java.lang.IllegalArgumentException
   at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
   at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
   at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
   at java.lang.reflect.Method.invoke(Method.java:585)
   at org.hibernate.property.BasicPropertyAccessor$BasicSetter.set(BasicPropertyAccessor.java:40)
   ... 81 more


Relevant code from Rating.java:
Code:
   // Internal instance variable declaration
   private boolean enabled = true;

   // Accessors
   public boolean getEnabled() { return enabled; }
   public void setEnabled(boolean enabled) { this.enabled = enabled; }


Relevant code from mapping file:
Code:
    <class name="com.auenrec.data.Rating" table="Ratings">
       <composite-id>
         <key-many-to-one name="user" column="UserID"
               class="com.auenrec.data.GenericUser"/>
         <key-many-to-one name="item" column="RatableItemID"
               class="com.auenrec.data.RatableItem"/>
      </composite-id>

        <property name="originalRating" column="OriginalRating" type="float"/>
        <property name="normalizedRating" column="NormalizedRating" type="float"/>
        <property name="ratingDate" column="RatingDate" type="timestamp"/>
        <property name="enabled" column="Enabled" type="boolean"/>
    </class>


MySQL table description:
Code:
+------------------+------------+------+-----+---------+-------+
| Field            | Type       | Null | Key | Default | Extra |
+------------------+------------+------+-----+---------+-------+
| UserID           | int(11)    |      | PRI | 0       |       |
| RatableItemID    | int(11)    |      | PRI | 0       |       |
| OriginalRating   | float      | YES  |     | NULL    |       |
| NormalizedRating | float      | YES  |     | NULL    |       |
| RatingDate       | datetime   | YES  |     | NULL    |       |
| Enabled          | tinyint(1) | YES  |     | NULL    |       |
+------------------+------------+------+-----+---------+-------+


Top
 Profile  
 
 Post subject:
PostPosted: Sun Sep 25, 2005 11:27 pm 
Expert
Expert

Joined: Mon Jul 04, 2005 5:19 pm
Posts: 720
The Enabled column is null by default. When retrieving an instance, null can be passed to setEnabled() . This fails because the pojo has a primitive boolean. Change this to Boolean, which can take a null value, so that your object model matches your data model - or, change the data model so that the default value for that column is 1 or 0 .


Top
 Profile  
 
 Post subject:
PostPosted: Mon Sep 26, 2005 9:28 am 
Beginner
Beginner

Joined: Sun Jul 31, 2005 1:23 pm
Posts: 35
dennisbyrne wrote:
The Enabled column is null by default. When retrieving an instance, null can be passed to setEnabled() . This fails because the pojo has a primitive boolean. Change this to Boolean, which can take a null value, so that your object model matches your data model - or, change the data model so that the default value for that column is 1 or 0 .


Thanks!

Can I change the hbm.xml mapping file to indicate a default value for a column/property? Or must I do that at the database level?


Top
 Profile  
 
 Post subject:
PostPosted: Mon Sep 26, 2005 9:41 am 
Beginner
Beginner

Joined: Sun Jul 31, 2005 1:23 pm
Posts: 35
dennisbyrne wrote:
The Enabled column is null by default. When retrieving an instance, null can be passed to setEnabled() . This fails because the pojo has a primitive boolean. Change this to Boolean, which can take a null value, so that your object model matches your data model - or, change the data model so that the default value for that column is 1 or 0 .


All rows were already non-NULL. However, I changed the database so the default value is 1, and I set a non-NULL constraint. I still get the same error. This value will never be NULL.

I know that 99% of the bugs reported here are user error. However, I susepct that this may really be a bug.


Top
 Profile  
 
 Post subject:
PostPosted: Mon Sep 26, 2005 12:00 pm 
Expert
Expert

Joined: Mon Jul 04, 2005 5:19 pm
Posts: 720
LanceVance wrote:
Can I change the hbm.xml mapping file to indicate a default value for a column/property?


There are a number of ways of doing this in H, but it won't make a difference if the java type cannot accomadate the values of the database column. primitives can't be null .


Top
 Profile  
 
 Post subject:
PostPosted: Mon Sep 26, 2005 1:56 pm 
Beginner
Beginner

Joined: Sun Jul 31, 2005 1:23 pm
Posts: 35
dennisbyrne wrote:
LanceVance wrote:
Can I change the hbm.xml mapping file to indicate a default value for a column/property?


There are a number of ways of doing this in H, but it won't make a difference if the java type cannot accomadate the values of the database column. primitives can't be null .


Primitive types can't support NULL: very true. And that column/property will never be NULL. Both the Java class and the database have default values and can NEVER be set to NULL. I don't understand why any code is trying to set this to NULL.

I've never had a problem using "int" or "float". "boolean" just might be buggy in Hibernate. I can build isolated test cases to prove this. But I really wish I could stop debugging/configuring my ORM layer and work on the actual application which is now sorely behind schedule.

Thanks for answering.


Top
 Profile  
 
 Post subject:
PostPosted: Mon Sep 26, 2005 2:02 pm 
Expert
Expert

Joined: Wed Apr 06, 2005 5:03 pm
Posts: 273
Location: Salt Lake City, Utah, USA
Just a shot in the dark: Did you try adding the not-null="true" attribute to the boolean property in your mapping file? Maybe Hibernate just needs to know that it's not-null.

_________________
nathan


Top
 Profile  
 
 Post subject:
PostPosted: Mon Sep 26, 2005 2:15 pm 
Hibernate Team
Hibernate Team

Joined: Tue Aug 26, 2003 6:10 am
Posts: 8615
Location: Neuchatel, Switzerland (Danish)
you database contains NULL at those places so when hibernate loads them it will complain since it detects non consistent data.

If NULL is allowed then change to use non primitives or write a usertype that handles your specific behavior.

_________________
Max
Don't forget to rate


Top
 Profile  
 
 Post subject:
PostPosted: Mon Sep 26, 2005 3:06 pm 
Beginner
Beginner

Joined: Sun Jul 31, 2005 1:23 pm
Posts: 35
nathanmoon wrote:
Just a shot in the dark: Did you try adding the not-null="true" attribute to the boolean property in your mapping file? Maybe Hibernate just needs to know that it's not-null.


Actually that's a really good idea.

But it didn't work:(

Thanks though


Top
 Profile  
 
 Post subject:
PostPosted: Mon Sep 26, 2005 3:12 pm 
Beginner
Beginner

Joined: Sun Jul 31, 2005 1:23 pm
Posts: 35
max wrote:
you database contains NULL at those places so when hibernate loads them it will complain since it detects non consistent data.

If NULL is allowed then change to use non primitives or write a usertype that handles your specific behavior.


No, it can't. I don't think you follow; NULL is not allowed in my database nor in my Java class. Here is my MySQL table:

Code:
+------------------+------------+------+-----+---------+-------+
| Field            | Type       | Null | Key | Default | Extra |
+------------------+------------+------+-----+---------+-------+
| UserID           | int(11)    |      | PRI | 0       |       |
| RatableItemID    | int(11)    |      | PRI | 0       |       |
| OriginalRating   | float      | YES  |     | NULL    |       |
| NormalizedRating | float      | YES  |     | NULL    |       |
| RatingDate       | datetime   | YES  |     | NULL    |       |
| Enabled          | tinyint(1) |      |     | 1       |       |
+------------------+------------+------+-----+---------+-------+


The database CAN NEVER have a NULL value. The Java class CAN NEVER have a NULL value. And I've updated by mapping XML to:

Code:
    <class name="com.auenrec.data.Rating" table="Ratings">
       <composite-id>
         <key-many-to-one name="user" column="UserID"
               class="com.auenrec.data.GenericUser"/>
         <key-many-to-one name="item" column="RatableItemID"
               class="com.auenrec.data.RatableItem"/>
      </composite-id>

        <property name="originalRating" column="OriginalRating" type="float"/>
        <property name="normalizedRating" column="NormalizedRating" type="float"/>
        <property name="ratingDate" column="RatingDate" type="timestamp"/>
        <property name="enabled" column="Enabled" type="boolean" not-null="true"/>
    </class>


There should be no mismatch. Ah well, I changed the Java type from boolean to Boolean and it works. I don't see why I should need to do this; I really believe this is just a bug but at least there is a workaround and I can return to working on my application.

Thanks guys!


Top
 Profile  
 
 Post subject:
PostPosted: Mon Sep 26, 2005 3:17 pm 
Hibernate Team
Hibernate Team

Joined: Tue Aug 26, 2003 6:10 am
Posts: 8615
Location: Neuchatel, Switzerland (Danish)
I have no idea why you keep saying it is a bug when it (probably) is not ;)

Your table can easily contain a null even though your schema says it can't (some databases does not perform those checks when you alter the schema).

Have you actually tried to see what happens when you do "select * from x where enabled is null" ?

If you look at the stacktrace you can see it happens at the time where hibernate is reading from the result set of the query you have executed and for some reason a null is returned at that point.

_________________
Max
Don't forget to rate


Top
 Profile  
 
 Post subject:
PostPosted: Mon Sep 26, 2005 3:59 pm 
Beginner
Beginner

Joined: Sun Jul 31, 2005 1:23 pm
Posts: 35
max wrote:
I have no idea why you keep saying it is a bug when it (probably) is not ;)

Your table can easily contain a null even though your schema says it can't (some databases does not perform those checks when you alter the schema).

Have you actually tried to see what happens when you do "select * from x where enabled is null" ?

If you look at the stacktrace you can see it happens at the time where hibernate is reading from the result set of the query you have executed and for some reason a null is returned at that point.


Yes, I've done:

SELECT COUNT(*) FROM Ratings WHERE Enabled IS NULL

and it returns 0. I'm very sure there are no NULL values in the database.


Top
 Profile  
 
 Post subject:
PostPosted: Mon Sep 26, 2005 4:03 pm 
Hibernate Team
Hibernate Team

Joined: Tue Aug 26, 2003 6:10 am
Posts: 8615
Location: Neuchatel, Switzerland (Danish)
so launch your debugger, put a breakpoint at BasicPropertyAccessor.java:59 and see at what place in the stacktrace you somehow get a null from the query you have executed.

Could it be you have a ref to an non existing row ?

_________________
Max
Don't forget to rate


Top
 Profile  
 
 Post subject:
PostPosted: Mon Sep 26, 2005 6:33 pm 
Beginner
Beginner

Joined: Sun Jul 31, 2005 1:23 pm
Posts: 35
max wrote:
so launch your debugger, put a breakpoint at BasicPropertyAccessor.java:59 and see at what place in the stacktrace you somehow get a null from the query you have executed.

Could it be you have a ref to an non existing row ?


Excellent idea. Thanks for the debugging tip.

I guess the advantage is the next time I hit a bug (yes, I know you don't think it's a bug) I will have some familiarity with the code base and can resolve it myself.

I definitely appreciate the responses everyone!


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 14 posts ] 

All times are UTC - 5 hours [ DST ]


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum

Search for:
© Copyright 2014, Red Hat Inc. All rights reserved. JBoss and Hibernate are registered trademarks and servicemarks of Red Hat, Inc.