I would like to build field which can be String, Boolean or Long. In this case I tried use generics type and inheratiance. All code compiles correctly but when I want to get some parameters from database I got a error like:
Object [id=1] was not of the specified subclass [unity.app.domain.Parameter] : Discriminator: 1",
What is more if I want to use post method to adding parameter to DB I got error like this:
“Type definition error: [simple type, class unity.app.domain.Parameter]; nested exception is com.fasterxml.jackson.databind.exc.InvalidDefinitionException: Cannot construct instance of unity.app.domain.Parameter (no Creators, like default construct, exist): abstract types either need to be mapped to concrete types, have custom deserializer, or contain additional type information\n at [Source: (PushbackInputStream); line: 1, column: 1]”,
I don’t have any ideas how to resolve this problem
@Data
@Entity
@Table(name = "parameter")
@Inheritance(strategy = InheritanceType.SINGLE_TABLE)
@DiscriminatorColumn(name = "parameter", discriminatorType = DiscriminatorType.STRING)
public abstract class Parameter<T> {
@Id
@GeneratedValue(strategy = GenerationType.TABLE)
@Column(name = "id", nullable = false)
private Long id;
@Column(name = "code")
private String code;
private T value;
@Transient
public abstract T getValue();
public abstract void setValue(T value);
}
@Entity
@DiscriminatorValue(value = "boolean")
public class BooleanValue extends Parameter<Boolean> {
@Column(name = "bool_value")
private Boolean booleanValue;
getValue()/setValue()[...]
}
@Entity
@DiscriminatorValue(value = "long")
public class LongValue extends Parameter<Long> {}
@Entity
@DiscriminatorValue(value = "string")
public class StringValue extends Parameter<String> {}
I am using postgresql and this is my table “parameter”:
id BIGINT,
code VARCHAR,
name VARCHAR,
description VARCHAR,
bool_value BOOLEAN,
string_value VARCHAR,
long_value BIGINT