使用Spring Data JDBC我希望将内部变量“inner”映射到外部表中的 varchar 列,而不是映射到自己的表。这可能吗?
public class Outer {
@Id
private String id;
private Inner inner;
}
...
public class Inner {
private String value;
}
...
public OuterRepository implements CrudRepository<Outer, String> {}
这是我的上下文配置:
@Configuration
@EnableJdbcRepositories
public class Config extends JdbcConfiguration {
@Bean
protected JdbcCustomConversions jdbcCustomConversions() {
return new JdbcCustomConversions(asList(StringToInner.INSTANCE, InnerToString.INSTANCE));
}
@WritingConverter
enum InnerToString implements Converter<Inner, String> {
INSTANCE;
@Override
public String convert(Inner source) {
return source.getValue();
}
}
@ReadingConverter
enum StringToInner implements Converter<String, Inner> {
INSTANCE;
@Override
public Inner convert(String source) {
return new Inner(source);
}
}
}