-->
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: why hbm2java do like this?
PostPosted: Thu Dec 09, 2004 12:38 pm 
Newbie

Joined: Thu Dec 09, 2004 12:31 pm
Posts: 3
Hibernate version:
2.1.7
Mapping documents:
<property name="volume" type="short">
<meta attribute="field-description">How loud to play the track</meta>
</property>
Code between sessionFactory.openSession() and session.close():
track = new Track("Video Killed the Radio Star",
"vol2/album611/track12.mp3",
Time.valueOf("00:03:49"), new Date(),
5, new HashSet(), new HashSet());
Full stack trace of any exception that occurs:
E:\se\src\com\oreilly\hh\CreateTest.java:105: cannot resolve symbol
[javac] symbol : constructor Track (java.lang.String,java.lang.String,java.sql.Time,java.util.Date,short,java.util.HashSet,java.util.HashSet)
[javac] location: class com.oreilly.hh.Track
[javac] track = new Track("The World '99",


why hbm2java change short type into Short?


Top
 Profile  
 
 Post subject:
PostPosted: Thu Dec 09, 2004 4:51 pm 
Newbie

Joined: Wed Nov 19, 2003 3:00 am
Posts: 7
Location: Madison, Wisconsin, USA
To add a bit more information, the generated class is using the Short object wrapper as the property's type, rather than the native short type. This is causing the constructor mismatch.

This was not the case in earlier versions of the Hibernate Tools. Why was this changed, and how can we get the previous behavior back?


Top
 Profile  
 
 Post subject:
PostPosted: Thu Dec 09, 2004 10:56 pm 
Hibernate Team
Hibernate Team

Joined: Tue Aug 26, 2003 7:19 pm
Posts: 2364
Location: Brisbane, Australia
Its being more strict on the field depending on the not-null status. 'Short' wrapper class can be made null while 'short' native type cannot.


Top
 Profile  
 
 Post subject:
PostPosted: Fri Dec 10, 2004 5:25 am 
Newbie

Joined: Thu Dec 09, 2004 12:31 pm
Posts: 3
so...
write like this :

new Short((short) 0)
?


Top
 Profile  
 
 Post subject:
PostPosted: Fri Dec 10, 2004 11:13 am 
Newbie

Joined: Wed Nov 19, 2003 3:00 am
Posts: 7
Location: Madison, Wisconsin, USA
Ah, so in other words, if the mapping document is updated to say not-null="true" it would return to using a native short?


Top
 Profile  
 
 Post subject:
PostPosted: Fri Dec 10, 2004 1:26 pm 
Newbie

Joined: Thu Dec 09, 2004 12:31 pm
Posts: 3
brunchboy wrote:
Ah, so in other words, if the mapping document is updated to say not-null="true" it would return to using a native short?


cool! you are right! thank you


Top
 Profile  
 
 Post subject:
PostPosted: Fri Dec 10, 2004 3:39 pm 
Hibernate Team
Hibernate Team

Joined: Tue Aug 26, 2003 6:10 am
Posts: 8615
Location: Neuchatel, Switzerland (Danish)
fyi this is mentioned in the very short changelog :)

...
...
* Fixed hbm2java so it honors the not-null attribute (HB-948)

_________________
Max
Don't forget to rate


Top
 Profile  
 
 Post subject: Object types, rather than built-in on not-null properties
PostPosted: Mon Dec 20, 2004 3:37 pm 
Newbie

Joined: Mon Dec 20, 2004 3:23 pm
Posts: 4
I just upgrade to version 2.1.3 of Hibernate tools from version 2.1.2

Version 2.1.3 of hbm2java is rendering not-null properties using object types rather than primitive types. eg. java.lang.Double instead of double.

For example version 2.1.3 of hbm2java renders the following property as a java.lang.Double, rather than double, despite the fact that not-null is true.

<property name="laborRate" type="double">
<meta attribute="use-in-tostring">true</meta>
<column name="laborRate" not-null="true"/>
</property>

The same property was rendered as 'double' in version 2.1.2 of hbm2java.

I can understand that nullable fields would be rendered as the object type Double, rather than primitive, but I don't see why this would apply to not-null fields.

Is this a feature or a bug?

- Andy


Top
 Profile  
 
 Post subject:
PostPosted: Mon Dec 20, 2004 4:04 pm 
Hibernate Team
Hibernate Team

Joined: Tue Aug 26, 2003 6:10 am
Posts: 8615
Location: Neuchatel, Switzerland (Danish)
make a short unit test and post it to the jira - it should work as expected...

_________________
Max
Don't forget to rate


Top
 Profile  
 
 Post subject:
PostPosted: Tue Dec 21, 2004 6:22 am 
Expert
Expert

Joined: Tue Oct 05, 2004 9:45 am
Posts: 263
just a question ...
Musn't the "not-null" be set at 'propery-level'?

You've mapped the column as "not-null" ...

Or does it work both ways?

It works fine for me, and i've done the mapping for the property.

gtx
curio


Top
 Profile  
 
 Post subject: Thx, not-null property on Column processed inconsistantly
PostPosted: Tue Dec 21, 2004 8:59 am 
Newbie

Joined: Mon Dec 20, 2004 3:23 pm
Posts: 4
Thx curio, you nailed it.

Moving the not-null attribute up from the <column> element to the <property> element solves the problem.

According to the dtd you can indeed specify not-null on either the <property> or <column> elements. In addition the schemaExport tool respects the not-null attribute when defined on a <column> element and generates the ddl accordingly. ie. the column in the database is correctly created as not-null when the not-null attribute is set at the column level.

However hbm2java ignores the not-null attribute on a <column> element and requires it to be set at the <property> level.

This behavior is surprising, since looking in the source at line #219 (pasted below) of ClassMapping.java it appears there is logic that specifically looks first at the property level, then tries the column level.

I haven't tried running under the debugger to determine the root cause - perhaps this fragment of code is irrelevant or perhaps a default value is inserted at the property level, and thus the method propertyElement.getAttributeValue( "not-null") may be returning the default value of "false"

Either way, moving the not-null attribute in my mapping file to the <property> element addresses my immediate problem.

Thanks for your help.

- Andy

=== fragment of code from line #219 of net.sf.hibernate.tool.hbm2java.ClassMapping.java

String notnull = propertyElement.getAttributeValue("not-null");
// if not-null property is missing lets see if it has been
// defined at column level
if(notnull == null) {
Element column = propertyElement.getChild("column");
if(column != null)
notnull = column.getAttributeValue("not-null");
}
boolean nullable = ( notnull == null || notnull.equals("false") );


Top
 Profile  
 
 Post subject: Must define not-null at both Property & Column levels
PostPosted: Tue Dec 21, 2004 11:35 am 
Newbie

Joined: Mon Dec 20, 2004 3:23 pm
Posts: 4
Turns out that in Hibernate tools 2.1.3 SchemaExport exclusively repects the not-null attribute on the <column> element, while hbm2java exclusively respects the not-null attribute on the <property>

ie. If the not-null attribute is only defined on the <property> element, schemaExport does not define the column and not-null. Conversely if the not-null attribute is only defined on the column element, hbm2java generates an Object property for not-null fields.

Thus you need to define the not-null attribute on both the property and column to ensure the correct behaviour from hbm2java and schemaExport as shown below.

<property name="laborRate" type="double" not-null="true" >
<meta attribute="use-in-tostring">true</meta>
<column name="laborRate" not-null="true"/>
</property>

This results in SchemaExport correctly defining a not-null column and hbm2java using the built in type of double, rather than java.lang.Double for the corresponding property on the generated Java class.


- Andy


Top
 Profile  
 
 Post subject:
PostPosted: Tue Dec 21, 2004 11:52 am 
Hibernate Team
Hibernate Team

Joined: Tue Aug 26, 2003 6:10 am
Posts: 8615
Location: Neuchatel, Switzerland (Danish)
remember that a property can be mapped to multiple columns - thus it cannot reliably listen to just the first column....

It should probably just AND them them all to get the proper result...make a jira for it and i'll see what i can do...I'll at least make this consistent when doing H3 hbmj2ava

_________________
Max
Don't forget to rate


Top
 Profile  
 
 Post subject:
PostPosted: Tue Dec 21, 2004 12:01 pm 
Newbie

Joined: Mon Dec 20, 2004 3:23 pm
Posts: 4
Thanks Max,

Of course the bahaviour makes sense when considering the semantical differences between a property & a column.

A reasonable enhancement for hbm2java would be that IF a property contains only 1 column and IF that column defines the not-null attribute AND the property does not, then the property should inherit nullability from the column.

Similarly, schemaExport could be enhanced, such that if a column does NOT define the not-null attribute and the property does, then schemaExport would use the property's nullability.

The current behaviour, where schemaExport only looks at column.not-null while hbm2java only uses property.not-null is a little confusing, as you are required to define it in both places.

I'll open a jira issue

thanks,


- Andy


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.