-->
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.  [ 9 posts ] 
Author Message
 Post subject: Efficient way of mapping a class to different tables
PostPosted: Thu Jan 06, 2005 12:55 am 
Newbie

Joined: Mon Jan 03, 2005 3:18 am
Posts: 8
This is in continuation to my post "Mapping the sql table and java object in runtime".

I have figured out that we can map a single POJO to different classes at runtime. The way i am doing it is

Code:
   Session session = HibernateUtil.currentSession();
   Transaction tx = session.beginTransaction();

   Query query = session.createQuery("select c from test.Cat as c where c.sex = 'F'");
   for (Iterator it = query.iterate(); it.hasNext();) {
      Cat cat = (Cat) it.next();

      String fileName = getServletContext().getRealPath("/WEB-INF/classes")+"\\ClientTable.hbm.xml";
      FileWriter fw = new FileWriter( new File(fileName));
      fw.write("<?xml version=\"1.0\"?> \n");
      fw.write("<!DOCTYPE hibernate-mapping \n" +
            "   PUBLIC \"-//Hibernate/Hibernate Mapping DTD//EN\"\n" +
            "   \"http://hibernate.sourceforge.net/hibernate-mapping-2.0.dtd\">\n");
      fw.write("<hibernate-mapping>\n");
      fw.write("   <class name=\"test.ClientTable\" table=\""+cat.getName()+"\">\n");
      fw.write("      <id name=\"col1\" type=\"string\" unsaved-value=\"null\" >\n");
      fw.write("          <column name=\""+cat.getSex()+"\" sql-type=\"char(10)\" not-null=\"true\"/> \n");
      fw.write("         <generator class=\"assigned\"/>\n");
      fw.write("      </id>\n");
      fw.write("      <property name=\"col2\">\n");
      fw.write("         <column name=\"val\" sql-type=\"char(10)\" not-null=\"true\"/>\n");
      fw.write("      </property>\n");
      fw.write("   </class>\n");
      fw.write("</hibernate-mapping>\n");
      fw.close();

      Configuration cfg = new Configuration().configure();
      cfg = cfg.addFile(new File(fileName));
               
      SessionFactory sf = cfg.buildSessionFactory();
      Session session2 = sf.openSession();

      Transaction tx2 = session2.beginTransaction();
      Query query2 = session2.createQuery("from test.ClientTable");
      out.println("table 2 contents Begin:");
      for (Iterator it2 = query3.iterate(); it2.hasNext();) {
         ClientTable ct2 = (ClientTable) it2.next();
         out.println(" values of second table : " + ct2.getCol1() + " :: " + ct2.getCol2() );
      }
      out.println("table 2 contents END:");
      tx2.commit();
      
   }

   tx.commit();
   HibernateUtil.closeSession();


As you can observe I have to create a new xml file, configuration, sessionfactory and session for every result of the first query. some fine tuning of code can be done to this raw code for perfomance, but the basic idea remains the same. i.e. create a new configuration and sessionfactory
I tried to use the same configuration, but as all the tables are using the same POJO, it is throwing a 'MappingException: duplicate import' error.

In my application, once the query is processes, i do not require that mapping anymore. so is there a way where in i can remove a mapping from the configuration, and use the same configuration to map it again to a different table which uses the same java class?

and The basic question is: "IS my approach correct OR is there a better way to do it??"


Top
 Profile  
 
 Post subject:
PostPosted: Thu Jan 06, 2005 4:26 am 
Hibernate Team
Hibernate Team

Joined: Tue Aug 26, 2003 6:10 am
Posts: 8615
Location: Neuchatel, Switzerland (Danish)
use Hibernate3 and entity-name

_________________
Max
Don't forget to rate


Top
 Profile  
 
 Post subject:
PostPosted: Thu Jan 06, 2005 5:54 am 
Newbie

Joined: Mon Jan 03, 2005 3:18 am
Posts: 8
Thanks max,

i have just downloaded the hibernate 3.0 and am going through the documentation. So far i have not found any documentation for the entity-name feature. The document says 'TODO'. even the api doesn't tell much.

looking at some other posts, i feel that entity-name should do the job for me, but i need some information
Can you provide me a link where in there are some good examples for the use of entity-name.

thanks


Top
 Profile  
 
 Post subject:
PostPosted: Thu Jan 06, 2005 6:09 am 
Hibernate Team
Hibernate Team

Joined: Tue Aug 26, 2003 6:10 am
Posts: 8615
Location: Neuchatel, Switzerland (Danish)
documentation isn't there yet - sorry.

but it should be rather trivial - you need to set an entity-name on those mappings where you are mapping the actual same class.

And then use that entity-name where you would normally use the classname.

Try it - it should be rather straight forward.

_________________
Max
Don't forget to rate


Top
 Profile  
 
 Post subject:
PostPosted: Thu Jan 06, 2005 6:10 am 
Hibernate Team
Hibernate Team

Joined: Tue Aug 26, 2003 6:10 am
Posts: 8615
Location: Neuchatel, Switzerland (Danish)
documentation isn't there yet - sorry.

but it should be rather trivial - you need to set an entity-name on those mappings where you are mapping the actual same class.

And then use that entity-name where you would normally use the classname.

Try it - it should be rather straight forward.

_________________
Max
Don't forget to rate


Top
 Profile  
 
 Post subject:
PostPosted: Thu Jan 06, 2005 6:49 am 
Newbie

Joined: Mon Jan 03, 2005 3:18 am
Posts: 8
hi max,

Thanks a lot for the quick reply.

max wrote:

but it should be rather trivial - you need to set an entity-name on those mappings where you are mapping the actual same class.

And then use that entity-name where you would normally use the classname.



But in my case i will get to know the table names only during runtime.

So, i cannot map the tables apriori.

Can i map the table names programatically. Is there an method,class or API which lets me do it.


Top
 Profile  
 
 Post subject:
PostPosted: Thu Jan 06, 2005 7:02 am 
Hibernate Team
Hibernate Team

Joined: Tue Aug 26, 2003 6:10 am
Posts: 8615
Location: Neuchatel, Switzerland (Danish)
yes - look at Configuration API ....

_________________
Max
Don't forget to rate


Top
 Profile  
 
 Post subject:
PostPosted: Thu Jan 06, 2005 7:09 am 
Newbie

Joined: Mon Jan 03, 2005 3:18 am
Posts: 8
do i have to use dynamic-class for it ?


Top
 Profile  
 
 Post subject:
PostPosted: Thu Jan 06, 2005 7:13 am 
Hibernate Team
Hibernate Team

Joined: Tue Aug 26, 2003 6:10 am
Posts: 8615
Location: Neuchatel, Switzerland (Danish)
no

_________________
Max
Don't forget to rate


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