基于列值反序列化hibernate json列



我有一个MySQL表,看起来像这个

-------------------------------------------------------------------------
| id  | name    | type  | properties                                            |
| 1   | nameA   | A     | {"prop_a1" : "value_1", "prop_a2": "value_2"}   |
| 2   | nameB1  | B     | {"prop_b1" : "value_3"}                         |
| 3   | nameB2  | B     | {"prop_b1" : "value_4"}.                        |
-------------------------------------------------------------------------
Class BaseEntity implements Serializable {
}
Class EntityA exends BaseEntity {
String prop_a1;
String prop_a2;
}
class EntityB extends BaseEntity {
String prop_b1;
}
class ParentEntity<T extends BaseEntity> {
Integer id;
String name;
Enum type;
T properties;
}

我正试图根据"类型"列将多态对象保留在"属性"列中。尝试使用JsonTypeInfo,虽然它可以写入MySQL表,但在读取时失败,错误为:com.fasterxml.jackson.databind.exc.UnrecognizedPropertyException: Unrecognized field

以前有人处理过这个用例吗?

您可以在ParentEntity上声明抽象的getter和setter方法,并在子类中声明持久属性。

最新更新