我有一些问题。 我有两个实体和一个 DTO。
@Entity
class X {
@OneToMany
Set<Y> set;
}
@Entity
class Y {
Long id;
@ManyToOne
X x;
}
class XDTO {
Set<Long> yId;
}
在这种情况下如何实现映射器?
@Mapper
public mapper() {
XDTO toDto (X x);
X toEntity (XDTO xDTO);
}
最佳答案
这样的事情应该有效:
@Mapper(uses=EntityMapper.class)
public interface XMapper() {
@Mapping(source="set", target="yId")
XDTO toDto (X x);
@InheritInverseConfiguration
X toEntity (XDTO xDTO);
}
public class EntityMapper {
EntityManager em = ...;
public <T extends BaseEntity> T resolve(long id, @TargetType Class<T> entityClass) {
entityManager.find( entityClass, id );
}
public long toReference(BaseEntity entity) {
return entity != null ? entity.getId() : null;
}
}
关于java - 如何将 id 集合转换为 beans 集合?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45187850/