【发布时间】:2015-11-22 09:29:24
【问题描述】:
正在使用的工具/框架:
-
JDK 1.7 Spring 3.2.5.RELEASEDozer 5.5.1
类:BaseModel.java
package com.demo.model;
public class BaseModel {
protected Long createdBy;
protected Timestamp createdTimestamp;
protected Long updatedBy;
protected Timestamp updatedTimestamp;
protected Long revision;
// ... Getters/Setters
}
类:Author.java
public class Author {
private BaseModel baseModel;
private Long id;
private String firstName;
private String lastName;
// Getter of baseModel
public BaseModel getBaseModel() {
return baseModel == null ? new BaseModel() : baseModel;
}
// ... Other Getters/Setters
}
类:BasePojo.java
package com.demo.web.pojo;
public class BasePojo {
protected Long createdBy;
protected Timestamp createdTimestamp;
protected Long updatedBy;
protected Timestamp updatedTimestamp;
protected Long revision;
// ... Getters/Setters
}
类:AuthorPojo.java
package com.demo.web.pojo;
public class AuthorPojo extends BasePojo {
private Long id;
private String firstName;
private String lastName;
// Getters/Setters
}
类:SpringConfig.java
@Configuration
@EnableAspectJAutoProxy(proxyTargetClass = true)
@ComponentScan("com.demo")
public class SpringConfig {
}
类:DemoDozerMapper.java
@Component("demoDozerMapper")
@Scope(ConfigurableBeanFactory.SCOPE_SINGLETON)
public class DemoDozerMapper extends DozerBeanMapper {
@PostConstruct
public void init() {
logger.info("PostConstruct called....");
this.initMappings();
}
private void initMappings() {
final BeanMappingBuilder authorMappingBuilder = new BeanMappingBuilder() {
@Override
protected void configure() {
mapping(Author.class, com.demo.web.pojo.Author.class,
TypeMappingOptions.mapId("authorMap"),
TypeMappingOptions.mapNull(true), TypeMappingOptions.oneWay())
.fields("baseModel.createdBy", "createdBy")
.fields("baseModel.createdTimestamp", "createdTimestamp")
.fields("baseModel.updatedBy", "updatedBy")
.fields("baseModel.updatedTimestamp", "updatedTimestamp");
}
};
addMapping(authorMappingBuilder);
}
}
类:Demo.java
public class Demo {
public static void main(String[] args) {
final ApplicationContext springAppContext = new AnnotationConfigApplicationContext(SpringConfig.class);
final Mapper dozerMapper = springAppContext.getBean("demoDozerMapper", DemoDozerMapper.class);
final Author authorModel = new Author();
authorModel.getBaseModel().setCreatedBy(Long.valueOf(1L));
authorModel.getBaseModel().setCreatedTimestamp(new Timestamp(System.currentTimeMillis()));
authorModel.setFirstName("First");
authorModel.setLastName("Last");
authorModel.setId(Long.valueOf(21101L));
final com.demo.web.pojo.Author author = new com.demo.web.pojo.Author();
dozerMapper.map(authorModel, author);
System.out.println("Author Pojo: " + author);
}
}
输出
Author Pojo: AuthorPojo {id=21101, firstName=First, lastName=Last, createdBy=null, createdTimestamp=null, updatedBy=null, updatedTimestamp=null, revision=null}
createdTimestamp 和 createdBy 字段没有从模型映射到 pojo。我做错什么了吗?有人可以帮忙吗?
【问题讨论】:
-
看起来您从未在
authorModel上设置baseModel对象,因此Author.getBaseModel()每次调用getter 时都会返回一个BaseModel的新实例,但该实例永远不会在正在转换的 Author 对象上设置。如果您显式创建BaseModel的实例并在其上设置createdBy和createdTimestamp,然后在Demo.java 中的authorModel对象上设置该实例,这行得通吗? -
嗨@IanA,我尝试了您的方法(明确创建了
BaseModel的实例,然后在其中设置这些createdBy和createdTimestamp,然后将其设置为authorModel;它仍然不起作用。
标签: java spring spring-java-config dozer