-->
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.  [ 5 posts ] 
Author Message
 Post subject: User-Provided JDBC Connections
PostPosted: Thu Sep 21, 2006 2:46 pm 
Newbie

Joined: Fri Mar 19, 2004 3:53 pm
Posts: 6
I have a test database and a production database (most of us do). I want to be able to take data exactly as it exists from my production database and save it into my test database. The hibernate configuration files and the POJOs are the same regardless of datasource. I want to know if it is possible to populate the POJOs while connected to the production datasource and then change my datasource on the fly and save the POJOs to my test datasource.
I am using Hibernate 3.
I am connecting to a Test DB2 environment and a Production DB2 environment.
I am still relatively new to Hibernate. So bear with me.
Thanks,
Christopher


Top
 Profile  
 
 Post subject:
PostPosted: Fri Sep 22, 2006 12:15 am 
Newbie

Joined: Tue May 10, 2005 2:32 pm
Posts: 14
Location: Quito, Ecuador South America
you could set up a second session factory that connects to your test database and then copy from production POJOS to test POJOS. For instance if you have com.yourapp.model.PojoOne you will need a com.yourapp.model.test.PojoOne, then use BeanUtils to copy from
com.yourapp.model.PojoOne to com.yourapp.model.test.PojoOne and that it.good luck.

_________________
Hernan Leon
Mindsoft


Top
 Profile  
 
 Post subject:
PostPosted: Fri Sep 22, 2006 9:30 am 
Newbie

Joined: Fri Mar 19, 2004 3:53 pm
Posts: 6
hernanleon wrote:
you could set up a second session factory that connects to your test database and then copy from production POJOS to test POJOS. For instance if you have com.yourapp.model.PojoOne you will need a com.yourapp.model.test.PojoOne, then use BeanUtils to copy from
com.yourapp.model.PojoOne to com.yourapp.model.test.PojoOne and that it.good luck.


I am using Spring + Hibernate - sorry I didn't mention that in my earlier post.
I have defined a test sessionFactory in my applicationContext with a test DataSource. I have followed your advice and created a test POJO and placed its mapping in the list of mappingResources in the applicationContext. The only problem is that I feel like I am going to have to create two new test packages; com.yourapp.test.dao and com.yourapp.dao.test.hibernate to accomodate this strategy. It is certainly something I could do, but it would require a lot of duplicate code since both databases have the same schema and the corresponding methods in the test.dao and .dao would be the same.
Is there a way for me to retrieve the Production Pojo from the database using getHibernateTemplate().find() in the com.yourapp.dao.hibernate.PojoOneDaoHibernate class which is mapped to the production sessionFactory and then do a getHibernateTemplate().setSessionFactory(arg0) that programmatically sets the sessionFactory to the testSessionFactory that I have defined in the applicationContext? If I can programmatically change to the new sessionFactory then I could simply copy the production Pojos into test Pojos and call getHibernateTemplate.save() and they would be saved through the testSessionFactory and into the test datasource.


Top
 Profile  
 
 Post subject:
PostPosted: Fri Sep 22, 2006 6:11 pm 
Newbie

Joined: Tue May 10, 2005 2:32 pm
Posts: 14
Location: Quito, Ecuador South America
Hello, Sorry but I am not familiar with Spring yet, so I do not Know if what I am about to tell you could help you.
You want to copy from
com.yourapp.dao.hibernate.PojoOneDaoHibernate (production) to
com.yourapp.dao.test.PojoOneDaoHibernate (test) right.

What I used to do is create SessionFactoryManager class that stores statically (in some static Hashtable property) references to all org.hibernate.SessionFactory builded for my application.
For 2 databases I will need 2 different SessionFactory each one with a different Id, so I can call SessionFactoryManager.getSession(String id).

With this approach you can code something like this
Code:

org.hibernate.Session sessProd =
SessionFactoryManager.getSession"prodId").openSession();
//since SessionFactoryManager hava a Hashtable with SessionFactory

PojoOneDaoHibernate prod = sess.get(......);
com.yourapp.dao.test.PojoOneDaoHibernate test = new PojoOneDaoHibernate ();
PropertyUtils.copyProperties(test, prod );

org.hibernate.Session sessTest =
SessionFactoryManager.getSession"testId").openSession();

sessTest.saveOrUpdate(test);

sessTest.close();
sessProd.close();


[quote]

That is the idea, well you will need to code it right off course but this should work, as a matter of fact I have used it for copy from one database to another.
Hope it works!!, let me Know.[/quote]

_________________
Hernan Leon
Mindsoft


Top
 Profile  
 
 Post subject:
PostPosted: Fri Sep 22, 2006 6:12 pm 
Newbie

Joined: Tue May 10, 2005 2:32 pm
Posts: 14
Location: Quito, Ecuador South America
Hello, Sorry but I am not familiar with Spring yet, so I do not Know if what I am about to tell you could help you.
You want to copy from
com.yourapp.dao.hibernate.PojoOneDaoHibernate (production) to
com.yourapp.dao.test.PojoOneDaoHibernate (test) right.

What I used to do is create SessionFactoryManager class that stores statically (in some static Hashtable property) references to all org.hibernate.SessionFactory builded for my application.
For 2 databases I will need 2 different SessionFactory each one with a different Id, so I can call SessionFactoryManager.getSession(String id).

With this approach you can code something like this
Code:

org.hibernate.Session sessProd =
SessionFactoryManager.getSession"prodId").openSession();
//since SessionFactoryManager hava a Hashtable with SessionFactory

PojoOneDaoHibernate prod = sess.get(......);
com.yourapp.dao.test.PojoOneDaoHibernate test = new PojoOneDaoHibernate ();
PropertyUtils.copyProperties(test, prod );

org.hibernate.Session sessTest =
SessionFactoryManager.getSession"testId").openSession();

sessTest.saveOrUpdate(test);

sessTest.close();
sessProd.close();



That is the idea, well you will need to code it right off course but this should work, as a matter of fact I have used it for copy from one database to another.
Hope it works!!, let me Know

_________________
Hernan Leon
Mindsoft


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