-->
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.  [ 7 posts ] 
Author Message
 Post subject: Really basic question about Hibernate mapping
PostPosted: Wed Sep 21, 2005 2:54 pm 
Newbie

Joined: Tue Aug 30, 2005 12:02 pm
Posts: 7
I'm trying to understand what Hibernate provides or looking at it from the opposite direction, what a developer does not need to provide when using Hibernate.

With this in mind, I'm confused about type casting on retrieval. For example, I have a Java class with an Integer attribute. This attribute is stored in the db as an int. In a JDBC implementation the code to retrieve this value from a result set and assign it to an instance of the object is:

myObj.setAttr ( new Integer ( rs.getInt(intCol))) ;

Will Hibernate do this correctly? I can imagine that it is possible to figure out via reflection that this is what has to be done but it seems like a LOT of work to build this into Hibernate for every combination of primitives and their wrapper classes.

The other alternative I can think of is that it would be the developers responsibility to create an int version of the set method. But this to seems problematic also because Hibernate must choose a set method by matching parameter types with the type being retrieved from the DB.

Could someone explain how Hibernate handles this.


Top
 Profile  
 
 Post subject:
PostPosted: Wed Sep 21, 2005 4:11 pm 
Expert
Expert

Joined: Fri Aug 19, 2005 2:11 pm
Posts: 628
Location: Cincinnati
yes.

this is all done with the mapping files.

_________________
Chris

If you were at work doing this voluntarily, imagine what you'd want to see to answer a question.


Top
 Profile  
 
 Post subject:
PostPosted: Wed Sep 21, 2005 4:28 pm 
Newbie

Joined: Tue Aug 30, 2005 12:02 pm
Posts: 7
Thanks for the reply Chris but it actually raised more questions in my mind. For instance, if an object has two set methods for an Integer attribute; one with an Integer parameter and one with an int parameter how does Hibernate pick one? Is it the developers responsibility to provide a 'standard' set method that internally handles type conversions?


Top
 Profile  
 
 Post subject:
PostPosted: Wed Sep 21, 2005 4:48 pm 
Expert
Expert

Joined: Fri Aug 19, 2005 2:11 pm
Posts: 628
Location: Cincinnati
The mapping file specifies which Java type hibernate uses.

here is a snippet from a mapping file

Code:
    <property
        name="sNum"
        type="java.lang.Integer"
        column="count"
        not-null="true"
        length="10"
    />


The above would use an Integer

_________________
Chris

If you were at work doing this voluntarily, imagine what you'd want to see to answer a question.


Top
 Profile  
 
 Post subject:
PostPosted: Wed Sep 21, 2005 5:09 pm 
Newbie

Joined: Tue Aug 30, 2005 12:02 pm
Posts: 7
Thanks again Chris. Your reply made me realize that I was probably wording my question incorrectly. Let me try again.

If the db type and the attribute type it is mapped to are different types what does Hibernate do?

Does Hibernate assume that there will be a set method for the db type that handles type conversion?

OR

Does Hibernate assume that there will be a set method for the object type that depends on Java built in type conversion?

OR

Does Hibernate assume that there will be a constructor for the object type that takes a parameter of the db type?

OR ??


Top
 Profile  
 
 Post subject:
PostPosted: Wed Sep 21, 2005 5:37 pm 
Expert
Expert

Joined: Fri Aug 19, 2005 2:11 pm
Posts: 628
Location: Cincinnati
Well, I think most developers wouldn't try to map a database type DATE to a java type Long. But, if you're doing logical matchings between db types and java types (such as Number to java.Long or varchar to String) hibernate knows exactly what to do and expects a setter and getter of type String.

Hibernate was designed to work with many different databases (mssql, oracle, db2, postregsql, etc) and it understands what to do with their types. This is done via hibernate.dialect setting in your config file.

You specify how hibernate converts these types in the mapping files. Therefore, if you specify String, it'll use the String setter.

Please read more about hibernate mapping files.

_________________
Chris

If you were at work doing this voluntarily, imagine what you'd want to see to answer a question.


Top
 Profile  
 
 Post subject:
PostPosted: Wed Sep 21, 2005 5:41 pm 
Expert
Expert

Joined: Fri Aug 19, 2005 2:11 pm
Posts: 628
Location: Cincinnati
Let me reiterate.

You specify how hibernate converts types in the database to types used by your java program.

_________________
Chris

If you were at work doing this voluntarily, imagine what you'd want to see to answer a question.


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 7 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.