2

I have a ResultSetMetaDataobject.

PreparedStatement ps=con.prepareStatement("select var1, var2 from test1, test2");  
ResultSet rs=ps.executeQuery();  
ResultSetMetaData rsmd=rs.getMetaData();  

System.out.println("Total columns: "+rsmd.getColumnCount());  
System.out.println("Column Name of 1st column: "+rsmd.getColumnName(1));  
System.out.println("Column Type Name of 1st column: "+rsmd.getColumnTypeName(1));  

I need to create an new empty table with all the fields in the above ResultSetMetaData

  1. Open a new connection in different database
  2. Use the meta data above
  3. Create a new empty table with this meta data

I can see two possible solutions

  1. Reflection. In general to create a table with Hibernate, I need to create a bean. But I don't know in advance what fields the ResultSetMetaData will contain . Should I create this bean with reflection?
  2. HQL CREATE TABLE . Is this possible?

What is the simplest way to do this? How can I do this?

2 Answers 2

2

Couple of solutions that I can think of:

  1. SchemaExport - Iterate through your ResultSetMetaData which exposes API's like getColumnName and getColumnType to get the information you need.
    Using this information create a hibernate mapping XML and send it to SchemaExport.create.

    // yourClassMapper.xml generated in runtime
    Configuration config = new Configuration();
    config.addResource("yourClassMapper.xml");  
    
    new SchemaExport(config).create(true,true) 
    
  2. Native Query - As Usual Iterate through your metadata to figure out what should the columns be.
    Using createSQLQuery or even using createNativeQuery

    session.createSQLQuery("create table .....").executeUpdate();
    
  3. This link provides an explanation on how to create custom fields. You can make use of its DOM Parser for 1st approach or create just tables and use the method mentioned in this blog to create custom fields. (Refer to this class MappingManager and its related custom map configuration)

Lastly, You need 2 different datasource to be configured for this. And inject the second database's source for creating session/sessionfactory in above implementations.

1
  • Thank you +1. I wish there was a simpler and more neat solution. Creating a class mapper seems harder than reflection. 'Native query` is not an option. Commented Jan 19, 2017 at 7:17
2

I dont think you need to create bean for it. If you want to create new table with same fields then simple fire this query on database using hibernate or jdbc.

SHOW CREATE TABLE tablename

Now simply change the name of the table in query, can be easily done in Java.

Now you does not need to create any bean for newly created table. There are two ways for fetching data from table in hibernate. First way is get table data in form of bean object, another way is fire sql query and get data in form of Object (Java Class). you can get data in form of Object and Object Array using hibernate.

When we use join query in hibernate, then there is no specific type of bean is available for handling result of join because it contain column from multiple table so you will get data in form of Object and Object Array.

Just get Data in form of Object or Object Array.

Check This Link of my github... not exactly what you want but still will help :

here

I hope I Helped you.
1
  • Thank you +1 . But I need to use the ResultSetMetaData . SHOW CREATE TABLE works only with a table. I have a query not a table Commented Jan 19, 2017 at 7:21

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.