Spring JPA Exception Translation - java

I have configured my application context as stated in the spring documentation to enable Exception Translation from jpa exceptions to spring DataAccessException. Should I also provide the implementation of PersistenceExceptionTranslator? If so, can anyone give me an example of how this is done?

I do it only by putting the #Repository annotation on my DAO or Manager class that uses the EntityManager.
Make sure that you enabled component scanning:
<context:component-scan base-package="org.example"/>

You can enable exception translation as well as repository scan by using following xml configuration
<jpa:repositories base-package="com.nagarro.ncpp.backend.repository" />

Related

spring integration and component scan

I'm a newbie with Spring MVC but now i've been moved to a new project which uses Spring Integration to turn in channel some service. Example in the context.xml
<int:channel id="example-channel" />
<int:service-activator input-channel="example-channel" ref="exampleServiceFacade" />
For every servicefacade i have to bind the service to a channel.
I was wandering, what if I could map the classes to be turned into channels as i could map the beans with component-scan?
<context:component-scan base-package="com.package" />
so i ended up with this tutorial which speaks about some annotation:
#IntegrationComponentScan
But i cannot understand how it's related to the xml tag service activator and channel.. So i'm quite confused. Does anyone with more experience have an idea if what i'm trying to do can be done?
I just want to scan con the classes which defines channels in integration without having to declare every class.
Thanks.
Your question is a bit unclear. Any Spring Integration custom XML tag is parsed by infrastructure and registered as beans in the application context. Like you'd do that via raw <bean>.
#ComponentScan, #Configuration, #Bean and so on are marker annotations to say application context which classes treat as beans.
So, using both techniques for application context configuration you don't lose anything and can continue to mark you class with #Service and use its bean name from <service-activator ref="">.
From other side now you can fully build Spring Integration without any XML! Please, read the mentioned doc in its entirety.

Convert Hibernate exceptions in Spring Roo to Spring Data Access exceptions

I am currently working on a Spring Roo project and I want the exceptions thrown by Hibernate to be converted to Spring Exceptions. E.g. I want the Hibernate exception ConstraintViolationException to be converted to Spring's DataIntegrityViolationException.
I have done this in Spring projects previously by adding in the following code to my Spring config:
<!-- Translates Hibernate exceptions to Spring Data Access Exceptions -->
<bean class="org.springframework.dao.annotation.PersistenceExceptionTranslationPostProcessor"/>
However in Roo this does not work. Can anybody tell me how I can get this working and why it doesn't work in Roo?
You must setup the persistence layer as JPA Repository in order Roo configures your project to use Spring Data.
http://docs.spring.io/spring-roo/reference/html/base-layers.html#d4e1962

Spring MVC 3.1.0 bug? After upgrade controllers are no more autodetected

I'm writing web application that uses Spring MVC to bind Spring beans with REST-like channels.
I've created the configuration basic both on my previous apps (pure XML configuration) and example, which used <mvc:annotation-driven/> feature. I'm pointing a package with controllers with <context:component-scan base-package="my.package"/> in spring xml file.
It is working - in Spring 3.0.6.RELEASE. However, after upgrading to 3.1.0.RELEASE my controllers stopped to be detected and no channel was registered. Spring context contains no implementation of HelloChannel interface.
Is this a bug in this Spring version, or I'm using deprecated configuration, which stopped to be supported in newer version? I got no error or warning, simply no bean is auto-detected.
The controller interface definition looks like that:
#RequestMapping("/config") public interface ConfigChannel
And the implementation:
#Controller
public class ConfigChannelImpl implements ConfigChannel
The Spring documentation indicates that interface-based #Controllers are for proxying transactional methods. As such, you are probably using the <tx:annotation-driven /> tag. The problem you now seem to have is that Spring 3.1 introduced support for CGLIB, a runtime-based bytecode manipulator. You need to add proxy-target-class="true" to your transaction configuration and add CGLIB to your classpath.
<tx:annotation-driven proxy-target-class="true" />
From http://static.springsource.org/spring/docs/3.1.0.M1/spring-framework-reference/html/mvc.html#mvc-ann-requestmapping

Query about Spring Transaction configuration and propagation

IN spring docs it's written
The most important concepts to grasp with regard to the Spring Framework's declarative transaction
support are that this support is enabled via AOP proxies, and that the transactional advice is driven by
metadata (currently XML- or annotation-based).
So If i use
<tx:annotation-driven proxy-target-class="true" order="100"/>
in config file and not use #Transactional annotation on my beans. Would transaction still be supported, as I am using AOP and transaction Interceptor should be built-in to my AOPs thus no use of explicitly using #Transactional annotation.
Thanks,
No, you'd still have to use the #Transactional annotation. AOP proxy is only used to inject the transaction related code into your code.
< tx:annotation-driven /> is used to auto detect the '#Transactional' annotation .So it's necessary to have one . Reference here.
The proxy-target-class="true" decides whether Spring should use JDK dynamic proxies or CGLIB class based proxies . See the reference for more info . Basically if your class implements atleast one interface, JDK dynamic proxies are used . If you have a class MyDaoImpl extends MyDao and in your service , you inject the dao reference via MyDaoImpl myDaoImpl , JDK dynamic proxies will not work if the annotations are on your interface as class bases proxies are created with proxy-target-class="true" and #Transactional annotation is not inherited .
The reason why your queries seem to work without #Transactional may be because you are using hibernate template which opens and closes the transactions internally . From Spring 3 , it is recommended to inject sessionFactory directly and not to use hibernate template .
Hope this helps.

Spring framework annotation question

In case of supporting both XML and annotation, when spring framework do the annotation scan and how to process it?
Could someone give detail scenario?
For XmlWebApplicationContext, it use loadBeanDefinations() via Xml file. But when it do the annotation scan and use which class to process it?
Could explain the detail for ?
Thanks.
Franklin
In an XML based Application Context, annotations are only registered if you explicitly configure that:
<!-- register default annotations (e.g. #Required) -->
<context:annotation-config />
<!-- scan for components in selected package -->
<context:component-scan base-package="org.example"/>
Reference:
Annotation-based container
configuration
Classpath scanning and managed
components
And if you want to check the inner workings, see:
CommonAnnotationBeanPostProcessor
JavaDoc, Source Code
ComponentScanBeanDefinitionParser
JavaDoc, Source Code
I guess u could dig in the sources starting with AnnotationConfigWebApplicationContext.
http://static.springsource.org/spring/docs/3.0.x/javadoc-api/org/springframework/web/context/support/AnnotationConfigWebApplicationContext.html

Categories