Spring 抽象数据@Transient class
Spring data @Transient in abstract class
我有一个策略模式,它是用一个名为 presentation
的抽象 class 实现的
@JsonTypeInfo(use = JsonTypeInfo.Id.NAME, include = JsonTypeInfo.As.PROPERTY, property = "type")
@JsonSubTypes({
@Type( value=PiePresentation.class, name="PIE"),
@Type( value=BarPresentation.class, name="BAR")})
public abstract class Presentation {
String id;
List<? extends DetailResponse> data = new ArrayList();
String[] variables;
Map<String, Object> configurations;
@Transient
protected ExecutionState state;
}
还有两个子class,这是其中一个
public class PiePresentation extends Presentation{
@Transient
private List<Segment> response;
}
这个 class 是存储在 mongo 集合中的另一个 class 的字段。
问题是 spring 数据正在存储 Presentation class 的 state 字段,就像在继承中一样,@Transient 注释被忽略了。
我觉得你的代码不错。
您确定您使用了正确的@Transient 注释吗?
它必须从包 org.springframework.data.annotation 中导入。
不要使用 javax 持久性。
此外一定要使用MappingMongoConverter。基于注释的映射仅在您使用 MappingMongoConverter 作为 MongoTemplate 的后备转换器时才有效。如果您没有配置转换器,默认情况下将使用 SimpleMongoConverter,它只是将对象序列化为 Mongo,而无需查看任何元信息。
我有一个策略模式,它是用一个名为 presentation
的抽象 class 实现的@JsonTypeInfo(use = JsonTypeInfo.Id.NAME, include = JsonTypeInfo.As.PROPERTY, property = "type")
@JsonSubTypes({
@Type( value=PiePresentation.class, name="PIE"),
@Type( value=BarPresentation.class, name="BAR")})
public abstract class Presentation {
String id;
List<? extends DetailResponse> data = new ArrayList();
String[] variables;
Map<String, Object> configurations;
@Transient
protected ExecutionState state;
}
还有两个子class,这是其中一个
public class PiePresentation extends Presentation{
@Transient
private List<Segment> response;
}
这个 class 是存储在 mongo 集合中的另一个 class 的字段。 问题是 spring 数据正在存储 Presentation class 的 state 字段,就像在继承中一样,@Transient 注释被忽略了。
我觉得你的代码不错。
您确定您使用了正确的@Transient 注释吗? 它必须从包 org.springframework.data.annotation 中导入。 不要使用 javax 持久性。
此外一定要使用MappingMongoConverter。基于注释的映射仅在您使用 MappingMongoConverter 作为 MongoTemplate 的后备转换器时才有效。如果您没有配置转换器,默认情况下将使用 SimpleMongoConverter,它只是将对象序列化为 Mongo,而无需查看任何元信息。