-->
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: Getting SQL column names
PostPosted: Fri Nov 16, 2007 8:21 am 
Newbie

Joined: Wed Nov 02, 2005 11:00 am
Posts: 12
Location: Austria, Vienna
Hi,

Is there a way to get the SQL column name from Hibernate.

I don't want to use Strings for creating Restrictions because changes in the data model and the Hibernate Beans wouldn't result in compiler errors. So a changed column name would produce errors at runtime instead of compile time.

My idea was that the Hibernate Tools would create extra methods which return the SQL column name for a property, like:

getPkUserId() ... Property in Hibernate Bean representing the primary key of a table

getPkUserIdSqlName() ... Method which returns the SQL column name as string

So I could use these methods in Criterias and would get a compile error if the column name has changed -> because the Hibernate tools renamed the according getter.

I searched a lot but didn't find anything. I can't understand why nobody would have the same problem / idea?

Thanks!

RĂ¼diger Engelberger


Top
 Profile  
 
 Post subject: SQL Names in Hibernate
PostPosted: Sat Nov 17, 2007 12:51 am 
Newbie

Joined: Tue Nov 13, 2007 7:25 am
Posts: 8
Location: Bhubaneswar
Okay dude.... Lemme tell u..... U r using the ***.hbm.xml file. From that u can refer the sql names.... But if u want it implicitly done then u have to take help of an IDE....

The best IDE I've ever used for Hibernate is Eclipse with MyEclipse Plugin.

U can get it from here......http://www.myeclipseide.com

If u have used it then please try to use MyEclipse 5.1 or higher.... Bcoz with this u can directly do the Reverse Engineering to get the POJOs, hibernate.cfg.xml, HibernateSessionFactory, DAO Classes & all the hbm.xml files mapped to each other.....

This tool converts the sql data to the variables & the corresponding Setters & Getters.....

If u have any doubt then ask this thread.... I or anyone will defenitely reply.....

_________________
Regards
Lalit Narayan Mishra


Top
 Profile  
 
 Post subject:
PostPosted: Wed Nov 21, 2007 9:32 am 
Newbie

Joined: Wed Nov 02, 2005 11:00 am
Posts: 12
Location: Austria, Vienna
Hi,

Thanks for your reply ... but I think you misunderstood my question.

I already did the mapping with Hibernate Tools. Everything works perfect.

But my question was ... is there a way to access the SQL column names without providing strings but methods which return these strings.

e.g.:

Instead of:

Criteria crit = session.createCriteria(CmsComment.class);
crit.add(Expression.eq("commentId", commentId.intValue()));
CmsComment comment = (CmsComment) crit.list().get(0);

... I want to do something like:

Criteria crit = session.createCriteria(CmsComment.class);
crit.add(Expression.eq(CmsComment.getCommentIdSqlName(), commentId.intValue()));
CmsComment comment = (CmsComment) crit.list().get(0);

Otherwise I use strings in a lot of places in my application and when the data model changes I run into errors only at runtime, not at compile time.

Thanks!

RĂ¼diger Engelberger


Top
 Profile  
 
 Post subject:
PostPosted: Wed Nov 21, 2007 9:42 am 
Beginner
Beginner

Joined: Thu Nov 02, 2006 9:38 am
Posts: 32
Location: Belgium
You could make a class that holds all the constants.

E.g.


Class PropertyNames
{
...
public static final String EMLOYEE_NAME = "name";
...
}

And use " + PropertyNames.EMPLOYEE_NAME + " where you'd otherwise use name.


Top
 Profile  
 
 Post subject:
PostPosted: Thu Nov 22, 2007 12:02 pm 
Newbie

Joined: Wed Nov 02, 2005 11:00 am
Posts: 12
Location: Austria, Vienna
Thanks for your hint. But then I would have to manage more and more column names in a constants class. The advantage of Hibernate tools is to free me from such tasks.

I can't imagine that nobody regards the usage of strings for column names as a problem!?

I also think that it couldn't be a big deal to extend the Hibernate Tools to optionally provide additional getters for the column names. I had a look at the templates used by Hibernate Tools but I think they are used only for generating the mapping (XML) files but not for generating the beans.

Any ideas?


Top
 Profile  
 
 Post subject:
PostPosted: Thu Nov 22, 2007 12:09 pm 
Beginner
Beginner

Joined: Thu Nov 02, 2006 9:38 am
Posts: 32
Location: Belgium
I agree that it's a problem. Not a huge one, but I like statically checked queries just as much as you do. I already use this approach for my class names. E.g. I don't write "from User where..." but "from " + User.class.getName() + " where..." or something like that.

To have methods you can call to get these names, you'd have to do code generation. Reflection alone can't handle it since you're statically binding the method names. I don't know much about the hibernate tools, I only use it for ddl schema generation.


Top
 Profile  
 
 Post subject: use org.hibernate.cfg.Configuration and try it out !!
PostPosted: Tue Aug 26, 2008 5:01 pm 
Newbie

Joined: Tue Aug 26, 2008 4:56 pm
Posts: 1
Use this code sinppet as reference and try it out might answer your question...

Configuration cfg = new Configuration();
InputStream in = this.getClass().getClassLoader().getResourceAsStream("test.hbm.xml");
Configuration conf = cfg.addInputStream(in);
Iterator tmIter = conf.getTableMappings();
while (tmIter.hasNext()) {
Table t = (Table) tmIter.next();
Iterator tIter = t.getColumnIterator();
while (tIter.hasNext()) {
System.out.println(
"ColumName : " + ((Column) tIter.next()).getName());
}


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.