-->
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: Reveng: Field lengths
PostPosted: Wed Feb 08, 2006 8:21 am 
Beginner
Beginner

Joined: Tue Jan 24, 2006 1:48 pm
Posts: 20
Location: Stevenage, UK
Ok, I've reverse-engineered a database, generated hbm.xml, pojo and dao classes - so far so good.
Most of the columns in my database are strings, hence most fields in my pojo classes are java.lang.String.
I've also got some presentation code responsible for showing the user this data and accepting changes from the user. This code needs to know how many characters the user should be allowed to enter for each string.

Question: How do I find out how many characters I'm allowed to put in a String field?

The database knows - I'll get an exception if I try to persist strings that are too long - and the reverse-engineering tools could know (the database meta-data tells them) - but the pojo classes don't contain any "public static final int fieldLength_XXX" attributes or similar.
I'd guess that one can probably access this information in the template code, but I'm not 100% sure if it's possible, or how to do this. My hope is that I could make my templates access this information and put that into my generated code, but I've not found the information anywhere.

Is this possible? If so, how? If not, is there a good reason why I shouldn't want this?

Thanks,
Peter

Hibernate tools version: 3.1.0_BETA4


Top
 Profile  
 
 Post subject: Re: Reveng: Field lengths
PostPosted: Wed Feb 08, 2006 8:53 am 
Hibernate Team
Hibernate Team

Joined: Tue Aug 26, 2003 6:10 am
Posts: 8615
Location: Neuchatel, Switzerland (Danish)
pjdarton wrote:
Question: How do I find out how many characters I'm allowed to put in a String field?


something like: configuration.getClassMapping("someName").getProperty("yaProperty").getColumn(0).getLength() ....non tested but you get the picture.

Quote:
database meta-data tells them) - but the pojo classes don't contain any "public static final int fieldLength_XXX" attributes or similar.
I'd guess that one can probably access this information in the template code, but I'm not 100% sure if it's possible, or how to do this. My hope is that I could make my templates access this information and put that into my generated code, but I've not found the information anywhere.

Is this possible? If so, how? If not, is there a good reason why I shouldn't want this?


some would argue that hardcoded constants into your pojo's about length is bad practice. I would instead generate interface with these constants ifi were you.

it has been in jira with a patch for the ooold tools, but noone have had the will to do it for the new tools. feel free ;)

_________________
Max
Don't forget to rate


Top
 Profile  
 
 Post subject:
PostPosted: Wed Feb 08, 2006 1:29 pm 
Beginner
Beginner

Joined: Tue Jan 24, 2006 1:48 pm
Posts: 20
Location: Stevenage, UK
Quote:
something like: configuration.getClassMapping("someName").getProperty("yaProperty").getColumn(0).getLength()


Aha! After some stumbling around in the dark, I'm now getting somewhere.
There isn't a getColumn(int) method (in the version of Hibernate I'm using - 3.1.0 I think), but there is a getColumnIterator(), which amounts to much the same effect.
I can now obtain the data I'm after BUT it means hanging on to the configuration object.

Given a Session, is it possible to get back to the Configuration object from which the Session was created?
I know you can get the SessionFactory from a Session, but I don't see any way of getting the Configuration from that.

(I think I'm going to end up adding methods to do this sort of stuff to my DAO Java classes - I agree that POJO is not the best place, but I think the auto-generated code should make it easy for user-code to access this information)


Top
 Profile  
 
 Post subject:
PostPosted: Wed Feb 08, 2006 1:52 pm 
Hibernate Team
Hibernate Team

Joined: Tue Aug 26, 2003 6:10 am
Posts: 8615
Location: Neuchatel, Switzerland (Danish)
no, you cannot get from a SF to a Configuration. A SF is a "compiled" version of a Configuration. The Configuration is mutable thus not really good to keep around in the API.

but that does not prevent you from accessing it.

_________________
Max
Don't forget to rate


Top
 Profile  
 
 Post subject:
PostPosted: Wed Feb 08, 2006 2:12 pm 
Beginner
Beginner

Joined: Tue Jan 24, 2006 1:48 pm
Posts: 20
Location: Stevenage, UK
So, given a Session (which is what I generally have available in my code), how can I find out how many characters I can put in text fields?
You're saying that it's not normal to have a Configuration object available as it's mutable, but if one only has a Session or SessionFactory, one can't get at the metadata.

Sounds like a bug or shortcoming in SessionFactory to me.

Am I being unreasonable? Are there better ways of ensuring data validity that don't require the Configuration object?


Top
 Profile  
 
 Post subject:
PostPosted: Wed Feb 08, 2006 2:17 pm 
Hibernate Team
Hibernate Team

Joined: Tue Aug 26, 2003 6:10 am
Posts: 8615
Location: Neuchatel, Switzerland (Danish)
nothing prevents *you* from keeping it around.

It would just not be a safe/good thing for us to keep around in the API since it is non-safe.

_________________
Max
Don't forget to rate


Top
 Profile  
 
 Post subject:
PostPosted: Wed Feb 08, 2006 2:24 pm 
Beginner
Beginner

Joined: Tue Jan 24, 2006 1:48 pm
Posts: 20
Location: Stevenage, UK
Ah, yes. Understood now.


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:
cron
© Copyright 2014, Red Hat Inc. All rights reserved. JBoss and Hibernate are registered trademarks and servicemarks of Red Hat, Inc.