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();
}
}