-->
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.  [ 4 posts ] 
Author Message
 Post subject: Getting the length of a property programmatically
PostPosted: Wed Aug 16, 2006 11:22 am 
Regular
Regular

Joined: Wed Feb 15, 2006 9:09 pm
Posts: 76
Hibernate version: 3.1.3

Hi,

How can I get the length of, say, a String property programmatically from the SessionFactory or Configuration? I'd like to retrieve, on the fly, the length of a column specified in the mapping document. Not quite sure how.

Thanks.


Top
 Profile  
 
 Post subject:
PostPosted: Wed Aug 16, 2006 11:42 am 
Newbie

Joined: Sat May 13, 2006 12:00 pm
Posts: 19
Do you mean the current string length or the underlying limitation on the column with in the database?

If you mean the underlying database metadata you could do this the same way we used to do these things before Hibernate by using the java.sql.DataBaseMetaData class.

(http://java.sun.com/j2se/1.5.0/docs/api ... aData.html)

For example...
Code:

  Connection c = session.connection();
  DataBaseMetaData dbmd = c.getMetaData();
  ResultSet rs = dbmd.getColumns(null,null,"my_table","my_column");
  rs.next();

  int columnSize = rs.getInt("COLUMN_SIZE");



Using that connection() call directly might not be allowed depending on the transaction, database, and other factors in Hibernate that I'm not aware of...


Top
 Profile  
 
 Post subject:
PostPosted: Wed Aug 16, 2006 11:56 am 
Regular
Regular

Joined: Wed Feb 15, 2006 9:09 pm
Posts: 76
Nope, I mean the one I specified in the mapping file. I'm actually using annotations, but the idea's the same. I have:

CREATE TABLE blah ( some_col VARCHAR2(32) );

So because it's 32, I put this in the mapping:

@Basic
@Column( name="some_col", length=32 ) <---- Need this!
private String someCol;

So I need to grab it from the property itself (mapping document).


Top
 Profile  
 
 Post subject:
PostPosted: Thu Aug 17, 2006 9:51 am 
Newbie

Joined: Sat May 13, 2006 12:00 pm
Posts: 19
Isn't that information just forwarded to the database for table creation. In other words, any column width restrictions are imposed by the underlying database and not hibernate (from what I understand). Once the table is created, the metadata can be retreived from the DataBaseMetaData instance.

However...

I was looking a little deeper and I wonder if this might work:

From Configurations try:
Code:
Configuration c

// these iterators aren't typed so I'm assuming that the iterator
// iterates over instances of Mappings.  I havn't tried it yet.
Mappings m = (Mappings)c.getTableMappings().next();

// same note; I assume this returns Tables
Table t = (Table)m.iterateTables().next();

Column c = t.getColumn(columnIndex);

// this looks like it...
int length = c.getLength();


A lot of assumptions here. I havn't tried it myself.


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