In my service I created abstract class ShapeEntity with two abstract methods.
@Entity
@Inheritance(strategy = TABLE_PER_CLASS)
public abstract class ShapeEntity {
@Id
private String id = UUID.randomUUID().toString();
public abstract double getArea();
public abstract double getPerimeter();
}
Other entites inherit id and methods, but have own parameters like for example radius or width:
@Entity
@Table(name = "circles")
public class CircleEntity extends ShapeEntity {
private Double radius;
@Override
public double getArea() {
return Math.PI * Math.pow(2, radius);
}
@Override
public double getPerimeter() {
return 2 * Math.PI * radius;
}
}
@Entity
@Table(name = "squares")
public class SquareEntity extends ShapeEntity {
private double width;
@Override
public double getArea() {
return width * width;
}
@Override
public double getPerimeter() {
return 4 * width;
}
}
and now I am working at get all objects with some parameters. I would like to make something like findAll() with parameters for example area, type, and radius, width.
I tried with: tkaczmarzyk / specification-arg-resolver but when I put radius as request parameter I am getting error:
"Unable to locate Attribute with the the given name [radius] on this ManagedType [com.example.ShapeEntity]; nested exception is java.lang.IllegalArgumentException: Unable to locate Attribute with the the given name [radius] on this ManagedType [com.example.ShapeEntity]" becouse it can not find field in ShapeEntity (what is obvious).
So is there any other way to make dynamic earch by request parameters? Can I do it by one findAll() method with some parameters?
I can do it with many if / else loops but I think it will not be the best solution. The second problem is what if I add rectangle (RectangleEntity) which has width too.. I would like to inlcude every shape which has given width so searching should be through squares and rectangles.