How to make delegate fields validation (using annotation) in wrapper? Is it possible to make annotation on getter instead on fields?
I tried to do something like this:
class A{
private Object obj;
public Object getObj(){
return obj;
}
public void setObj(Object obj){
this.obj = obj;
}
}
#Component
class Wrapper{
A a;
public Wrapper(A a){
this.a = a;
}
#NotNull
public getObj(){
a.getObj();
}
public setObj(Object obj){
a.setObj(obj);
}
}
Related
I have an entity with non-mapped properties and a LifeCycleCallBback Listener.
In PrePersist/PreUpdate callback the non-mapped property is null but before the save was triggered this property was set.
I tried to annotate it with #Transient but it doesn't work as well. I tried to staticly give the property to the listnener but it does not support heavy loading.
public abstract class AbstractA {
private Object a;
public void setA(Object a){
this.a = a;
}
public Object getA(){
return a;
}
}
#Entity
#EntityListeners(MyListener.class)
public class B extends AbstractA {
//...
}
public class BService {
public B save(B b){
b.setA(object); // Object is not null and object.getA returns the good value
bRepository.saveAndFlush(b); // Triggers the prePersist Callback
}
}
public class MyListener {
#PrePersist
public void prePersist(AbstractA a){
a.getA(); // here a.a is null
}
}
I am having two java class as below,
public class Class1{
private Object actionObject;
public Object getActionObject() {
return actionObject;
}
public void setActionObject(Object actionObject) {
this.actionObject = actionObject;
}
}
Second class
public class Class2 {
private Long id;
private int idver;
private int valueDate;
}
There are two statement as below,
Class1 deserializedValue = (Class1) event.getDeserializedValue();
Class2.class.isAssignableFrom(deserializedValue.getActionObject().getClass());
I want to mock the second statement
Class2.class.isAssignableFrom(deserializedValue.getActionObject().getClass());
how can i do this?
For testing purposes you can use a strategy pattern. You just need an interface or an abstract class with two different implementations. One of them is the mock implementation, something like this:
public interface EventStrategy {
// More methods...
boolean isAssignableFrom(final Object object);
}
public class MyEvent implements EventStrategy {
public boolean isAssignableFrom(final Object object) {
return Class2.class.isAssignableFrom(object.getClass());
}
}
public class MockEvent implements EventStrategy {
public boolean isAssignableFrom(final Object object) {
return true;
}
}
#Entity
#Inheritance(strategy = InheritanceType.JOINED)
public class A{
private long id;
}
#Entity
public class B extends A{
private String bProperty;
}
#Entity
public class C extends A{
private String cProperty;
}
#Entity
public class Person{
#OneToMany
private Set<A> a;
}
when I use person.getVehicles
How can I know the A is B or C?
I'm using instanceof to check and cast it to get bProperty or cProperty.
Is there any other better way?
The only safe way is to use a polymorphic method. Even instanceof will not work because the instance might actually be a proxy, i.e. a subclass of A that is neither a B or a C, but delegates to a B or a C.
public class A{
private long id;
public abstract boolean isB();
public abstract boolean isC();
public abstract String getBProperty();
public abstract String getCProperty();
}
public class B extends A{
private String bProperty;
public boolean isB() {
return true;
}
public boolean isC() {
return false;
}
public String getBProperty() {
return bProperty;
}
public String getCProperty() {
throw new IllegalStateException("I'm not a C");
}
}
To be cleaner, try using the visitor pattern. I've written a blog post about it. It's in French, but it should be easily translatable.
I have a complex object that contains two BigDecimal field
public class Test
{
private BigDecimal property1;
private BigDecimal property2;
//setter and getter method
}
Now when user entered property1 in spring form It should be 10% of property2.
This validation i have to do using spring validation framework.
public class TestValidator implements Validator {
public boolean supports(Class clazz) {
return Test.class.equals(clazz);
}
public void validate(Object obj, Errors e) {
Test t = (Test) obj;
if(t.getProperty1()!=0.1*t.getProperty2()){
e.rejectValue("Test", "property1 not 10% of property2");
}
}
}
Why does example A work, while example B throws a "JAXB annotation is placed on a method that is not a JAXB property" exception?
I'm using JAX-WS with Spring MVC.
Example A
package com.casanosa2.permissions;
import javax.xml.bind.annotation.XmlAccessType;
import javax.xml.bind.annotation.XmlAccessorType;
import javax.xml.bind.annotation.XmlElement;
import javax.xml.bind.annotation.XmlType;
#XmlAccessorType(XmlAccessType.PROPERTY)
#XmlType(name = "FooXMLMapper")
public class FooXMLMapper implements IFoo {
#XmlElement
private final boolean propA;
#XmlElement
private final boolean propB;
public FooMapper(IFoo foo) {
propA = foo.getPropA()
propB = foo.getPropB()
}
public FooMapper() {
propA = false;
propB = false;
}
#Override
public boolean getPropA() {
return propA;
}
#Override
public boolean getPropB() {
return propB;
}
}
Example B
#XmlAccessorType(XmlAccessType.PROPERTY)
#XmlType(name = "FooXMLMapper")
public class FooXMLMapper {
private final IFoo foo;
public FooMapper() {
foo = new IFoo() {
#Override
public boolean getPropA() {
return false;
}
#Override
public boolean getPropB() {
return false;
}
};
}
public FooXMLMapper(IFoo foo) {
this.foo = foo;
}
#XmlElement
public boolean getPropA() {
return foo.getPropA();
}
#XmlElement
public boolean getPropB() {
return foo.getPropB();
}
}
I believe the accessors are ignored if it's looking directly at the instance variables and in your example B there are no actual instance variables of the right name. You have to tell it explicitly to use #XmlAccessorType(XmlAccessType.NONE) on the class and #XmlElement and #XmlAttribute on the get/set methods. At least, that's what I ended up doing with my JAXB mapping.
I believe for it to be a proper JAXB property, you would need setters for them as well as getters. (you would likely need a default constructor as well).
I haven't tried your code yet, but it's example A that looks wrong, not B. In example A you have specified the property accessors (get/set methods) but you have annotated the class fields instead (instance variables).