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.  [ 8 posts ] 
Author Message
 Post subject: How do i get the column length.
PostPosted: Fri Jan 23, 2009 1:05 pm 
Newbie

Joined: Fri Jan 23, 2009 12:33 pm
Posts: 3
I need to get the column length for some entity properties so we can perform some data validation before saving.

Is there a way to do this?

There was a suggestion on another post to obtain the underlying JDBC connection and get the info from there, but the connection method has been deprecated (we're using version 3.2.6)
I should mention that we're using the EntityManager (JPA) interface of Hibernate.

Any help is apprechiated


Top
 Profile  
 
 Post subject:
PostPosted: Fri Jan 23, 2009 5:43 pm 
Newbie

Joined: Fri Jan 23, 2009 5:39 pm
Posts: 2
Location: Pittsburgh, PA
Have you tried a direct database connection? Most of the popular databases like Oracle, MySQL and SQLServer have sQL that you can run to get the properties of a field, like its length. Each one is different, so you'll have to consult the individual database's documentation to see how.


Top
 Profile  
 
 Post subject:
PostPosted: Fri Jan 23, 2009 5:47 pm 
Newbie

Joined: Fri Jan 23, 2009 12:33 pm
Posts: 3
haven't tried that, but i find it hard to believe that there isn't a way to obtain this information from hibernate.

After all this information is available to it from orm files.


Top
 Profile  
 
 Post subject:
PostPosted: Fri Jan 23, 2009 6:07 pm 
Newbie

Joined: Fri Jan 23, 2009 5:39 pm
Posts: 2
Location: Pittsburgh, PA
If you use the Eclipse tools to auto-generate your mapping from the database, it should automatically put the length in, at least it does for me using an Oracle database.


Top
 Profile  
 
 Post subject:
PostPosted: Fri Jan 23, 2009 8:04 pm 
Expert
Expert

Joined: Wed Mar 03, 2004 6:35 am
Posts: 1240
Location: Lund, Sweden
The class org.hibernate.mapping.Column holds information about a mapped database column. See: http://www.hibernate.org/hib_docs/v3/ap ... olumn.html

One way to get to that information is to start at your org.hibernate.cfg.Configuration object, assuming that you somehow can get access to it from your EntityManager (I have no experience with it). Then something like:

Code:
Configuration cfg = ....
PersistentClass pc =cfg.getClassMapping(String entityName);
Table t = pc.getTable();
Column c = t.getColumn(columnName);


This is the simple case which should work if you have one table per entity. More complex cases may require calling other methods. Trial and error is your best friend, since the Javadoc is not so full with information. And... be prepared to have to change your code if moving to a different Hibernate version since this is not considered as a public API and may change.


Top
 Profile  
 
 Post subject:
PostPosted: Fri Jan 23, 2009 8:33 pm 
Newbie

Joined: Fri Jan 23, 2009 12:33 pm
Posts: 3
That's the problem. I can't figure out how to get at the configuration object.

From the entity manager i can get the session, from there the session factory and finally the SingleTableEntityPersister

This seems to have a lot of information including column names, but i can't find the column lengths anywhere.

I'm stuck at this point. It seems like i should be able to get this info somehow because hibernate obviously has it...but so far i'm getting nowhere.


Top
 Profile  
 
 Post subject:
PostPosted: Sat Jan 24, 2009 6:26 am 
Expert
Expert

Joined: Wed Mar 03, 2004 6:35 am
Posts: 1240
Location: Lund, Sweden
I don't know if you can get "backwards" from a SessionFactory to the Configuration. In my app we start out with a Configuration object and use Configuration.buildSessionFactory() to get the session factory. And if I check the code it seems like the SessionFactory only uses the Configuration object in the constructor and then "forgets" about it.


Top
 Profile  
 
 Post subject: Re: How do i get the column length.
PostPosted: Tue Oct 20, 2009 5:57 am 
Newbie

Joined: Tue Aug 26, 2008 12:18 pm
Posts: 9
I have just solved the problem of obtaining the Configuration object. If u r using Spring, you can get it from LocalSessionFactoryBean (which you get from your application context). The obtaining of legth is then a piece of cake ;), as described by norborg:

Code:
localSessionFactoryBean.getConfiguration().
getClassMapping(String entityName).getTable().getColumn(Column cl).getLength();

_________________
Arguing on the Internet is like running in the Special Olympics.

Even if you win, you’re still retarded. :P


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