-->
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.  [ 6 posts ] 
Author Message
 Post subject: how to access all the existing table details in a database
PostPosted: Wed May 10, 2006 5:58 am 
Newbie

Joined: Wed May 10, 2006 5:34 am
Posts: 7
Hi all,

Hibernate version:3.1
I wants to list all existing tables in a database using hibernate.
Iam trying to execute sql query:"SELECT * FROM TAB";
but, it is not working in hibernate, it is giving mapping exception. and also don't know how to access the table details in an object..
So, please help me to solve this problem.
Regards,
chintu


Top
 Profile  
 
 Post subject:
PostPosted: Wed May 10, 2006 9:14 am 
Expert
Expert

Joined: Tue Apr 25, 2006 12:04 pm
Posts: 260
Try this code if you are using oracle and want to query table names in database. I am not sure if it works with other databases as well.

Code:
String sql = "select table_name from all_tables where owner='YOUR_DB_SCHEMA_NAME'";

Query q = session.createSQLQuery( sql );
List names = q.list();
if ( names != null && !(names.isEmpty()) ) {
   int length = names.size();
   for ( int i=0; i<length; i++ ) {
      Object object = names.get( i );
      System.out.println("object = " + object.toString());
   }
}


or else try this code if you want to query configured tables with Hibernate SessionFactory

Code:
SessionFactory factory =HibernateUtil.getSessionFactory();
Map metadata = factory.getAllClassMetadata();
Iterator it = metadata.entrySet().iterator();
while ( it.hasNext() ) {
   Map.Entry entry = (Map.Entry) it.next();
   Object key = entry.getKey();
}


Top
 Profile  
 
 Post subject: how to access all the existing table details in a database
PostPosted: Thu May 11, 2006 1:08 am 
Newbie

Joined: Wed May 10, 2006 5:34 am
Posts: 7
Hi ,
Iam sorry to say your code didnt worked with my database,iam using oracle only.
in the query how can i give table_name all tables.thats the pbm. ..if 10 tables were there in a database name employee.. i need to fetch all the table names thats all.
please help me out in this..
Regards,
Shilpa


Top
 Profile  
 
 Post subject:
PostPosted: Thu May 11, 2006 10:59 am 
Expert
Expert

Joined: Tue Apr 25, 2006 12:04 pm
Posts: 260
Will this work for you. Its prints out all the columns information ( separated by | ) including table name for your database EMPLOYEE

Code:
String sql = "select * from all_tables where owner='EMPLOYEE'";

Query q = session.createSQLQuery( sql );
List names = q.list();
if ( names != null && !(names.isEmpty()) ) {
   int length = names.size();
   for ( int i=0; i<length; i++ ) {
      Object object = names.get( i );
      if ( object instanceof Object[] ) {
         Object[] cols = (Object[]) object;
         for ( int col=0; col<cols.length; col++ ) {
            System.out.print( cols[ col ] + " | ");
         }
      }
   }
}


Top
 Profile  
 
 Post subject: how to access all the existing table details in a database
PostPosted: Fri May 12, 2006 1:59 am 
Newbie

Joined: Wed May 10, 2006 5:34 am
Posts: 7
hi bkmr_77,
thanks for ur help, again the cde u gave me is not working it is givg error in add entiti()/scalar().i need to cal this addentity mthod but dont knoew how and alo why?
Is this add entity refer to pojo class, isit i need to create a class for storing the value as objects .. plz let meknow..
waiting for your reply.\
Regards,
chintu


Top
 Profile  
 
 Post subject: try this, and it should work.
PostPosted: Fri May 19, 2006 10:54 am 
Newbie

Joined: Thu Apr 06, 2006 6:06 pm
Posts: 10
Code:
String sql = "select table_name from all_tables where owner='YOUR_DB_SCHEMA_NAME'";

Query q = session.createSQLQuery( sql )
                   .addScalar("table_name", Hibernate.STRING);
List names = q.list();
if ( names != null && !(names.isEmpty()) ) {
   int length = names.size();
   for ( int i=0; i<length; i++ ) {
      String tableName = names.get( i );
      System.out.println("a table name = " + tableName);
   }
}


Hope this helps.
nate


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