tag:blogger.com,1999:blog-1306958590608174053.post7921484800218695599..comments2024-03-21T08:28:12.966+01:00Comments on Musings of a Programming Addict: A generic class-level constraint for the Bean Validation APIAnonymoushttp://www.blogger.com/profile/16910067065508493885noreply@blogger.comBlogger13125tag:blogger.com,1999:blog-1306958590608174053.post-88395655268487903232022-09-23T14:00:33.126+02:002022-09-23T14:00:33.126+02:00Thank you so much for your excellent blog! I reall...Thank you so much for your excellent blog! I really enjoy to visit your very interesting post, Well done!<br /><a href="https://srislawyer.com/virginia-family-lawyer-fairfax/" rel="nofollow">Best Family Court Lawyers Near Me</a><br /><a href="https://srislawyer.com/virginia-family-lawyer-fairfax/" rel="nofollow">Best Female Family Law Attorney</a>Sruthi Karanhttps://www.blogger.com/profile/17059406463629405366noreply@blogger.comtag:blogger.com,1999:blog-1306958590608174053.post-11443334468700921092022-05-06T11:57:07.780+02:002022-05-06T11:57:07.780+02:00This was a fantastically written post with a strai...This was a fantastically written post with a straightforward description. That is why I am also providing you with my article. The post is based on a <a href="https://keyboardkeytester.thinkific.com/courses/9-easy-online-keyboard-tester-2022" rel="nofollow"> keyboard tester tools </a>, and I discovered some popular 9 simple online keyboard tester websites to help you test laptop keyboards online quickly and easily.MeriFainhttps://www.blogger.com/profile/04711548377562674278noreply@blogger.comtag:blogger.com,1999:blog-1306958590608174053.post-44205506223588246242019-07-04T12:53:44.769+02:002019-07-04T12:53:44.769+02:00The thing with @AssertTrue is that I lose the abil...The thing with @AssertTrue is that I lose the ability to add my custom constraint violation messages when cross parameter validation fails. With @AssertTrue, I just have the info that it failed, but I dont know why. This can be done via custom validator as shown initially in this article.stewehttps://www.blogger.com/profile/00832693872732787617noreply@blogger.comtag:blogger.com,1999:blog-1306958590608174053.post-62480403791352554932016-12-02T08:21:21.611+01:002016-12-02T08:21:21.611+01:00Thanks for sharing this valuable information.
jav...<br /><br />Thanks for sharing this valuable information. <br /><a href="http://www.1croreprojects.com" rel="nofollow">java projects in chennai</a><br /><a href="http://www.1croreprojects.com" rel="nofollow">dotnet projects in chennai</a><br /><a href="http://www.1croreprojects.com" rel="nofollow">ns2 projects in chennai</a>1croreprojectshttps://www.blogger.com/profile/07251930030015416724noreply@blogger.comtag:blogger.com,1999:blog-1306958590608174053.post-47126880906737298212014-12-31T14:38:41.488+01:002014-12-31T14:38:41.488+01:00A more elegant solution here :
http://soadev.blog...A more elegant solution here : <br />http://soadev.blogspot.fr/2010/01/jsr-303-bean-validation.htmlAnonymousnoreply@blogger.comtag:blogger.com,1999:blog-1306958590608174053.post-72242528703771407482012-11-20T01:53:50.359+01:002012-11-20T01:53:50.359+01:00Yes, I much prefer using the @AssertTrue validatio...Yes, I much prefer using the @AssertTrue validation for this - partly because it's standard (and so doesn't require me to maintain code to do it), and partly because you can use it more than once. That's important, because you might have several multi-field validations you need to run, each with different messages.Simonnoreply@blogger.comtag:blogger.com,1999:blog-1306958590608174053.post-98423525722973842012-05-30T15:18:09.645+02:002012-05-30T15:18:09.645+02:00Seriously, what's wrong with having the assert...Seriously, what's wrong with having the assertions right in the constructor. This has a series of advantages:<br /><br />First, you can get rid of all the annotation and framework madness and shrink down the code to the actual business code. Second, you cannot instantiate the object in an invalid state. Especially the latter is extremely valuable as one can be sure the object is a valid state if one gets one returned from a method or handed into a method. With the shown approach you always have to rely on someone call the isValid() method and can never be sure someone already did. So you'll probably end up with unnecessary calls to that method "just to be sure".Olliehttps://www.blogger.com/profile/10119949826664398804noreply@blogger.comtag:blogger.com,1999:blog-1306958590608174053.post-25090144240042119902012-01-05T21:11:43.416+01:002012-01-05T21:11:43.416+01:00Anonymous,
I got to admit that I'm not really...Anonymous,<br /><br />I got to admit that I'm not really convinced of the idea anymore. <br /><br />A much simpler approach would just be<br /><br />class MyBean {<br /><br /> @AssertTrue<br /> public boolean isValid() {<br /> //validation logic<br /> }<br /><br />}Anonymoushttps://www.blogger.com/profile/16910067065508493885noreply@blogger.comtag:blogger.com,1999:blog-1306958590608174053.post-53476655291495372212012-01-05T11:41:27.746+01:002012-01-05T11:41:27.746+01:00Hi,
I've stumbled upon this by chance and I a...Hi,<br /><br />I've stumbled upon this by chance and I am really impressed with the idea and the implementation even though the points made out by Pino are true for my needs.Anonymousnoreply@blogger.comtag:blogger.com,1999:blog-1306958590608174053.post-28370466889923718202010-03-14T22:54:38.708+01:002010-03-14T22:54:38.708+01:00Anders,
it's hard to say what the actual prob...Anders,<br /><br />it's hard to say what the actual problem is without knowing the project itself.<br /><br />What concerns retrieving additional dependencies within validator implementations, you could either use JNDI lookups (have a look at the JPA 2 spec chapter 7.2 how to lookup an EntityManager within a Java EE container) or you could provide a custom <a href="http://docs.jboss.org/hibernate/stable/validator/reference/en/html_single/#d0e2303" rel="nofollow">ConstraintValidatorFactory</a> which performs DI on the validator instances it creates.Anonymoushttps://www.blogger.com/profile/16910067065508493885noreply@blogger.comtag:blogger.com,1999:blog-1306958590608174053.post-70627988406679922392010-03-11T18:15:56.055+01:002010-03-11T18:15:56.055+01:00Hi Gunnar
Great articles, they've helped us a ...Hi Gunnar<br />Great articles, they've helped us a lot in getting validation up and running.<br /><br />We are trying to create a class-level constraint for checking that entities are unique. For some strange reason Hibernate doesn't do this before submitting to the database (even though @UniqueConstraint's are in place). This UniqueValidator needs access to the EntityManager. Any idea on how to get access to it?<br /><br />We've tried with @PersistenceContext and using Persistence.createEntityManager(".."), in the first case we get null and in the second we get an exception:<br />"The chosen transaction strategy requires access to the JTA TransactionManager".<br /><br />We suspect it has something to do with how the Validator is instantiated, but we're a bit inexperienced with JPA and JCDI.<br /><br />Any suggestions?<br /><br />Regards, AndersUnknownhttps://www.blogger.com/profile/15403040585460314967noreply@blogger.comtag:blogger.com,1999:blog-1306958590608174053.post-25649794863148149682010-02-17T22:12:32.396+01:002010-02-17T22:12:32.396+01:00Hi Pino,
great to hear you like the idea. To answ...Hi Pino,<br /><br />great to hear you like the idea. To answer your questions:<br /><br />1) Generally it's not possible to restrict the types at which a given annotation may be specified, you only can specify the allowed element kinds (types, methods, fields etc.).<br /><br />What one can do though is to provide an <a href="http://java.sun.com/javase/6/docs/api/javax/annotation/processing/Processor.html" rel="nofollow">annotation processor</a>, that hooks into the compiler and raises an error upon compilation, if a constraint annotation is given at an unsupported type. Actually I'm <a href="http://anonsvn.jboss.org/repos/hibernate/validator/trunk/hibernate-validator-annotation-processor/" rel="nofollow">building</a> such a processor right now for Hibernate Validator.<br /><br />2) Without knowing the exact requirements, I probably would work with a specific constraint in that case. <br /><br />Within the validator implementation the <a href="http://java.sun.com/javaee/6/docs/api/javax/validation/ConstraintValidatorContext.html" rel="nofollow">ConstraintValidatorContext</a> can be used to create custom constraint violations depending on your specific rules.<br /><br />Alternatively you might create a complex constraint <a href="http://docs.jboss.org/hibernate/stable/validator/reference/en/html_single/#validator-customconstraints-compound" rel="nofollow">composed</a> from multiple constraints representing the different rules.<br /><br />GunnarAnonymoushttps://www.blogger.com/profile/16910067065508493885noreply@blogger.comtag:blogger.com,1999:blog-1306958590608174053.post-9877269368013230982010-02-17T08:36:34.720+01:002010-02-17T08:36:34.720+01:00Hi Gunnar!
This idea is great. I just wonder the f...Hi Gunnar!<br />This idea is great. I just wonder the following things:<br />1)What will happen if I will put a @SelfValidating annotation on a class that doesn't implement a Validatable interface? If this will not throw a compile-time error then better not to enforce implementation of Validatable interface but just invoke the isValid() method on that object through java reflection(note - this can be achieve in my case as just placing a plain <a href="http://soadev.blogspot.com/2010/01/jsr-303-bean-validation.html" rel="nofollow">@AssertMethodAsTrue</a> annotation with no parameters on my class because the default method that it will invoke is isValid()).<br /><br />2)What if I have three business logic that needs to be validated on this class, of course I could call them all inside the isValid() method, but what if just one of them fails, how can I display a specific message on the failing logic?<br /><br />If this concerns will be resolved, then I could say that this @SelfValidating annotation will be a must-have.<br /><br />regards,<br />PinoRommel Pinohttps://www.blogger.com/profile/18179809528284596087noreply@blogger.com