I'm fighting with a strange behavior here...
Doing an asp.net mvc3 application with NHibernate as ORM and MS SQL Server 2008 as the DB, I'm running into these exeption:
System.NotSupportedException: Dialect does not support variable limits.
The code is pretty simple, a classic pagination query:
public IList<Agenzia> getAllAgenzie(int maximumRows, int startRowIndex)
{
using (var session = PersistenceManager.Istance.GetSession()) {
var result = (from agenzia in session.Query<Agenzia>()
select agenzia)
.Skip(startRowIndex)
.Take(maximumRows)
.ToList();
return result;
}
}
And here's the NHibernate configuration
<?xml version="1.0" encoding="utf-8" ?>
<!-- NHibernate Configuration -->
<hibernate-configuration xmlns="urn:nhibernate-configuration-2.2">
<session-factory name="NHibernate.xlns">
<property name="dialect">
NHibernate.Dialect.MsSql2008Dialect
</property>
<property name="connection.driver_class">
NHibernate.Driver.SqlClientDriver
</property>
<property name="show_sql">true</property>
</session-factory>
</hibernate-configuration>
Any idea what's wrong with it? I can't believe that NHibernate don't support this simple pagination...
What version of NHibernate are you using? (latest is 3.2)
It works without problems for me.
Also, this is redundant:
(from agenzia in session.Query<Agenzia>() select agenzia)
It's equivalent to:
session.Query<Agenzia>()
Related
I'm trying to upgrade a current project from Jackson 1.9 to 2.5. Everything was going well until I tried to startup my WAS 7 server and receive this error:
org.springframework.beans.factory.CannotLoadBeanClassException: Error
loading class
[com.fasterxml.jackson.jaxrs.json.JacksonJaxbJsonProvider] for bean
with name 'jaxbProvider' defined in ServletContext resource
[/WEB-INF/spring/applicationContext-configuration.xml]: problem with
class file or dependent class; nested exception is
java.lang.NoClassDefFoundError:
com.fasterxml.jackson.jaxrs.base.ProviderBase
This appears to be in relation to trying to register the Jackson Provider in my web.xml below:
<!-- Jackson Provider -->
<bean id="jaxbProvider" class="com.fasterxml.jackson.jaxrs.json.JacksonJaxbJsonProvider" >
<property name="mapper" ref="jacksonObjectMapper"/>
</bean>
<bean id="jacksonObjectMapper" class="com.fasterxml.jackson.databind.ObjectMapper" >
<property name="annotationIntrospector" ref="jacksonAnnotationIntrospector"></property>
</bean>
<bean id="jacksonAnnotationIntrospector" class="com.fasterxml.jackson.databind.introspect.AnnotationIntrospectorPair" >
<constructor-arg ref="primaryAnnotationIntrospector" />
<constructor-arg ref="secondaryAnnotationIntrospector" />
</bean>
<bean id="primaryAnnotationIntrospector" class="com.fasterxml.jackson.module.jaxb.JaxbAnnotationIntrospector" />
<bean id="secondaryAnnotationIntrospector" class="com.fasterxml.jackson.databind.introspect.JacksonAnnotationIntrospector" />
I have the following jars on my classpath:
jackson-annotations-2.5.0.jar
jackson-core-2.5.0.jar
jackson-databind-2.5.0.jar
jackson-jaxrs-json-provider-2.5.0.jar
jackson-module-jaxb-annotations.2.5.0.jar
Now from my understanding its looking for this fellow:
com.fasterxml.jackson.jaxrs.base.ProviderBase
but can't find it. A google search reveals that class as belonging to a
jackson-jaxrs-provider project, but I can't find a specific jar for that. I think that's because that is just a base for the jackson-jaxrs-json-provider.2.5.0.jar that I already included. So shouldn't it inherently be able to see that base class through the jackson-jaxrs-json-provider.2.5.0.jar??
If anyone has an idea of what could be wrong I would be very appreciative!
Thanks.
If you used Maven, adding jackson-jaxrs-json-provider as a dependency, you will see all the following pulled in
(I had an image from another post with v2.2.3- disregard the version)
As you can see, it does depend on a jackson-jaxrs-base, which is where the ProviderBase is located.
You can download it here (just click the 2.5.0, then the Download Bundle)
Is there any way to use MySQL for JUnit testing with Spring?
I ask this because I have a lot of store procedures in MYSQL and I have noticed that H2 is not working with SPs.
Edit:
Right now my configuration for src/main looks like:
#ComponentScan
#EnableAutoConfiguration
#EnableJpaRepositories
public class Application {
public static void main(String[] args) {
ConfigurableApplicationContext applicationContext = SpringApplication.run(Application.class, args);
}
}
spring.datasource.url=jdbc:mysql://localhost/mydb
spring.datasource.username=user
spring.datasource.password=pw
spring.datasource.driver-class-name=com.mysql.jdbc.Driver
Spring works fine with this configuration. It reads the application.properties, sees the spring.datasource.* and knows that I have set a datasource.
Everything is fine here.
Now, in tests I use something like:
#RunWith(SpringJUnit4ClassRunner.class)
#ContextConfiguration(classes = {Application.class})
#Transactional
public class TxServiceTest {
}
This is not going to run the tests against MySQL database. It will run the tests with H2 database. I need to use MySQL for testing because I am using many stored procedures.
I have tried many other combinations but none of them works. What am I missing here?
Possible fix:
I somehow found a way to make it work. I let the main project unchanged and inside my test project I added a new class:
#Configuration
public class PersistenceContext {
#Bean
public DataSource dataSource() {
org.apache.tomcat.jdbc.pool.DataSource dataSource = new org.apache.tomcat.jdbc.pool.DataSource();
dataSource.setDriverClassName("com.mysql.jdbc.Driver");
dataSource.setUrl("jdbc:mysql://localhost/mydb?noAccessToProcedureBodies=true");
dataSource.setUsername("xx");
dataSource.setPassword("yy");
return dataSource;
}
}
I don't like this solution too much but it is working.
Sounds like Integration Test. First, make sure that your database is always up-to-date in order to avoid restores and work in common errors. You can use liquibase or flyway to manage deltas. Also, make sure that after execution of the tests the state of the database has not been altered. I mean if you have a table without data at the beginning, after the execution of your test this table must not have data. You can use dbunit or spring-dbunit which is an extension of dbunit for spring to manage this.
In order to use a local mysql database you can use this plugin http://mysql.jcabi.com/
EDIT 01
Replace your h2 dataSource bean:
<bean id="dataSource"
class="org.springframework.jdbc.datasource.DriverManagerDataSource">
<property name="driverClassName" value="com.mysql.jdbc.Driver" />
<property name="url" value="jdbc:mysql://localhost:3306/demo" />
<property name="username" value="root" />
<property name="password" value="root" />
</bean>
Japanese characters cannot be seen in Spring Batch Admin UI.
My code is as the following:
public RepeatStatus execute(StepContribution stepContribution, ChunkContext chunkContext) throws Exception {
stepContribution.setExitStatus(new ExitStatus("SKIPPED", "例パラメータ does not exist."));
}
But it displayed Exit Message as the following:
How can I do to display the japanese characters in Spring Batch Admin UI??
I used SpringBatchAdmin 1.0.0.
When I set the UTF-8 encoding in freemarker template, Japanese character can be shown in Spring Batch Admin UI.
There are 2 ways to solve the problem:
Using SpringBatchAdmin 1.2.2 and above because I found that the problem had already fixed on SpringBatchAdmin 1.2.2.
Overriding the resources-context.xml under /META-INF/spring/batch/servlet/resources/ of spring-batch-admin-resources-1.0.0.RELEASE.jar
Since the first way is not ok for me, I use the second way.
I create an xml file under /META-INF/spring/batch/servlet/override/ to override the resources-context.xml.
I add the contentType property in standard bean.
Here is the snippet of newly created xml file:
<bean id="standard" parent="parentLayout">
<property name="url" value="/layouts/standard.ftl" />
<property name="contentType" value="text/html;charset=UTF-8" />
<property name="attributes">
<props merge="true">
<prop key="body">/layouts/home.ftl</prop>
<!-- Matches the prefix of the servlet mapping in web.xml -->
<prop key="servletPath">#{resourceService.servletPath}</prop>
</props>
</property>
</bean>
Hope this will help the others also. :)
I have this working now now, but am lost as to why this problem occurred..
I followed the following
http://pfelitti87.blogspot.co.uk/2012/07/rest-services-with-spring-3-xml-json.html
but i changed the controller method and added #ResponseBody...
#ResponseBody
#ResponseStatus(HttpStatus.OK)
#RequestMapping(value="/names", method=RequestMethod.GET)
public List<Book> getNames() {
return returnData();
}
By adding this i noticed that the output would appear as json, regardless of the extension i specified?...
Any ideas why #RepsonseBody would cause this issue?
The post only works for resolving different views based on different types. It does not work on your case.
If you are using Spring 3.2.x, the configuration below would solve your problem.
<mvc:annotation-driven content-negotiation-manager="contentNegotiationManager"/>
<bean id="contentNegotiationManager" class="org.springframework.web.accept.ContentNegotiationManagerFactoryBean">
<property name="favorPathExtension" value="true"/>
<property name="mediaTypes">
<value>
json=application/json
xml=application/xml
</value>
</property>
<property name="defaultContentType" value="application/json"/>
</bean>
However, if you are using 3.1.x, there are approaches like http://tedyoung.me/2011/07/28/spring-mvc-responsebody and http://springinpractice.com/2012/02/22/supporting-xml-and-json-web-service-endpoints-in-spring-3-1-using-responsebody that might help you.
I had webapps using spring jdbc template and now , want to improve performance of my application , so i want to cache result of some DATABASE QUERY in my Tomcat server.How i can achieve dis concept.
Thanks
Spring 3.1 has introduced a Caching Abstraction. You should be able to make use of this to cache the results of your DAO method calls.
The documentation is here and it was included in the Spring blog here.
I have it working, but Alex is correct, there are a few different ways to configure this, depending on what you want as your Caching backend.
For cache configuration, I've selected ehcache, as it's simple to configure, but has powerful features for configuring ttl/etc.
Configuration:
<beans xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns="http://www.springframework.org/schema/beans"
xmlns:p="http://www.springframework.org/schema/p"
xmlns:cache="http://www.springframework.org/schema/cache"
xsi:schemaLocation="
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.1.xsd
http://www.springframework.org/schema/cache http://www.springframework.org/schema/cache/spring-cache.xsd">
<cache:annotation-driven />
<bean id="cacheManager" class="org.springframework.cache.ehcache.EhCacheCacheManager">
<property name="cacheManager"><ref local="ehcache"/></property>
</bean>
<bean id="ehcache" class="org.springframework.cache.ehcache.EhCacheManagerFactoryBean"
p:configLocation="classpath:ehcache.xml"/>
</beans>
ehcache.xml:
<?xml version="1.0" encoding="UTF-8"?>
<ehcache xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:noNamespaceSchemaLocation="http://ehcache.org/ehcache.xsd"
updateCheck="false" monitoring="autodetect"
dynamicConfig="true">
<!-- http://ehcache.org/documentation/configuration.html -->
<!-- Also See org.springframework.cache.ehcache.EhCacheFactoryBean -->
<diskStore path="java.io.tmpdir"/>
<cache name="resourceBundle"
overflowToDisk="false"
eternal="false"
maxElementsInMemory="500"
timeToIdleSeconds="86400"
timeToLiveSeconds="86400"/>
</ehcache>
I had issues with running the ehcache 2.5 in my junit environment, due to caches with duplicate names running concurrently was not allowed, and they didn't seem to shut down right away, but here is my pom entry:
<dependency>
<groupId>net.sf.ehcache</groupId>
<artifactId>ehcache-core</artifactId>
<version>2.4.7</version>
</dependency>
Finally, on your repository, do the following:
#Repository
public class someStore {
#PersistenceContext
EntityManager em;
//The value here needs to match the name of the cache configured in your ehcache xml. You can also use Spel expressions in your key
#Cachable(value = "resourceBundle", key = "#basename+':'+#locale.toString()")
public ResourceBundle getResourceBundle(final String basename, final Locale locale){
...
}
#CacheEvict(value = "resourceBundle", key = "#basename+':'+#locale.toString()")
public void revertResourceBundle(final String basename, final Locale locale){
...
}
}