How to integrate Spring and Hibernate

Here are the few tips I have gathered on how to integrate spring and hibernate.

Create your bean.xml something like this…

 












Connection.properties




${db.driverClassName}


${db.url}


${db.username}


${db.password}








com.usermanagement.pojo.User
com.usermanagement.pojo.Role
com.usermanagement.pojo.Privilege
com.usermanagement.pojo.Password
com.usermanagement.pojo.PasswordPolicy





org.hibernate.dialect.Oracle10gDialect
update
true

true
<!--org.hibernate.cache.SingletonEhCacheProvider-->
org.hibernate.cache.EhCacheProvider
































The list of jars that you might need are

  1. antlr-2.7.6.jar
  2. asm-1.5.3.jar
  3. axis2-spring-1.6.1.jar
  4. cglib-2.1_3.jar
  5. commons-collections-3.1.jar
  6. commons-dbcp-1.4.jar
  7. commons-logging-1.0.4.jar
  8. commons-pool-1.6.jar
  9. dom4j-1.6.1.jar
  10. hibernate-3.2.0.ga.jar
  11. hibernate-3.2.6.jar
  12. hibernate-annotations-3.4.0.GA.jar
  13. hibernate-commons-annotations-3.1.0.GA.jar
  14. hibernate-core-3.3.0.SP1.jar
  15. hibernate-entitymanager.jar
  16. hibernate-jpa-2.0-api-1.0.0.Final.jar
  17. jta-1.1.jar
  18. log4j-1.2.14.jar
  19. ojdbc14.jar
  20. org.springframework.asm-3.0.0.RELEASE.jar
  21. org.springframework.beans-3.0.0.RELEASE.jar
  22. org.springframework.context-3.0.0.RELEASE.jar
  23. org.springframework.core-3.0.0.RELEASE.jar
  24. org.springframework.expression-3.0.0.RELEASE.jar
  25. org.springframework.jdbc-3.0.0.RELEASE.jar
  26. org.springframework.orm-3.0.0.RELEASE.jar
  27. org.springframework.web.servlet-3.0.1.RELEASE-A.jar
  28. slf4j-api-1.6.1.jar
  29. spring-tx-3.0.0.RELEASE.jar

SOAP Version Mismatch: SOAP Version “SOAP 1.2 Protocol” in request does not match the SOAP version “SOAP 1.1 Protocol” of the Web service.

We were getting the below error in our project, when trying to invoke a web service.

[Server:server-one] 07:29:12,927 ERROR [stderr] (http-/10.99.12.28:8080-13) org.springframework.ws.soap.client.SoapFaultClientException: [ISS.0088.9168] SOAP Version Mismatch: SOAP Version "SOAP 1.2 Protocol" in request does not match the SOAP version "SOAP 1.1 Protocol" of the Web service.
[Server:server-one] 07:29:12,928 ERROR [stderr] (http-/10.99.12.28:8080-13) at org.springframework.ws.soap.client.core.SoapFaultMessageResolver.resolveFault(SoapFaultMessageResolver.java:37)
[Server:server-one] 07:29:12,929 ERROR [stderr] (http-/10.99.12.28:8080-13) at org.springframework.ws.client.core.WebServiceTemplate.handleFault(WebServiceTemplate.java:774)
[Server:server-one] 07:29:12,929 ERROR [stderr] (http-/10.99.12.28:8080-13) at org.springframework.ws.client.core.WebServiceTemplate.doSendAndReceive(WebServiceTemplate.java:600)
[Server:server-one] 07:29:12,929 ERROR [stderr] (http-/10.99.12.28:8080-13) at org.springframework.ws.client.core.WebServiceTemplate.sendAndReceive(WebServiceTemplate.java:537)
[Server:server-one] 07:29:12,930 ERROR [stderr] (http-/10.99.12.28:8080-13) at org.springframework.ws.client.core.WebServiceTemplate.marshalSendAndReceive(WebServiceTemplate.java:384)
[Server:server-one] 07:29:12,930 ERROR [stderr] (http-/10.99.12.28:8080-13) at org.springframework.ws.client.core.WebServiceTemplate.marshalSendAndReceive(WebServiceTemplate.java:378)
[Server:server-one] 07:29:12,931 ERROR [stderr] (http-/10.99.12.28:8080-13) at org.springframework.ws.client.core.WebServiceTemplate.marshalSendAndReceive(WebServiceTemplate.java:370)

Solution:
The solution lies in the configuration files used by spring.
Here in our project, the version specified in this file was 1.2 where as the web service was expecting 1.1






How to make ajax call in spring framework

Write a server side method, with a mapping something as follows:

@RequestMapping("/getKids.html")   
public @ResponseBody
String getChildren(@RequestParam(value = "ocn") String ocn,
HttpServletRequest request, HttpServletResponse response)
{
return ocn.toUpperCase();
}

Here @ResponseBody tells the framework to return value of the method to the browser and not lookup for a view with that name.
You can optionally omit request and response parameters in the signature of the method, if you like to.

After removal of request and response the method will look like

@RequestMapping("/getKids.html")   
public @ResponseBody
String getChildren(@RequestParam(value = "ocn") String ocn)
{
return ocn.toUpperCase();
}

Write a javascript method as follows (This uses jquery to fire ajax request)

function populateSubAgents(obj)
{
$.ajax({
url: "getKids.html?ocn="+obj.value,
success: function(data) {
$("#subAgentName").html(data);
},
error: function(XMLHttpRequest, textStatus, errorThrown) {
if (XMLHttpRequest.status == 0) {
alert(' Check Your Network.');
} else if (XMLHttpRequest.status == 404) {
alert('Requested URL not found.');
} else if (XMLHttpRequest.status == 500) {
alert('Internel Server Error.');
} else {
alert('Unknown Error.n' + XMLHttpRequest.responseText);
}
}
});
}

What is the difference between @RequestMapping’s param attribute AND @RequestParam

Source : http://www.captaindebug.com/2011/07/accessing-request-parameters-using.html#uds-search-results

@RequestMapping is only used to map request URLs to your controller.
The ‘params’ value is used to narrow the mapping down allowing you to map different param values
(uuids in this case) to different methods. For example:

/** This is only called when uuid=6 e.g.: /help/detail?uuid=6 */ 
@RequestMapping(value = "/help/detail", params={"uuid=6"}
method = RequestMethod.GET)
public String displaySomeHelpDetail(Model model) {

// Do Something
return "view.name"
}

/** This is only called when uuid=1234 e.g.: /help/detail?uuid=1234 */ 
@RequestMapping(value = "/help/detail", params={"uuid=1234"}
method = RequestMethod.GET)
public String displaySomeHelpDetail(Model model) {

// Do Something Else
return "another.view.name"
}

Whilst @RequestParam is used to pass request parameters into a method so that you can use them.
For example:

@RequestMapping(value = "/help/detail", 
method = RequestMethod.GET)
public String displaySomeHelpDetail(@RequestParam("uuid") String uuid, Model model) {

log.info("The uuid = " + uuid);
return "view.name"
}