You are viewing a plain text version of this content. The canonical link for it is here.
Posted to users@maven.apache.org by Stephane-3 <mi...@yahoo.se> on 2012/11/23 07:15:34 UTC

DAO injection in integration tests with a few database server specific methods

Hi,

I have a Spring Hibernate project with a DAO coded against a MySql database.
Most of the DAO methods would run on HSQLDB and on Oracle, except for some
that contain database server specific syntax.

I already use some Maven profiles to filter the database server data source
specific properties.

Now I wonder which way to go so as to have a few DAO methods specific to
each database server.

I would like to keep most of the DAO methods in a common location, and only
have the database server specific DAO methods in anpther one.

So for example, for the Oracle database server:

Should the Oracle specific DAO methods sit in a different Java package ?

Should there be a different resource xml file to have the beans for the
integration tests injection of these specific DAO methods ?

Here is my setup as of today:

The abstract test class:

@ContextConfiguration(locations = { "classpath:spring-hibernate.xml",
"classpath:spring-hibernate-dao.xml", "classpath:spring-data-source.xml",
"classpath:log4j.xml" })
public abstract class AbstractDaoTest extends
AbstractTransactionalJUnit4SpringContextTests {
}

A Maven profile for the Oracle database server:

    <profile>
      <id>oracle-test</id>
      <properties>
        <test.source.dir>src/integration/java</test.source.dir>
      </properties>
      <build>
        <filters>
         
<filter>src/integration/filters/data-source.oracle.properties</filter>
        </filters>
        <resources>
          <resource>
            <directory>src/main/resources</directory>
            <filtering>true</filtering>
          </resource>
          <resource>
            <directory>src/integration/resources</directory>
            <filtering>true</filtering>
          </resource>
        </resources>
      </build>
    </profile>

The spring-hibernate-dao.xml file content:

<beans>

	<bean id="addressDao"
		class="com.thalasoft.learnintouch.core.dao.hibernate.AddressHibernateDao">
		<property name="sessionFactory" ref="sessionFactory" />
	</bean>

	<bean id="adminDao"
		class="com.thalasoft.learnintouch.core.dao.hibernate.AdminHibernateDao">
		<property name="sessionFactory" ref="sessionFactory" />
	</bean>

etc...
</beans>





--
View this message in context: http://maven.40175.n5.nabble.com/DAO-injection-in-integration-tests-with-a-few-database-server-specific-methods-tp5732576.html
Sent from the Maven - Users mailing list archive at Nabble.com.

---------------------------------------------------------------------
To unsubscribe, e-mail: users-unsubscribe@maven.apache.org
For additional commands, e-mail: users-help@maven.apache.org


Re: DAO injection in integration tests with a few database server specific methods

Posted by Stephane-3 <mi...@yahoo.se>.
Thanks for your sharing, I have posted on the Hibernate forum,
https://forum.hibernate.org/viewtopic.php?f=1&t=1024833 Thanks for the
interest.



--
View this message in context: http://maven.40175.n5.nabble.com/DAO-injection-in-integration-tests-with-a-few-database-server-specific-methods-tp5732576p5732972.html
Sent from the Maven - Users mailing list archive at Nabble.com.

---------------------------------------------------------------------
To unsubscribe, e-mail: users-unsubscribe@maven.apache.org
For additional commands, e-mail: users-help@maven.apache.org


Re: DAO injection in integration tests with a few database server specific methods

Posted by Markku Saarela <ma...@iki.fi>.
Not on this list, this is for Maven.

Markku
On 11/24/2012 08:28 PM, Martin Gainty wrote:
> so.. i guess the short answer is no there is no way to introduce hints into Hibernate generated queries.. Takk,
> Martin______________________________________________
> Verzicht und Vertraulichkeitanmerkung/Note de déni et de confidentialité
>
> Diese Nachricht ist vertraulich. Sollten Sie nicht der vorgesehene Empfaenger sein, so bitten wir hoeflich um eine Mitteilung. Jede unbefugte Weiterleitung oder Fertigung einer Kopie ist unzulaessig. Diese Nachricht dient lediglich dem Austausch von Informationen und entfaltet keine rechtliche Bindungswirkung. Aufgrund der leichten Manipulierbarkeit von E-Mails koennen wir keine Haftung fuer den Inhalt uebernehmen.
> Ce message est confidentiel et peut être privilégié. Si vous n'êtes pas le destinataire prévu, nous te demandons avec bonté que pour satisfaire informez l'expéditeur. N'importe quelle diffusion non autorisée ou la copie de ceci est interdite. Ce message sert à l'information seulement et n'aura pas n'importe quel effet légalement obligatoire. Étant donné que les email peuvent facilement être sujets à la manipulation, nous ne pouvons accepter aucune responsabilité pour le contenu fourni.
>
>   > Date: Sat, 24 Nov 2012 19:42:49 +0200
>> From: markku.saarela@iki.fi
>> To: users@maven.apache.org
>> Subject: Re: DAO injection in integration tests with a few database server specific methods
>>
>> Use Spring and Hibernate list for questions related on those.
>>
>> Markku
>>
>> On 11/24/2012 05:36 PM, Martin Gainty wrote:
>>> that *should* work as Hibernate Session would generate an List of Persisted NavbarLanguage Classes (which your method returns) ..but...the generated Query would not be hinted..so optimizing the query is not enabled
>>> does anyone know to introduce Hints into Hibernate generated queries?
>>> Martin
>>> ______________________________________________
>>> Place long winded disclaimer here > Date: Fri, 23 Nov 2012 01:55:57 -0800
>>>> From: mittiprovence@yahoo.se
>>>> To: users@maven.apache.org
>>>> Subject: Re: DAO injection in integration tests with a few database server specific methods
>>>>
>>>> Here is how I'm using the injected custom DAO bean method:
>>>>
>>>> 	public List<NavbarLanguage> findWithNavbar(Navbar navbar) {
>>>> 		if (navbarLanguageCustomDao != null) {
>>>> 			return navbarLanguageCustomDao.findWithNavbar(navbar);
>>>> 		} else {
>>>> 			Criteria criteria = getSession().createCriteria(getPersistentClass());
>>>> 			criteria.add(Restrictions.eq("navbar",
>>>> navbar)).addOrder(Order.asc("languageCode"));
>>>> 			return criteria.list();
>>>> 		}
>>>> 	}
>>>>
>>>> Not sure if it's the best way to go though :-)
>>>>
>>>>
>>>>
>>>> --
>>>> View this message in context: http://maven.40175.n5.nabble.com/DAO-injection-in-integration-tests-with-a-few-database-server-specific-methods-tp5732576p5732611.html
>>>> Sent from the Maven - Users mailing list archive at Nabble.com.
>>>>
>>>> ---------------------------------------------------------------------
>>>> To unsubscribe, e-mail: users-unsubscribe@maven.apache.org
>>>> For additional commands, e-mail: users-help@maven.apache.org
>>>>
>>>    		 	   		
>>
>> ---------------------------------------------------------------------
>> To unsubscribe, e-mail: users-unsubscribe@maven.apache.org
>> For additional commands, e-mail: users-help@maven.apache.org
>>
>   		 	   		


---------------------------------------------------------------------
To unsubscribe, e-mail: users-unsubscribe@maven.apache.org
For additional commands, e-mail: users-help@maven.apache.org


RE: DAO injection in integration tests with a few database server specific methods

Posted by Martin Gainty <mg...@hotmail.com>.
so.. i guess the short answer is no there is no way to introduce hints into Hibernate generated queries.. Takk,
Martin______________________________________________ 
Verzicht und Vertraulichkeitanmerkung/Note de déni et de confidentialité

Diese Nachricht ist vertraulich. Sollten Sie nicht der vorgesehene Empfaenger sein, so bitten wir hoeflich um eine Mitteilung. Jede unbefugte Weiterleitung oder Fertigung einer Kopie ist unzulaessig. Diese Nachricht dient lediglich dem Austausch von Informationen und entfaltet keine rechtliche Bindungswirkung. Aufgrund der leichten Manipulierbarkeit von E-Mails koennen wir keine Haftung fuer den Inhalt uebernehmen.
Ce message est confidentiel et peut être privilégié. Si vous n'êtes pas le destinataire prévu, nous te demandons avec bonté que pour satisfaire informez l'expéditeur. N'importe quelle diffusion non autorisée ou la copie de ceci est interdite. Ce message sert à l'information seulement et n'aura pas n'importe quel effet légalement obligatoire. Étant donné que les email peuvent facilement être sujets à la manipulation, nous ne pouvons accepter aucune responsabilité pour le contenu fourni.

 > Date: Sat, 24 Nov 2012 19:42:49 +0200
> From: markku.saarela@iki.fi
> To: users@maven.apache.org
> Subject: Re: DAO injection in integration tests with a few database server specific methods
> 
> Use Spring and Hibernate list for questions related on those.
> 
> Markku
> 
> On 11/24/2012 05:36 PM, Martin Gainty wrote:
> > that *should* work as Hibernate Session would generate an List of Persisted NavbarLanguage Classes (which your method returns) ..but...the generated Query would not be hinted..so optimizing the query is not enabled
> > does anyone know to introduce Hints into Hibernate generated queries?
> > Martin
> > ______________________________________________
> > Place long winded disclaimer here > Date: Fri, 23 Nov 2012 01:55:57 -0800
> >> From: mittiprovence@yahoo.se
> >> To: users@maven.apache.org
> >> Subject: Re: DAO injection in integration tests with a few database server specific methods
> >>
> >> Here is how I'm using the injected custom DAO bean method:
> >>
> >> 	public List<NavbarLanguage> findWithNavbar(Navbar navbar) {
> >> 		if (navbarLanguageCustomDao != null) {
> >> 			return navbarLanguageCustomDao.findWithNavbar(navbar);
> >> 		} else {
> >> 			Criteria criteria = getSession().createCriteria(getPersistentClass());
> >> 			criteria.add(Restrictions.eq("navbar",
> >> navbar)).addOrder(Order.asc("languageCode"));
> >> 			return criteria.list();
> >> 		}
> >> 	}
> >>
> >> Not sure if it's the best way to go though :-)
> >>
> >>
> >>
> >> --
> >> View this message in context: http://maven.40175.n5.nabble.com/DAO-injection-in-integration-tests-with-a-few-database-server-specific-methods-tp5732576p5732611.html
> >> Sent from the Maven - Users mailing list archive at Nabble.com.
> >>
> >> ---------------------------------------------------------------------
> >> To unsubscribe, e-mail: users-unsubscribe@maven.apache.org
> >> For additional commands, e-mail: users-help@maven.apache.org
> >>
> >   		 	   		
> 
> 
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: users-unsubscribe@maven.apache.org
> For additional commands, e-mail: users-help@maven.apache.org
> 
 		 	   		  

Re: DAO injection in integration tests with a few database server specific methods

Posted by Markku Saarela <ma...@iki.fi>.
Use Spring and Hibernate list for questions related on those.

Markku

On 11/24/2012 05:36 PM, Martin Gainty wrote:
> that *should* work as Hibernate Session would generate an List of Persisted NavbarLanguage Classes (which your method returns) ..but...the generated Query would not be hinted..so optimizing the query is not enabled
> does anyone know to introduce Hints into Hibernate generated queries?
> Martin
> ______________________________________________
> Place long winded disclaimer here > Date: Fri, 23 Nov 2012 01:55:57 -0800
>> From: mittiprovence@yahoo.se
>> To: users@maven.apache.org
>> Subject: Re: DAO injection in integration tests with a few database server specific methods
>>
>> Here is how I'm using the injected custom DAO bean method:
>>
>> 	public List<NavbarLanguage> findWithNavbar(Navbar navbar) {
>> 		if (navbarLanguageCustomDao != null) {
>> 			return navbarLanguageCustomDao.findWithNavbar(navbar);
>> 		} else {
>> 			Criteria criteria = getSession().createCriteria(getPersistentClass());
>> 			criteria.add(Restrictions.eq("navbar",
>> navbar)).addOrder(Order.asc("languageCode"));
>> 			return criteria.list();
>> 		}
>> 	}
>>
>> Not sure if it's the best way to go though :-)
>>
>>
>>
>> --
>> View this message in context: http://maven.40175.n5.nabble.com/DAO-injection-in-integration-tests-with-a-few-database-server-specific-methods-tp5732576p5732611.html
>> Sent from the Maven - Users mailing list archive at Nabble.com.
>>
>> ---------------------------------------------------------------------
>> To unsubscribe, e-mail: users-unsubscribe@maven.apache.org
>> For additional commands, e-mail: users-help@maven.apache.org
>>
>   		 	   		


---------------------------------------------------------------------
To unsubscribe, e-mail: users-unsubscribe@maven.apache.org
For additional commands, e-mail: users-help@maven.apache.org


RE: DAO injection in integration tests with a few database server specific methods

Posted by Martin Gainty <mg...@hotmail.com>.
that *should* work as Hibernate Session would generate an List of Persisted NavbarLanguage Classes (which your method returns) ..but...the generated Query would not be hinted..so optimizing the query is not enabled
does anyone know to introduce Hints into Hibernate generated queries?
Martin 
______________________________________________ 
Place long winded disclaimer here > Date: Fri, 23 Nov 2012 01:55:57 -0800
> From: mittiprovence@yahoo.se
> To: users@maven.apache.org
> Subject: Re: DAO injection in integration tests with a few database server specific methods
> 
> Here is how I'm using the injected custom DAO bean method:
> 
> 	public List<NavbarLanguage> findWithNavbar(Navbar navbar) {
> 		if (navbarLanguageCustomDao != null) {
> 			return navbarLanguageCustomDao.findWithNavbar(navbar);
> 		} else {
> 			Criteria criteria = getSession().createCriteria(getPersistentClass());
> 			criteria.add(Restrictions.eq("navbar",
> navbar)).addOrder(Order.asc("languageCode"));
> 			return criteria.list();
> 		}
> 	}
> 
> Not sure if it's the best way to go though :-)
> 
> 
> 
> --
> View this message in context: http://maven.40175.n5.nabble.com/DAO-injection-in-integration-tests-with-a-few-database-server-specific-methods-tp5732576p5732611.html
> Sent from the Maven - Users mailing list archive at Nabble.com.
> 
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: users-unsubscribe@maven.apache.org
> For additional commands, e-mail: users-help@maven.apache.org
> 
 		 	   		  

Re: DAO injection in integration tests with a few database server specific methods

Posted by Stephane-3 <mi...@yahoo.se>.
Here is how I'm using the injected custom DAO bean method:

	public List<NavbarLanguage> findWithNavbar(Navbar navbar) {
		if (navbarLanguageCustomDao != null) {
			return navbarLanguageCustomDao.findWithNavbar(navbar);
		} else {
			Criteria criteria = getSession().createCriteria(getPersistentClass());
			criteria.add(Restrictions.eq("navbar",
navbar)).addOrder(Order.asc("languageCode"));
			return criteria.list();
		}
	}

Not sure if it's the best way to go though :-)



--
View this message in context: http://maven.40175.n5.nabble.com/DAO-injection-in-integration-tests-with-a-few-database-server-specific-methods-tp5732576p5732611.html
Sent from the Maven - Users mailing list archive at Nabble.com.

---------------------------------------------------------------------
To unsubscribe, e-mail: users-unsubscribe@maven.apache.org
For additional commands, e-mail: users-help@maven.apache.org


Re: DAO injection in integration tests with a few database server specific methods

Posted by Stephane-3 <mi...@yahoo.se>.
My mistake... The custom DAO bean implementation class was missing the
implements NavbarLanguageCustomDao statement. I added it. No more exception
any more !




--
View this message in context: http://maven.40175.n5.nabble.com/DAO-injection-in-integration-tests-with-a-few-database-server-specific-methods-tp5732576p5732609.html
Sent from the Maven - Users mailing list archive at Nabble.com.

---------------------------------------------------------------------
To unsubscribe, e-mail: users-unsubscribe@maven.apache.org
For additional commands, e-mail: users-help@maven.apache.org


Re: DAO injection in integration tests with a few database server specific methods

Posted by Stephane-3 <mi...@yahoo.se>.
Yes, it should be the same method signature, but with an implementation that
is specific to each database server.

I created another spring-hibernate-custom-dao.xml file in a
src/main/resources/oracle directory listed in the Maven oracle-test profile,
which contains the custom DAO bean definition:
	<bean id="navbarLanguageCustomDao"

class="com.thalasoft.learnintouch.core.dao.oracle.hibernate.NavbarLanguageCustomHibernateDao">
		<property name="sessionFactory" ref="sessionFactory" />
	</bean>

This bean is then injected in the existing common DAO bean with:
	<bean id="navbarLanguageDao"
	
class="com.thalasoft.learnintouch.core.dao.hibernate.NavbarLanguageHibernateDao">
		<property name="sessionFactory" ref="sessionFactory" />
		<property name="navbarLanguageCustomDao" ref="navbarLanguageCustomDao" />
	</bean>

And the existing common DAO class can now use the injected custom DAO with:
	private NavbarLanguageCustomDao navbarLanguageCustomDao;
	public void setNavbarLanguageCustomDao(NavbarLanguageCustomDao
navbarLanguageCustomDao) {
		this.navbarLanguageCustomDao = navbarLanguageCustomDao;
	}

I think I'm almost there, but I still get an exception:

Caused by: java.lang.IllegalStateException: Cannot convert value of type
[$Proxy14 implementing
com.thalasoft.learnintouch.core.dao.GenericDao,org.springframework.aop.SpringProxy,org.springframework.aop.framework.Advised]
to required type
[com.thalasoft.learnintouch.core.dao.oracle.NavbarLanguageCustomDao] for
property 'navbarLanguageCustomDao': no matching editors or conversion
strategy found





--
View this message in context: http://maven.40175.n5.nabble.com/DAO-injection-in-integration-tests-with-a-few-database-server-specific-methods-tp5732576p5732604.html
Sent from the Maven - Users mailing list archive at Nabble.com.

---------------------------------------------------------------------
To unsubscribe, e-mail: users-unsubscribe@maven.apache.org
For additional commands, e-mail: users-help@maven.apache.org


Re: DAO injection in integration tests with a few database server specific methods

Posted by Samuli Saarinen <sa...@remion.com>.
When you say
"Now I wonder which way to go so as to have a few DAO methods specific 
to each database server."
do you mean that you have different methods for different databases or 
same methods that execute different SQL?

I was thinking that it is the latter and if so I think you can't avoid 
having the configuration. It's only a matter of where the configuration 
is at. Another way could be that you have the sql externalized and 
inject that based on the configuration.

br,
Samuli




On 23.11.2012 10:08, Stephane-3 wrote:
> Hi Samuli,
>
> Yes, it's probably more of a Spring configuration question than a Maven one.
>
> What I would like to avoid is having to create lots of DAO configuration
> beans for nothing.
>
> My aim is to create a DAO configuration bean and inject it only when there
> is a requirement for a database server specific sql statement.
>
> Kind Regards,
>
>
>
>
> --
> View this message in context: http://maven.40175.n5.nabble.com/DAO-injection-in-integration-tests-with-a-few-database-server-specific-methods-tp5732576p5732594.html
> Sent from the Maven - Users mailing list archive at Nabble.com.
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: users-unsubscribe@maven.apache.org
> For additional commands, e-mail: users-help@maven.apache.org
>



-- 
Remion Oy           Etävalvontajärjestelmät liiketoiminnan
Samuli Saarinen           tehostamiseen
gsm +358 (0)50 3560075
fax +358 (0)3 2125064       www.remion.com

---------------------------------------------------------------------
To unsubscribe, e-mail: users-unsubscribe@maven.apache.org
For additional commands, e-mail: users-help@maven.apache.org


Re: DAO injection in integration tests with a few database server specific methods

Posted by Stephane-3 <mi...@yahoo.se>.
Hi Samuli,

Yes, it's probably more of a Spring configuration question than a Maven one.

What I would like to avoid is having to create lots of DAO configuration
beans for nothing.

My aim is to create a DAO configuration bean and inject it only when there
is a requirement for a database server specific sql statement.

Kind Regards,




--
View this message in context: http://maven.40175.n5.nabble.com/DAO-injection-in-integration-tests-with-a-few-database-server-specific-methods-tp5732576p5732594.html
Sent from the Maven - Users mailing list archive at Nabble.com.

---------------------------------------------------------------------
To unsubscribe, e-mail: users-unsubscribe@maven.apache.org
For additional commands, e-mail: users-help@maven.apache.org


Re: DAO injection in integration tests with a few database server specific methods

Posted by Samuli Saarinen <sa...@remion.com>.
Hi,

I don't know if this really is a maven issue but could you isolate the 
db spesific stuff into their own classes and then use spring to inject 
the desired bean per configuration.

I'm not a spring expert so there might be simpler ways to achive this 
but something along the lines

The spring-hibernate-dao-mysql.xml file content:

  <beans>

  	<bean id="addressDao" 
class="com.thalasoft.learnintouch.core.dao.hibernate.AddressHibernateDao">
  		<property name="sessionFactory" ref="sessionFactory" />
  		<property name="nativeAdapter" ref="addressMysqlNativeBean" />
	</bean>

	<bean id="addressMysqlnativeBean" .... </bean>

</beans>


and for hsqldb you would have spring-hibernate-dao-hsqldb.xml with <bean 
id="addressHsqldbNativeBean" /> that you could use with your tests.

br,
Samuli

On 23.11.2012 8:15, Stephane-3 wrote:
> Hi,
>
> I have a Spring Hibernate project with a DAO coded against a MySql database.
> Most of the DAO methods would run on HSQLDB and on Oracle, except for some
> that contain database server specific syntax.
>
> I already use some Maven profiles to filter the database server data source
> specific properties.
>
> Now I wonder which way to go so as to have a few DAO methods specific to
> each database server.
>
> I would like to keep most of the DAO methods in a common location, and only
> have the database server specific DAO methods in anpther one.
>
> So for example, for the Oracle database server:
>
> Should the Oracle specific DAO methods sit in a different Java package ?
>
> Should there be a different resource xml file to have the beans for the
> integration tests injection of these specific DAO methods ?
>
> Here is my setup as of today:
>
> The abstract test class:
>
> @ContextConfiguration(locations = { "classpath:spring-hibernate.xml",
> "classpath:spring-hibernate-dao.xml", "classpath:spring-data-source.xml",
> "classpath:log4j.xml" })
> public abstract class AbstractDaoTest extends
> AbstractTransactionalJUnit4SpringContextTests {
> }
>
> A Maven profile for the Oracle database server:
>
>      <profile>
>        <id>oracle-test</id>
>        <properties>
>          <test.source.dir>src/integration/java</test.source.dir>
>        </properties>
>        <build>
>          <filters>
>
> <filter>src/integration/filters/data-source.oracle.properties</filter>
>          </filters>
>          <resources>
>            <resource>
>              <directory>src/main/resources</directory>
>              <filtering>true</filtering>
>            </resource>
>            <resource>
>              <directory>src/integration/resources</directory>
>              <filtering>true</filtering>
>            </resource>
>          </resources>
>        </build>
>      </profile>
>
> The spring-hibernate-dao.xml file content:
>
> <beans>
>
> 	<bean id="addressDao"
> 		class="com.thalasoft.learnintouch.core.dao.hibernate.AddressHibernateDao">
> 		<property name="sessionFactory" ref="sessionFactory" />
> 	</bean>
>
> 	<bean id="adminDao"
> 		class="com.thalasoft.learnintouch.core.dao.hibernate.AdminHibernateDao">
> 		<property name="sessionFactory" ref="sessionFactory" />
> 	</bean>
>
> etc...
> </beans>
>
>
>
>
>
> --
> View this message in context: http://maven.40175.n5.nabble.com/DAO-injection-in-integration-tests-with-a-few-database-server-specific-methods-tp5732576.html
> Sent from the Maven - Users mailing list archive at Nabble.com.
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: users-unsubscribe@maven.apache.org
> For additional commands, e-mail: users-help@maven.apache.org
>



-- 
Remion Oy           Etävalvontajärjestelmät liiketoiminnan
Samuli Saarinen           tehostamiseen
gsm +358 (0)50 3560075
fax +358 (0)3 2125064       www.remion.com

---------------------------------------------------------------------
To unsubscribe, e-mail: users-unsubscribe@maven.apache.org
For additional commands, e-mail: users-help@maven.apache.org