I am new at abstract class as Entity so for training I created abstract class
@Entity
@Inheritance(strategy = TABLE_PER_CLASS)
@Getter
@Setter
@NoArgsConstructor
public abstract class ShapeEntity {
@Id
@GeneratedValue(generator = "system-uuid")
@GenericGenerator(name = "system-uuid", strategy = "uuid")
private String id = UUID.randomUUID().toString();
}
and classes which extand Shape:
public class SquareEntity extends ShapeEntity {
...
}
public class CircleEntity extends ShapeEntity {
...
}
every entity has repostiory like that:
public interface CircleEntityRepository extends JpaRepository<CircleEntity, String> {
}
When I want to update entity I am searching it in every repository, next (if entity was found) map parameters and finnaly have to save that entity in database. How can I easy decide which repository should be used for it? I created method save with if else which now works:
public ShapeEntity save(ShapeEntity shapeEntity) {
if (shapeEntity.getClass().getName().contains("CircleEntity")) {
return circleEntityRepository.save((CircleEntity) shapeEntity);
} else if (shapeEntity.getClass().getName().contains("SquareEntity")) {
return squareEntityRepository.save((SquareEntity) shapeEntity);
} else {
throw new EntityNotFoundException();
}
}
but it is a bit uncomfortable becouse when I will have 20 entities I will have to create 20 else if loops. Can I do any interface for this or something like that?