-->
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.  [ 2 posts ] 
Author Message
 Post subject: Hibernate Validator implementation at cascaded level
PostPosted: Tue Jul 25, 2017 11:18 am 
Newbie

Joined: Tue Jul 25, 2017 11:02 am
Posts: 1
Hi Team,

I am facing challenges, with validation getting applied at cascaded level. Here is details below.
____________________________________________________________________________________________________________________
@Entity
@Table(name = "business_function_object", schema = "common")
public class BusinessFunctionObject extends AuditableEntity {

private static final long serialVersionUID = 4658765981177645668L;

@EmbeddedId
private BusinessFunctionObjectPK businessFunctionObjectPK;

BusinessFunctionObject() {
}

public BusinessFunctionObject(final BusinessFunction businessFunction, final BusinessObject businessObject) {
this.businessFunctionObjectPK = new BusinessFunctionObjectPK(businessFunction, businessObject);
}

public BusinessFunctionObjectPK getBusinessFunctionObjectPK() {
return businessFunctionObjectPK;
}

@Valid
public BusinessFunctionObject setBusinessFunctionObjectPK(@Valid final BusinessFunctionObjectPK businessFunctionObjectPK) {
this.businessFunctionObjectPK = businessFunctionObjectPK;
return this;
}

@SuppressWarnings("unchecked")
@Override
public BusinessFunctionObjectPK getId() {
return businessFunctionObjectPK;
}

}
______________________________________________________________________________________________________________________
@Entity
@Table(name = "business_function", catalog = "ordering", schema = "common")
@AttributeOverrides({
@AttributeOverride(name = "code", column = @Column(name = "business_function_cd", length = 4, insertable = false)),
@AttributeOverride(name = "name", column = @Column(name = "business_function_name", length = 30, insertable = false)) })
public class BusinessFunction extends CodifiedLabeledAuditableEntity {

private static final long serialVersionUID = 1656189018581041355L;


BusinessFunction() {
super();
businessFunctionObjects = new HashSet<>();
}

public BusinessFunction(String code, String name) {
super(code, name);
businessFunctionObjects = new HashSet<>();
}

@OneToMany(mappedBy = "businessFunctionObjectPK.businessFunction")
private Set<BusinessFunctionObject> businessFunctionObjects = new HashSet<>();

public Set<BusinessFunctionObject> getBusinessFunctionObjects() {
return businessFunctionObjects;
}

@Valid
public BusinessFunction setBusinessFunctionObjects(final Set<BusinessFunctionObject> businessFunctionObjects) {
this.businessFunctionObjects = businessFunctionObjects;
return this;
}

public BusinessFunction addBusinessFunctionObject(final BusinessFunctionObject businessFunctionObject) {
this.businessFunctionObjects.add(businessFunctionObject);
return this;
}

@SuppressWarnings("unchecked")
@Override
public String getId() {
return code;
}

}
____________________________________________________________________________________________________________________
@Entity
@Table(name = "business_object", catalog = "ordering", schema = "common")
@AttributeOverrides({
@AttributeOverride(name = "code", column = @Column(name = "business_object_cd", length = 4, insertable = false)),
@AttributeOverride(name = "name", column = @Column(name = "business_object_name", length = 30, insertable = false)) })

public class BusinessObject extends CodifiedLabeledAuditableEntity {

private static final long serialVersionUID = -488323504661657378L;

BusinessObject() {
super();
}

public BusinessObject(String code, String name) {
super(code, name);
}

@OneToMany(mappedBy = "businessFunctionObjectPK.businessObject", fetch = FetchType.LAZY)
private Set<BusinessFunctionObject> businessFunctionObjects = new HashSet<>();

@OneToMany(mappedBy = "businessObjectAttributeRelationshipPK.businessObject")
private Set<BusinessObjectAttributeRelationship> businessObjectAttributeRelationships = new HashSet<>();

public Set<BusinessObjectAttributeRelationship> getBusinessObjectAttributeRelationships() {
return businessObjectAttributeRelationships;
}

public BusinessObject setBusinessObjectAttributeRelationship(final Set<BusinessObjectAttributeRelationship> businessObjectAttributeRelationships) {
this.businessObjectAttributeRelationships = businessObjectAttributeRelationships;
return this;
}

public BusinessObject addBusinessObjectAttributeRelationship(final BusinessObjectAttributeRelationship businessObjectAttributeRelationship) {
this.businessObjectAttributeRelationships.add(businessObjectAttributeRelationship);
return this;
}


public Set<BusinessFunctionObject> getBusinessFunctionObjects() {
return businessFunctionObjects;
}

@Valid
public BusinessObject setBusinessFunctionObjects(@Valid final Set<BusinessFunctionObject> businessFunctionObjects) {
this.businessFunctionObjects = businessFunctionObjects;
return this;
}

public BusinessObject addBusinessFunctionObject(final BusinessFunctionObject businessFunctionObject) {
this.businessFunctionObjects.add(businessFunctionObject);
return this;
}


}
_____________________________________________________________________________________________________________________

import com.elementfleet.ordering.common.testing.theories.EntityTheory;
import static com.elementfleet.ordering.common.util.CommonTestUtil.add;
import org.junit.Before;
import org.junit.experimental.theories.DataPoints;
public class BusinessFunctionObjectTest extends EntityTheory{

@Before
public void setUp() throws Exception {
ENTITY_OBJECT = new BusinessFunctionObject(new BusinessFunction("ORD", "Ordering"), new BusinessObject("ORD", "Ordering"));

//add(ENTITY_FAILED_VALIDATION_MAP,new BusinessFunctionObject(new BusinessFunction("ORD", "Ordering"), new BusinessObject("ORD", "Ordering")));
add(ENTITY_FAILED_VALIDATION_MAP,new BusinessFunctionObject(new BusinessFunction("O RD", "Ordering"), new BusinessObject("O RD", "Ordering")),"businessFunctionObjectPK");
}

@DataPoints
public static BusinessFunctionObject[] dataPoints() {
return new BusinessFunctionObject[] {
new BusinessFunctionObject(new BusinessFunction("ORD", "Ordering"), new BusinessObject("ORD", "Ordering")),
new BusinessFunctionObject(new BusinessFunction("ORD1", "Ordering"), new BusinessObject("ORD1", "Ordering")),
new BusinessFunctionObject(new BusinessFunction("ORD2", "Ordering"), new BusinessObject("ORD2", "Ordering"))};
}
}
__________________________________________________________________________________________________________________
@Rule
public ErrorCollector errorCollector = new ErrorCollector();

protected Map<Object, List<String>> ENTITY_FAILED_VALIDATION_MAP = new HashMap<Object, List<String>>();//NOSONAR
protected Map<Object, List<String>> ENTITY_PASSED_VALIDATION_MAP = new HashMap<Object, List<String>>();//NOSONAR

@Test
public void validationFails() {
assumeFalse(ENTITY_FAILED_VALIDATION_MAP.isEmpty());
for (Map.Entry<Object, List<String>> entry : ENTITY_FAILED_VALIDATION_MAP.entrySet()) {
Object eachObject = entry.getKey();
List<String> fieldNames = entry.getValue();
if (null != fieldNames) {
for (String fieldName : fieldNames) {
errorCollector.checkThat(
String.format("Not found expected failed validation on field: %s", fieldName),
validationFor(eachObject, onField(fieldName)), fails());
errorCollector.checkThat(
String.format("Incorrect expected validation failure count on field: %s", fieldName),
validationCount(eachObject, fieldName),
equalTo(Collections.frequency(fieldNames, fieldName)));
}
errorCollector.checkThat("Incorrect total expected validation failure count",
validationCount(eachObject), equalTo(fieldNames.size()));
}
}
}

@Test
public void validationSuccedes() {
assumeFalse(ENTITY_PASSED_VALIDATION_MAP.isEmpty());
for (Map.Entry<Object, List<String>> entry : ENTITY_PASSED_VALIDATION_MAP.entrySet()) {
Object eachObject = entry.getKey();
List<String> fieldNames = entry.getValue();
if (null != fieldNames) {
for (String fieldName : fieldNames) {
errorCollector.checkThat(
String.format("Not found expected passed validation on field: %s", fieldName),
validationFor(eachObject, onField(fieldName)), succedes());
errorCollector.checkThat(
String.format("Incorrect expected validation success count on field: %s", fieldName),
validationCount(eachObject, fieldName), equalTo(0));
}
errorCollector.checkThat("Total validation count is not zero", validationCount(eachObject), equalTo(0));
}
}
}
______________________________________________________________________________________________________________________
If I have class with simple attribute "code" and "name" , validation test case works fine.
But if i have cascaded level of implementation for example:

add(ENTITY_FAILED_VALIDATION_MAP,new BusinessFunctionObject(new BusinessFunction("O RD", "Ordering"), new BusinessObject("O RD", "Ordering")),"businessFunctionObjectPK");

Unit test case is failing with cascaded level, here is the example.
java.lang.AssertionError: Not found expected failed validation on field: businessFunctionObjectPK
Expected: not valid
but: was <[]>
at org.hamcrest.MatcherAssert.assertThat(MatcherAssert.java:20)
at org.junit.Assert.assertThat(Assert.java:956)
at org.junit.rules.ErrorCollector$1.call(ErrorCollector.java:65)
at org.junit.rules.ErrorCollector.checkSucceeds(ErrorCollector.java:78)
at org.junit.rules.ErrorCollector.checkThat(ErrorCollector.java:63)
at com.elementfleet.ordering.common.testing.theories.EntityTheory.validationFails(EntityTheory.java:54)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:498)
at org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:50)
at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:12)
at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:47)
at org.junit.experimental.theories.Theories$TheoryAnchor$2.evaluate(Theories.java:274)
at org.junit.internal.runners.statements.RunBefores.evaluate(RunBefores.java:26)
at org.junit.rules.Verifier$1.evaluate(Verifier.java:35)
at org.junit.rules.RunRules.evaluate(RunRules.java:20)
at org.junit.experimental.theories.Theories$TheoryAnchor$1$1.evaluate(Theories.java:232)
at org.junit.experimental.theories.Theories$TheoryAnchor.runWithCompleteAssignment(Theories.java:218)
at org.junit.experimental.theories.Theories$TheoryAnchor.runWithAssignment(Theories.java:204)
at org.junit.experimental.theories.Theories$TheoryAnchor.evaluate(Theories.java:187)
at org.junit.runners.ParentRunner.runLeaf(ParentRunner.java:325)
at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:78)
at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:57)
at org.junit.runners.ParentRunner$3.run(ParentRunner.java:290)
at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:71)
at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:288)
at org.junit.runners.ParentRunner.access$000(ParentRunner.java:58)
at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:268)
at org.junit.runners.ParentRunner.run(ParentRunner.java:363)
at org.eclipse.jdt.internal.junit4.runner.JUnit4TestReference.run(JUnit4TestReference.java:86)
at org.eclipse.jdt.internal.junit.runner.TestExecution.run(TestExecution.java:38)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:459)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:678)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTestRunner.java:382)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java:192)

java.lang.AssertionError: Incorrect expected validation failure count on field: businessFunctionObjectPK
Expected: <1>
but: was <0>
at org.hamcrest.MatcherAssert.assertThat(MatcherAssert.java:20)
at org.junit.Assert.assertThat(Assert.java:956)
at org.junit.rules.ErrorCollector$1.call(ErrorCollector.java:65)
at org.junit.rules.ErrorCollector.checkSucceeds(ErrorCollector.java:78)
at org.junit.rules.ErrorCollector.checkThat(ErrorCollector.java:63)
at com.elementfleet.ordering.common.testing.theories.EntityTheory.validationFails(EntityTheory.java:57)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:498)
at org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:50)
at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:12)
at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:47)
at org.junit.experimental.theories.Theories$TheoryAnchor$2.evaluate(Theories.java:274)
at org.junit.internal.runners.statements.RunBefores.evaluate(RunBefores.java:26)
at org.junit.rules.Verifier$1.evaluate(Verifier.java:35)
at org.junit.rules.RunRules.evaluate(RunRules.java:20)
at org.junit.experimental.theories.Theories$TheoryAnchor$1$1.evaluate(Theories.java:232)
at org.junit.experimental.theories.Theories$TheoryAnchor.runWithCompleteAssignment(Theories.java:218)
at org.junit.experimental.theories.Theories$TheoryAnchor.runWithAssignment(Theories.java:204)
at org.junit.experimental.theories.Theories$TheoryAnchor.evaluate(Theories.java:187)
at org.junit.runners.ParentRunner.runLeaf(ParentRunner.java:325)
at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:78)
at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:57)
at org.junit.runners.ParentRunner$3.run(ParentRunner.java:290)
at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:71)
at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:288)
at org.junit.runners.ParentRunner.access$000(ParentRunner.java:58)
at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:268)
at org.junit.runners.ParentRunner.run(ParentRunner.java:363)
at org.eclipse.jdt.internal.junit4.runner.JUnit4TestReference.run(JUnit4TestReference.java:86)
at org.eclipse.jdt.internal.junit.runner.TestExecution.run(TestExecution.java:38)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:459)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:678)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTestRunner.java:382)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java:192)

java.lang.AssertionError: Incorrect total expected validation failure count
Expected: <1>
but: was <0>
at org.hamcrest.MatcherAssert.assertThat(MatcherAssert.java:20)
at org.junit.Assert.assertThat(Assert.java:956)
at org.junit.rules.ErrorCollector$1.call(ErrorCollector.java:65)
at org.junit.rules.ErrorCollector.checkSucceeds(ErrorCollector.java:78)
at org.junit.rules.ErrorCollector.checkThat(ErrorCollector.java:63)
at com.elementfleet.ordering.common.testing.theories.EntityTheory.validationFails(EntityTheory.java:67)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:498)
at org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:50)
at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:12)
at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:47)
at org.junit.experimental.theories.Theories$TheoryAnchor$2.evaluate(Theories.java:274)
at org.junit.internal.runners.statements.RunBefores.evaluate(RunBefores.java:26)
at org.junit.rules.Verifier$1.evaluate(Verifier.java:35)
at org.junit.rules.RunRules.evaluate(RunRules.java:20)
at org.junit.experimental.theories.Theories$TheoryAnchor$1$1.evaluate(Theories.java:232)
at org.junit.experimental.theories.Theories$TheoryAnchor.runWithCompleteAssignment(Theories.java:218)
at org.junit.experimental.theories.Theories$TheoryAnchor.runWithAssignment(Theories.java:204)
at org.junit.experimental.theories.Theories$TheoryAnchor.evaluate(Theories.java:187)
at org.junit.runners.ParentRunner.runLeaf(ParentRunner.java:325)
at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:78)
at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:57)
at org.junit.runners.ParentRunner$3.run(ParentRunner.java:290)
at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:71)
at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:288)
at org.junit.runners.ParentRunner.access$000(ParentRunner.java:58)
at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:268)
at org.junit.runners.ParentRunner.run(ParentRunner.java:363)
at org.eclipse.jdt.internal.junit4.runner.JUnit4TestReference.run(JUnit4TestReference.java:86)
at org.eclipse.jdt.internal.junit.runner.TestExecution.run(TestExecution.java:38)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:459)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:678)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTestRunner.java:382)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java:192)


Any help would be appreciated.

Thanks,
Sudeen


Top
 Profile  
 
 Post subject: Re: Hibernate Validator implementation at cascaded level
PostPosted: Tue Aug 08, 2017 9:15 am 
Hibernate Team
Hibernate Team

Joined: Sat Jan 24, 2009 12:46 pm
Posts: 388
Hi, that's a lot of code to look at. Could you try and condense this to a minimal reproducer for the issue you are observing? Thanks!

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


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 2 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.