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.
For that i just open hbm file and change only the table name by SAXBuilder and save into the same file. When i run this program it only retrieves data from report_1 table only.
But when i save the modified hbm file into another file, it works fine(the first block get data from report_1 and second from report_2).
Plz help me to solve this problem, our project on binding for this purpose.
it's my mapping file:
Report1.hbm.xml
<?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="magnettelx">
<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 main coding
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();