-->
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: Dynamic table mapping
PostPosted: Fri Oct 24, 2008 10:46 am 
Regular
Regular

Joined: Wed Oct 15, 2008 6:59 am
Posts: 103
Location: Chennai
I want to use same mapping file and POJO file for two tables report1 and report2. These two tables schema is same. but the names of the table only differ. when i change the table name 'report1'in the Report1.hbm.xml to report2, my code works fine. i get data from report2 table also. then i change my table value to report1. it retrieves report1 table values.

My problem is to change the table name report_1 to report_2 in the Report1.hbm.xml file by code.

my code and xml files as follows ...

Report1.hbm.xml
Quote:
<?xml version="1.0" encoding="utf-8"?>
<!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
<!--
Mapping file autogenerated by MyEclipse Persistence Tools
-->
<hibernate-mapping>
<class name="com.hcl.Report1" table="report_1" catalog="testDB">
<id name="id" type="java.lang.Integer">
<column name="id" />
<generator class="native" />
</id>
<property name="name" type="java.lang.String">
<column name="name" length="45" not-null="true" />
</property>
</class>
</hibernate-mapping>


My POJO code....
public class Report1 implements java.io.Serializable {
// Fields
private Integer id;
private String name;
// Constructors
/** default constructor */
public Report1() {
}
/** full constructor */
public Report1(String name) {
this.name = name;
}
// Property accessors
public Integer getId() {
return this.id;
}
public void setId(Integer id) {
this.id = id;
}
public String getName() {
return this.name;
}
public void setName(String name) {
this.name = name;
}

}

My Main method code ..
Code:
import java.util.Iterator;
import org.hibernate.Session;
import org.hibernate.Transaction;
import org.hibernate.cfg.Configuration;

public class HibernateTest {

   /**
    * @param args
    */
   public static void main(String[] args) throws Exception {
      // TODO Auto-generated method stub
       Configuration config=new Configuration();
       Session session=HibernateSessionFactory.getSession();
       Transaction trx=session.beginTransaction();
       Iterator results=session.createQuery("select r.name from Report1 r").iterate();
       while(results.hasNext())
          System.out.println(results.next().toString());
       trx.commit();
       session.close();
   }
}

_________________
If u feel it will help you, don't forget to rate me....


Last edited by Madan_Prabhu on Mon Feb 16, 2009 4:12 am, edited 1 time in total.

Top
 Profile  
 
 Post subject: Error in mapping same POJO to two tables(Schema is same)
PostPosted: Fri Oct 31, 2008 12:57 am 
Regular
Regular

Joined: Wed Oct 15, 2008 6:59 am
Posts: 103
Location: Chennai
I just open HBM file and modify the table value to report_2 as like that

Code:
File file=new File("Report1.hbm.xml");
SAXBuilder builder = new SAXBuilder();
Document doc = builder.build(file);
Element docroot=doc.getRootElement();
System.out.println("Root Elemet: "+ docroot.toString());
System.out.println(docroot.getChild("class").getAttribute("table").setValue("report_2"));
XMLOutputter xmlout=new XMLOutputter();
xmlout.output(doc, new FileOutputStream(new File("Report1.hbm.xml")));


And i also rebuild HibernateSessionFactory ..
Code:
HibernateSessionFactory.rebuildSessionFactory();


It's shows following error...

%%%% Error Creating SessionFactory %%%%
org.hibernate.InvalidMappingException: Could not parse mapping document from resource com/hcl/Report1.hbm.xml
at org.hibernate.cfg.Configuration.addResource(Configuration.java:569)
at org.hibernate.cfg.Configuration.parseMappingElement(Configuration.java:1587)
at org.hibernate.cfg.Configuration.parseSessionFactory(Configuration.java:1555)
at org.hibernate.cfg.Configuration.doConfigure(Configuration.java:1534)
at org.hibernate.cfg.Configuration.doConfigure(Configuration.java:1508)
at org.hibernate.cfg.Configuration.configure(Configuration.java:1428)
at com.hcl.HibernateSessionFactory.rebuildSessionFactory(HibernateSessionFactory.java:69)
at HibernateTest.main(HibernateTest.java:107)
Caused by: org.hibernate.DuplicateMappingException: Duplicate class/entity mapping com.hcl.Report1
at org.hibernate.cfg.Mappings.addClass(Mappings.java:118)
at org.hibernate.cfg.HbmBinder.bindRoot(HbmBinder.java:145)
at org.hibernate.cfg.Configuration.add(Configuration.java:669)
at org.hibernate.cfg.Configuration.addInputStream(Configuration.java:504)
at org.hibernate.cfg.Configuration.addResource(Configuration.java:566)
... 7 more

_________________
If u feel it will help you, don't forget to rate me....


Top
 Profile  
 
 Post subject:
PostPosted: Wed Nov 05, 2008 2:03 am 
Regular
Regular

Joined: Wed Oct 15, 2008 6:59 am
Posts: 103
Location: Chennai
If i save in same name, it gets data from one table only (report_1).

when i save the hbm file into other name and dynamically create configuration, it's work fine.

but i want to use only one mapping hbm file to map multiple tables.

plz help me.

_________________
If u feel it will help you, don't forget to rate me....


Top
 Profile  
 
 Post subject:
PostPosted: Wed Nov 05, 2008 5:42 am 
Expert
Expert

Joined: Wed Mar 03, 2004 6:35 am
Posts: 1240
Location: Lund, Sweden
You'll need two hbm files. But you can map both tables to the same class.

Report1.hbm.xml
Code:
<class name="com.hcl.Report" entity-name="Report1" table="report_1">
...
</class>


Report2.hbm.xml
Code:
<class name="com.hcl.Report" entity-name="Report2" table="report_2">
...
</class>


Top
 Profile  
 
 Post subject:
PostPosted: Wed Nov 05, 2008 8:31 am 
Regular
Regular

Joined: Wed Oct 15, 2008 6:59 am
Posts: 103
Location: Chennai
Thanks for ur reply friend..

Actually i try to solve this problem by same mapping file with only modification in table attribute in class tag in hbm file.

Dynamically create configuration object and modify hbm file by SAXBuilder and create new configuration and sessionfactory by the modified hbm file.

the Code as follows..

Code:
Configuration config=new Configuration();
       config.configure("/hibernate.cfg.xml");
       config.addResource("Report1.hbm.xml");
       SessionFactory sf=config.buildSessionFactory();
       Session session=sf.openSession();
       Transaction trx=session.beginTransaction();
       System.out.println("Report1");
        while(results.hasNext())
          System.out.println(results.next().toString());
        File file=new File("Report1.hbm.xml");
        SAXBuilder builder = new SAXBuilder();
      Document doc = builder.build(file);
        Element docroot=doc.getRootElement();
      System.out.println("Root Elemet: "+ docroot.toString());
      System.out.println(docroot.getChild("class").getAttribute("table").setValue("report_2"));
      XMLOutputter xmlout=new XMLOutputter();
      xmlout.output(doc, new FileOutputStream(new File("Report1.hbm.xml")));
      trx.commit();
      session.flush();
      session.close();
     
       System.out.println("Report2");
       Configuration config1=new Configuration();
       config1.configure("/hibernate.cfg.xml");
       config1.addResource("Report2.hbm.xml");
       SessionFactory sf1=config1.buildSessionFactory();
       Session session1=sf1.openSession();
       Transaction trx1=session1.beginTransaction();
            results=session1.createQuery("select r.name from Report1 r").iterate();
              while(results.hasNext())
              System.out.println(results.next().toString());
      trx1.commit();
     
      session1.flush();
      session1.close();


But this code only retrieve data from report_1 table even mapping hbm file table attribute changed to report_2.

Due to this our project on the binding state. Hibernate performance also very slow.
Becaz our project has to handle large table and table are dynamically created.
So plz help to solve this problem.

_________________
If u feel it will help you, don't forget to rate me....


Top
 Profile  
 
 Post subject:
PostPosted: Wed Nov 05, 2008 8:46 am 
Expert
Expert

Joined: Wed Mar 03, 2004 6:35 am
Posts: 1240
Location: Lund, Sweden
I really don't know what you are trying to achieve with this. Building a SessionFactory is expensive and should be done at application startup only.

In any case, it seems like the modified DOM is saved to Report1.hbm.xml but your second Configuration is reading Report2.hbm.xml.


Top
 Profile  
 
 Post subject:
PostPosted: Wed Nov 05, 2008 8:53 am 
Regular
Regular

Joined: Wed Oct 15, 2008 6:59 am
Posts: 103
Location: Chennai
sorry i don't update that code.
acc i try to save in other file. it works fine.
but my superior ask me do this by one HBM for multiple table.

suggests me some article or books for better hibernate coding.

_________________
If u feel it will help you, don't forget to rate me....


Top
 Profile  
 
 Post subject:
PostPosted: Wed Nov 05, 2008 9:03 am 
Expert
Expert

Joined: Wed Mar 03, 2004 6:35 am
Posts: 1240
Location: Lund, Sweden
It is a strange requirement to say that you can only use one hbm file. If you are just going to read from the tables there is an option to use SQL queries that maps to entities. With this you can read data as entites from any table. Read more here: http://www.hibernate.org/hib_docs/v3/re ... l-creating

I would think that the "Java Persistence with Hibernate" book that is advertised in this forum is a very good book. I have only read the first edition (Hibernate in Action) which really saved our project when it was going in the wrong direction.


Top
 Profile  
 
 Post subject:
PostPosted: Wed Nov 05, 2008 9:12 am 
Regular
Regular

Joined: Wed Oct 15, 2008 6:59 am
Posts: 103
Location: Chennai
very grateful to ur answer friend..

in our project each day has different table for report generation.
but schema same and name appends it's date.

when i ask abt Hibernate Sharding.(MySQL horizontal partition). he told me to do this by one hbm file for all tables.

is there any solution for this Dynamic table mapping problem.

_________________
If u feel it will help you, don't forget to rate me....


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.