-->
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: Validation only on update and on create
PostPosted: Mon Jan 07, 2013 5:50 am 
Newbie

Joined: Wed Dec 19, 2012 2:52 am
Posts: 3
Hallo,

I have already searched for my problem but i didn't get an answer. I hope you can help me with my problem:

I use the Hibernate Validator Version 4.3.0 and have included it like this in the pom.xml :
Code:
<dependency>
   <groupId>org.hibernate</groupId>
   <artifactId>hibernate-validator</artifactId>
   <version>4.3.0.Final</version>
</dependency>


Then we added the annotations to the classes and it is also validated everything wonderful.

However, we wish that it validates only when writing data to the database. So for an update or an insert.

I hope you can help me with my problem.
Sincerely,
Evil
He currently validated always and unfortunately I do not know how to tell Hibernate that it should be validated only if it writes data to the database.


Top
 Profile  
 
 Post subject: Re: Validation only on update and on create
PostPosted: Mon Jan 07, 2013 4:29 pm 
Hibernate Team
Hibernate Team

Joined: Sat Jan 24, 2009 12:46 pm
Posts: 388
Some more context of your set-up would be helpful. What exactly do you mean by "validates always"? Do you invoke validation yourself (by using one of the methods on javax.validation.Validator) or do you get a ConstraintViolationException thrown by Hibernate?

_________________
Visit my blog at http://musingsofaprogrammingaddict.blogspot.com/


Top
 Profile  
 
 Post subject: Re: Validation only on update and on create
PostPosted: Wed Jan 09, 2013 3:19 am 
Newbie

Joined: Wed Dec 19, 2012 2:52 am
Posts: 3
Hi,

follow szenario:
I want to update the values of a vehicle. Soi get first the instance out of the database, add the changes and than i want to validate the values. (code is below)
Code:
// Get vehicle
VehicleEntity entity = vehicleDAO.getVehicle(id);
// Copy values
copy(vehicle, entity);
// Validate values
EntityValidator<VehicleEntity> e = new EntityValidator<VehicleEntity>();
e.validate(entity);


I also added the annotations to my VehicleEntity
Code:

@Entity
@Table(name = "tbl_vehicle")
public class VehicleEntity {

   @Id
   @GeneratedValue(strategy=GenerationType.IDENTITY)
   @Column(name = "vehicle_id")
   private long id;

   @Length(min=0,max=250,message="The length of the vehicle name may not exceed 250.")
   @Column(name = "vehicle_name")
   private String name;

   @Range(min=0,message="The range has to be positive")
   @Column(name = "complete_charge_range")
   private BigDecimal fullRange;

   @OneToMany(cascade = { CascadeType.ALL }, orphanRemoval = true, fetch = FetchType.EAGER)
   @JoinColumn(name = "vehicle_id", nullable = false)
   private Set<VehicleChargingDetailEntity> chargingDetails = new HashSet<VehicleChargingDetailEntity>();

   @Range(min=0,message="The range has to be positive")
   @Column(name = "eighty_charge", nullable = true)
   private BigDecimal range80;

   @ManyToOne
   @JoinColumn(name = "user_id")
   private UserEntity owner;

   @Column(name = "status")
   private Character status;
...

For example:
If i try to save a negative range for a vehicle i should get a Exception with this text:
The range has to be positive

And now the problem. If i validate, the Exception is thrown but finally i get a RuntimeException and don't know how to configure the hibernate validator, that i get only my ConstraintViolationException.

Below is my stack trace.
Code:
Jan 09, 2013 8:06:50 AM com.sun.jersey.spi.container.ContainerResponse mapMappableContainerException
SEVERE: The RuntimeException could not be mapped to a response, re-throwing to the HTTP container
org.springframework.transaction.TransactionSystemException: Could not commit JPA transaction; nested exception is javax.persistence.RollbackException: Error while committing the transaction
   at org.springframework.orm.jpa.JpaTransactionManager.doCommit(JpaTransactionManager.java:476)
   at org.springframework.transaction.support.AbstractPlatformTransactionManager.processCommit(AbstractPlatformTransactionManager.java:754)
   at org.springframework.transaction.support.AbstractPlatformTransactionManager.commit(AbstractPlatformTransactionManager.java:723)
   at org.springframework.transaction.interceptor.TransactionAspectSupport.commitTransactionAfterReturning(TransactionAspectSupport.java:393)
   at org.springframework.transaction.interceptor.TransactionInterceptor.invoke(TransactionInterceptor.java:120)
   at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:172)
   at org.springframework.aop.framework.Cglib2AopProxy$DynamicAdvisedInterceptor.intercept(Cglib2AopProxy.java:622)
   at net.erouting.admin.VehicleManager$$EnhancerByCGLIB$$fe29a661.putVehicle(<generated>)
   at net.erouting.api.Vehicle.putVehicle(Vehicle.java:136)
   at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
   at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
   at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
   at java.lang.reflect.Method.invoke(Method.java:601)
   at com.sun.jersey.spi.container.JavaMethodInvokerFactory$1.invoke(JavaMethodInvokerFactory.java:60)
   at com.sun.jersey.server.impl.model.method.dispatch.AbstractResourceMethodDispatchProvider$ResponseOutInvoker._dispatch(AbstractResourceMethodDispatchProvider.java:205)
   at com.sun.jersey.server.impl.model.method.dispatch.ResourceJavaMethodDispatcher.dispatch(ResourceJavaMethodDispatcher.java:75)
   at com.sun.jersey.server.impl.uri.rules.HttpMethodRule.accept(HttpMethodRule.java:288)
   at com.sun.jersey.server.impl.uri.rules.RightHandPathRule.accept(RightHandPathRule.java:147)
   at com.sun.jersey.server.impl.uri.rules.ResourceClassRule.accept(ResourceClassRule.java:108)
   at com.sun.jersey.server.impl.uri.rules.RightHandPathRule.accept(RightHandPathRule.java:147)
   at com.sun.jersey.server.impl.uri.rules.RootResourceClassesRule.accept(RootResourceClassesRule.java:84)
   at com.sun.jersey.server.impl.application.WebApplicationImpl._handleRequest(WebApplicationImpl.java:1483)
   at com.sun.jersey.server.impl.application.WebApplicationImpl._handleRequest(WebApplicationImpl.java:1414)
   at com.sun.jersey.server.impl.application.WebApplicationImpl.handleRequest(WebApplicationImpl.java:1363)
   at com.sun.jersey.server.impl.application.WebApplicationImpl.handleRequest(WebApplicationImpl.java:1353)
   at com.sun.jersey.spi.container.servlet.WebComponent.service(WebComponent.java:414)
   at com.sun.jersey.spi.container.servlet.ServletContainer.service(ServletContainer.java:537)
   at com.sun.jersey.spi.container.servlet.ServletContainer.service(ServletContainer.java:708)
   at javax.servlet.http.HttpServlet.service(HttpServlet.java:722)
   at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:305)
   at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:210)
   at org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:311)
   at org.springframework.security.web.access.intercept.FilterSecurityInterceptor.invoke(FilterSecurityInterceptor.java:116)
   at org.springframework.security.web.access.intercept.FilterSecurityInterceptor.doFilter(FilterSecurityInterceptor.java:83)
   at org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:323)
   at org.springframework.security.web.access.ExceptionTranslationFilter.doFilter(ExceptionTranslationFilter.java:113)
   at org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:323)
   at org.springframework.security.web.authentication.AnonymousAuthenticationFilter.doFilter(AnonymousAuthenticationFilter.java:113)
   at org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:323)
   at org.springframework.security.web.servletapi.SecurityContextHolderAwareRequestFilter.doFilter(SecurityContextHolderAwareRequestFilter.java:54)
   at org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:323)
   at org.springframework.security.web.authentication.www.DigestAuthenticationFilter.doFilter(DigestAuthenticationFilter.java:209)
   at org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:323)
   at org.springframework.security.web.context.SecurityContextPersistenceFilter.doFilter(SecurityContextPersistenceFilter.java:87)
   at org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:323)
   at org.springframework.security.web.FilterChainProxy.doFilter(FilterChainProxy.java:173)
   at org.springframework.web.filter.DelegatingFilterProxy.invokeDelegate(DelegatingFilterProxy.java:346)
   at org.springframework.web.filter.DelegatingFilterProxy.doFilter(DelegatingFilterProxy.java:259)
   at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:243)
   at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:210)
   at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:222)
   at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:123)
   at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:472)
   at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:168)
   at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:99)
   at org.apache.catalina.valves.AccessLogValve.invoke(AccessLogValve.java:929)
   at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:118)
   at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:407)
   at org.apache.coyote.http11.AbstractHttp11Processor.process(AbstractHttp11Processor.java:1002)
   at org.apache.coyote.AbstractProtocol$AbstractConnectionHandler.process(AbstractProtocol.java:585)
   at org.apache.tomcat.util.net.JIoEndpoint$SocketProcessor.run(JIoEndpoint.java:310)
   at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1110)
   at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:603)
   at java.lang.Thread.run(Thread.java:722)
Caused by: javax.persistence.RollbackException: Error while committing the transaction
   at org.hibernate.ejb.TransactionImpl.commit(TransactionImpl.java:92)
   at org.springframework.orm.jpa.JpaTransactionManager.doCommit(JpaTransactionManager.java:467)
   ... 63 more
Caused by: javax.validation.ConstraintViolationException: Validation failed for classes [net.erouting.db.VehicleEntity] during update time for groups [javax.validation.groups.Default, ]
List of constraint violations:[
   ConstraintViolationImpl{interpolatedMessage='The range has to be positive', propertyPath=fullRange, rootBeanClass=class net.erouting.db.VehicleEntity, messageTemplate='The range has to be positive'}
]
   at org.hibernate.cfg.beanvalidation.BeanValidationEventListener.validate(BeanValidationEventListener.java:159)
   at org.hibernate.cfg.beanvalidation.BeanValidationEventListener.onPreUpdate(BeanValidationEventListener.java:102)
   at org.hibernate.action.internal.EntityUpdateAction.preUpdate(EntityUpdateAction.java:235)
   at org.hibernate.action.internal.EntityUpdateAction.execute(EntityUpdateAction.java:115)
   at org.hibernate.engine.spi.ActionQueue.execute(ActionQueue.java:362)
   at org.hibernate.engine.spi.ActionQueue.executeActions(ActionQueue.java:354)
   at org.hibernate.engine.spi.ActionQueue.executeActions(ActionQueue.java:276)
   at org.hibernate.event.internal.AbstractFlushingEventListener.performExecutions(AbstractFlushingEventListener.java:326)
   at org.hibernate.event.internal.DefaultFlushEventListener.onFlush(DefaultFlushEventListener.java:52)
   at org.hibernate.internal.SessionImpl.flush(SessionImpl.java:1214)
   at org.hibernate.internal.SessionImpl.managedFlush(SessionImpl.java:403)
   at org.hibernate.engine.transaction.internal.jdbc.JdbcTransaction.beforeTransactionCommit(JdbcTransaction.java:101)
   at org.hibernate.engine.transaction.spi.AbstractTransactionImpl.commit(AbstractTransactionImpl.java:175)
   at org.hibernate.ejb.TransactionImpl.commit(TransactionImpl.java:75)
   ... 64 more

Jan 09, 2013 8:06:50 AM org.apache.catalina.core.StandardWrapperValve invoke
SEVERE: Servlet.service() for servlet [erouting] in context with path [/api] threw exception
org.springframework.transaction.TransactionSystemException: Could not commit JPA transaction; nested exception is javax.persistence.RollbackException: Error while committing the transaction
   at org.springframework.orm.jpa.JpaTransactionManager.doCommit(JpaTransactionManager.java:476)
   at org.springframework.transaction.support.AbstractPlatformTransactionManager.processCommit(AbstractPlatformTransactionManager.java:754)
   at org.springframework.transaction.support.AbstractPlatformTransactionManager.commit(AbstractPlatformTransactionManager.java:723)
   at org.springframework.transaction.interceptor.TransactionAspectSupport.commitTransactionAfterReturning(TransactionAspectSupport.java:393)
   at org.springframework.transaction.interceptor.TransactionInterceptor.invoke(TransactionInterceptor.java:120)
   at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:172)
   at org.springframework.aop.framework.Cglib2AopProxy$DynamicAdvisedInterceptor.intercept(Cglib2AopProxy.java:622)
   at net.erouting.admin.VehicleManager$$EnhancerByCGLIB$$fe29a661.putVehicle(<generated>)
   at net.erouting.api.Vehicle.putVehicle(Vehicle.java:136)
   at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
   at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
   at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
   at java.lang.reflect.Method.invoke(Method.java:601)
   at com.sun.jersey.spi.container.JavaMethodInvokerFactory$1.invoke(JavaMethodInvokerFactory.java:60)
   at com.sun.jersey.server.impl.model.method.dispatch.AbstractResourceMethodDispatchProvider$ResponseOutInvoker._dispatch(AbstractResourceMethodDispatchProvider.java:205)
   at com.sun.jersey.server.impl.model.method.dispatch.ResourceJavaMethodDispatcher.dispatch(ResourceJavaMethodDispatcher.java:75)
   at com.sun.jersey.server.impl.uri.rules.HttpMethodRule.accept(HttpMethodRule.java:288)
   at com.sun.jersey.server.impl.uri.rules.RightHandPathRule.accept(RightHandPathRule.java:147)
   at com.sun.jersey.server.impl.uri.rules.ResourceClassRule.accept(ResourceClassRule.java:108)
   at com.sun.jersey.server.impl.uri.rules.RightHandPathRule.accept(RightHandPathRule.java:147)
   at com.sun.jersey.server.impl.uri.rules.RootResourceClassesRule.accept(RootResourceClassesRule.java:84)
   at com.sun.jersey.server.impl.application.WebApplicationImpl._handleRequest(WebApplicationImpl.java:1483)
   at com.sun.jersey.server.impl.application.WebApplicationImpl._handleRequest(WebApplicationImpl.java:1414)
   at com.sun.jersey.server.impl.application.WebApplicationImpl.handleRequest(WebApplicationImpl.java:1363)
   at com.sun.jersey.server.impl.application.WebApplicationImpl.handleRequest(WebApplicationImpl.java:1353)
   at com.sun.jersey.spi.container.servlet.WebComponent.service(WebComponent.java:414)
   at com.sun.jersey.spi.container.servlet.ServletContainer.service(ServletContainer.java:537)
   at com.sun.jersey.spi.container.servlet.ServletContainer.service(ServletContainer.java:708)
   at javax.servlet.http.HttpServlet.service(HttpServlet.java:722)
   at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:305)
   at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:210)
   at org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:311)
   at org.springframework.security.web.access.intercept.FilterSecurityInterceptor.invoke(FilterSecurityInterceptor.java:116)
   at org.springframework.security.web.access.intercept.FilterSecurityInterceptor.doFilter(FilterSecurityInterceptor.java:83)
   at org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:323)
   at org.springframework.security.web.access.ExceptionTranslationFilter.doFilter(ExceptionTranslationFilter.java:113)
   at org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:323)
   at org.springframework.security.web.authentication.AnonymousAuthenticationFilter.doFilter(AnonymousAuthenticationFilter.java:113)
   at org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:323)
   at org.springframework.security.web.servletapi.SecurityContextHolderAwareRequestFilter.doFilter(SecurityContextHolderAwareRequestFilter.java:54)
   at org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:323)
   at org.springframework.security.web.authentication.www.DigestAuthenticationFilter.doFilter(DigestAuthenticationFilter.java:209)
   at org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:323)
   at org.springframework.security.web.context.SecurityContextPersistenceFilter.doFilter(SecurityContextPersistenceFilter.java:87)
   at org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:323)
   at org.springframework.security.web.FilterChainProxy.doFilter(FilterChainProxy.java:173)
   at org.springframework.web.filter.DelegatingFilterProxy.invokeDelegate(DelegatingFilterProxy.java:346)
   at org.springframework.web.filter.DelegatingFilterProxy.doFilter(DelegatingFilterProxy.java:259)
   at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:243)
   at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:210)
   at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:222)
   at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:123)
   at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:472)
   at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:168)
   at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:99)
   at org.apache.catalina.valves.AccessLogValve.invoke(AccessLogValve.java:929)
   at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:118)
   at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:407)
   at org.apache.coyote.http11.AbstractHttp11Processor.process(AbstractHttp11Processor.java:1002)
   at org.apache.coyote.AbstractProtocol$AbstractConnectionHandler.process(AbstractProtocol.java:585)
   at org.apache.tomcat.util.net.JIoEndpoint$SocketProcessor.run(JIoEndpoint.java:310)
   at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1110)
   at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:603)
   at java.lang.Thread.run(Thread.java:722)
Caused by: javax.persistence.RollbackException: Error while committing the transaction
   at org.hibernate.ejb.TransactionImpl.commit(TransactionImpl.java:92)
   at org.springframework.orm.jpa.JpaTransactionManager.doCommit(JpaTransactionManager.java:467)
   ... 63 more
Caused by: javax.validation.ConstraintViolationException: Validation failed for classes [net.erouting.db.VehicleEntity] during update time for groups [javax.validation.groups.Default, ]
List of constraint violations:[
   ConstraintViolationImpl{interpolatedMessage='The range has to be positive', propertyPath=fullRange, rootBeanClass=class net.erouting.db.VehicleEntity, messageTemplate='The range has to be positive'}
]
   at org.hibernate.cfg.beanvalidation.BeanValidationEventListener.validate(BeanValidationEventListener.java:159)
   at org.hibernate.cfg.beanvalidation.BeanValidationEventListener.onPreUpdate(BeanValidationEventListener.java:102)
   at org.hibernate.action.internal.EntityUpdateAction.preUpdate(EntityUpdateAction.java:235)
   at org.hibernate.action.internal.EntityUpdateAction.execute(EntityUpdateAction.java:115)
   at org.hibernate.engine.spi.ActionQueue.execute(ActionQueue.java:362)
   at org.hibernate.engine.spi.ActionQueue.executeActions(ActionQueue.java:354)
   at org.hibernate.engine.spi.ActionQueue.executeActions(ActionQueue.java:276)
   at org.hibernate.event.internal.AbstractFlushingEventListener.performExecutions(AbstractFlushingEventListener.java:326)
   at org.hibernate.event.internal.DefaultFlushEventListener.onFlush(DefaultFlushEventListener.java:52)
   at org.hibernate.internal.SessionImpl.flush(SessionImpl.java:1214)
   at org.hibernate.internal.SessionImpl.managedFlush(SessionImpl.java:403)
   at org.hibernate.engine.transaction.internal.jdbc.JdbcTransaction.beforeTransactionCommit(JdbcTransaction.java:101)
   at org.hibernate.engine.transaction.spi.AbstractTransactionImpl.commit(AbstractTransactionImpl.java:175)
   at org.hibernate.ejb.TransactionImpl.commit(TransactionImpl.java:75)
   ... 64 more



Sincerely,
Evil


Top
 Profile  
 
 Post subject: Re: Validation only on update and on create
PostPosted: Mon Jan 14, 2013 5:11 am 
Hibernate Team
Hibernate Team

Joined: Sat Jan 24, 2009 12:46 pm
Posts: 388
Based on your stacktrace, validation is triggered automatically by Hibernate upon committing the current transaction. You can consider this as a measure to avoid writing invalid data into the database.

What do you actually want to achieve? Do you want to display an error message in the user interface when a user entered invalid data? In this case you should validate the entered data using the javax.validation.Validator interface yourself, which returns a Set<ConstraintViolation<YourType>> from which you can retrieve all required information.

In your example, where does EntityValidator come from? Using Bean Validation 1.x and Hibernate Validator 4/5, you'd use the aforementioned Validator interface to validate objects:

Code:
Validator validator = Validation.buildDefaultValidatorFactory().getValidator();
Set<ConstraintViolation<YourType>> violations = validator.validate(yourObject);


Hth,

--Gunnar

_________________
Visit my blog at http://musingsofaprogrammingaddict.blogspot.com/


Top
 Profile  
 
 Post subject: Re: Validation only on update and on create
PostPosted: Mon Jan 14, 2013 4:55 pm 
Newbie

Joined: Mon Jan 14, 2013 4:07 pm
Posts: 17
It sounds like you are expecting the naked ConstraintViolationException to be thrown when inserting or updating. That just wont be the case. In face, I have found that depending on even the driver where the exception is will be different. For example, with HSQL it will be thrown naked from a preInsert handler while in PSQL it would be thrown during a commit, similar to the stack trace you posted.

My suggestion is for you to add an exception handling function to pull out the ConstrainViolationException. Something like this:

Code:
    public static ConstraintViolationException getConstraintViolationOrRethrow( RuntimeException e ) {
        Throwable curr = e;

        // if there is a CVE in the path return it otherwise rethrow
        while ( curr != null ) {
            if ( curr instanceof ConstraintViolationException )
                return (ConstrainViolationException)curr;
            else
                curr = curr.getCause();
        }

        throw e;
    }

Then you can handle it like so:

Code:
   catch ( Exception e ) {
      handleContrainViolations( getConstraintViolationsOrReThrow( e ) );
   }

I hope this helps.


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.