How to load entity based on property value in java hibernate

Viewed 25

Is it possible to load an entity (class) based on property value in hibernate?

I'm trying to load a model or an entity from the database based on the value of "type" property in the table.

I'm really not familiar with Spring or Hibernate or Java, thus I've been struggling to solve this problem for the past two weeks.

Furthermore, I do not want to create a separate table or repository.

It is kinda hard to explain, but hopefully this sketch will help.

Thanks in advance.

code:

@Getter
@Setter
@Entity
class SimpleQuestion {
    @Id
    @GeneratedValue(strategy = GenerationType.AUTO)
    @Column(nullable = false)
    private Long id;

    @Column(nullable = true)
    private String helperText;

    @Column(nullable = false)
    private String label;

    @Column(nullable = true)
    private String placeholder;

    @Column(nullable = false)
    private int position;

    @Column(nullable = true)
    private boolean required;

    @Column(nullable = false)
    private QuestionType type;

}
@Getter
@Setter
@Entity
final class MultipleQuestion extends  SimpleQuestion{
    @Column(nullable = false)
    @ElementCollection
    private ArrayList<String> choices;

    @Column(nullable = true)
    @OneToMany
    private ArrayList<SimpleQuestion> textFields;

    @Column(nullable = true)
    private TextFieldsPosition textFieldsPosition;

}


enum TextFieldsPosition{
    Before ("before"),
    After ("after");
    TextFieldsPosition(String after) {
    }
}


enum QuestionType {
    Simple ("Simple"),
    Multiple ("Multiple");
    QuestionType(String after) {
    }
}
0 Answers
Related