-->
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.  [ 7 posts ] 
Author Message
 Post subject: Reg : two hbm file for one java class...........
PostPosted: Mon Jan 12, 2009 3:19 am 
Newbie

Joined: Fri Jan 02, 2009 2:14 am
Posts: 15
Hi All,

I have 2 tables 'A'table, 'B'table, having both same columns.
My question is.. can i use one java file "setter/getter" for both tables like writing 2 hbm files for one java file? is it possible , or ?


Top
 Profile  
 
 Post subject:
PostPosted: Mon Jan 12, 2009 3:26 am 
Expert
Expert

Joined: Wed Mar 03, 2004 6:35 am
Posts: 1240
Location: Lund, Sweden
Yes it is possible with <class class="..." entity-name="..." > . See http://www.hibernate.org/hib_docs/v3/re ... yname.html for more information.


Top
 Profile  
 
 Post subject: Reg : two hbm file for one java class...........
PostPosted: Mon Jan 12, 2009 3:53 am 
Newbie

Joined: Fri Jan 02, 2009 2:14 am
Posts: 15
my question is that.. can i write 2 hbm files for 2 tables... and use only one java file(setter/getter).. because 2 tables have same column names..
is it posible?


Top
 Profile  
 
 Post subject:
PostPosted: Mon Jan 12, 2009 4:04 am 
Expert
Expert

Joined: Wed Mar 03, 2004 6:35 am
Posts: 1240
Location: Lund, Sweden
A.hbm.xml
Code:
<class name="TheClass" entity-name="A" table="A">
.... property definitions for all getters/setters
</class>


B.hbm.xml
Code:
<class name="TheClass" entity-name="B" table="B">
.... property definitions for all getters/setters
</class>


Top
 Profile  
 
 Post subject:
PostPosted: Mon Jan 12, 2009 4:10 am 
Regular
Regular

Joined: Wed Oct 15, 2008 6:59 am
Posts: 103
Location: Chennai
This is sample code for problem stated in the following post...

http://forum.hibernate.org/viewtopic.php?t=991814

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();
Iterator results=session.createQuery("from Report1 r").iterate();
Logger log = Logger.getLogger(Report1.class);
while(results.hasNext())
{
Report1 r=(Report1)results.next();
System.out.println(r.getName());
}

log.info("Changing Table value dynamically");
PersistentClass pClass = config.getClassMapping("com.hcl.Report1");
System.out.println();
pClass.getTable().setName("report_2");

log.info("Build new SessionFactory");
sf=config.buildSessionFactory();
sf.evict(Report1.class);
session.evict(Report1.class);
trx.commit();
session.flush();
session.close();


System.out.println(config.getClassMapping("com.hcl.Report1").getTable().getName());
Session session1=sf.openSession();
Transaction trx1=session1.beginTransaction();
results=session1.createQuery("from Report1 r").iterate();
while(results.hasNext())
{
Report1 r=(Report1)results.next();
System.out.println(r.getName());
}

Report1 r=new Report1();
r.setName("hibernate");
session1.save(r);
r.setName("jack");
trx1.commit();
session1.flush();
session1.close();
}

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


Top
 Profile  
 
 Post subject:
PostPosted: Mon Jan 12, 2009 4:12 am 
Newbie

Joined: Fri Jan 02, 2009 2:14 am
Posts: 15
Hi

i want to use one getters/setters for both hbm? is it possible?

A.hbm.xml
<class name="TheClass" entity-name="A" table="A">
</class>



B.hbm.xml
<class name="TheClass" entity-name="B" table="B">
</class>


class TheClass
{

setter/getters
....
...
..

}


Top
 Profile  
 
 Post subject:
PostPosted: Mon Jan 12, 2009 4:14 am 
Regular
Regular

Joined: Wed Oct 15, 2008 6:59 am
Posts: 103
Location: Chennai
Use that sample code for accessing different tables(same schema) with one POJO file.
for that u have to map that table for that POJO, so i use PresistentClass object to chnage the table attribute in hbm file to other table name.

if u go through my code and my post u will get wat u want..

_________________
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.  [ 7 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.