5

我有 2个具有一对多关系的实体CallRecordsCallRecordOperators ,如下所示

 public class CallRecords {

    @Id
    @Column(name = "id", unique = true)
    private String id;

    @Column(columnDefinition = "varchar(255) default ''")
    private String callerNumber = "";

    @OneToMany(mappedBy="callrecord")
    private List<CallRecordOperators> callRecordOperators = new ArrayList<CallRecordOperators>();


   //getter setters
}

public class CallRecordOperators {

    @Id
    @Column(name = "id", length = 50, unique = true, nullable = false, insertable = false, updatable = false)
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    private Long id;

    @JsonIgnore
    @ManyToOne
    @JoinColumn(name = "callRecordId")
    private CallRecords callrecord;

    @ManyToOne
    @JoinColumn(name = "operatorId")
    private Operator operator;

    @Formats.DateTime(pattern = "yyyy-MM-dd HH:mm:yy")
    @Column(columnDefinition = "TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP")
    private Date startTime = new Date();

    @Column(columnDefinition = "varchar(100) default ''")
    private String dialStatus;

   //getter setter
}

因此,如果用户要求所有“CallRecords”数据,我还必须提供“CallRecordOperators”,因为它们是相关的。

Mapper 和 DTO 的当前代码

@Mapper(unmappedTargetPolicy = ReportingPolicy.IGNORE)
public interface CallRecordsMapper {

    CallRecordsMapper INSTANCE = Mappers.getMapper(CallRecordsMapper.class);

    @Mapping(source="callRecordOperators",target = "operators")
    CallRecordsDto callRecordsToCallRecordsDto(CallRecords callRecords);

    public abstract CallRecordOperatorsDto toTarget(CallRecordOperators source);

    List<CallRecordsDto> callRecordsToCallRecordsDtos(List<CallRecords> callRecords);

}

public class CallRecordsDto {

    private String callerNumber;

    private List<CallRecordOperatorsDto> operators;

    //getter setters
}

public class CallRecordOperatorsDto {

    private String callRecordsId;

    private String operatorId;
    private String operatorName;

    private String currentTime;

   // getter setter

}

但是对于上面的代码,我得到了

{
    "callerNumber": "9898989898",
    "operators": [{
        "callRecordsId": null,
        "operatorId": null,
        "operatorName": null,
        "currentTime": null
    }, {
        "callRecordsId": null,
        "operatorId": null,
        "operatorName": null,
        "currentTime": null
    }]
}

运算符数组的值为空。他可能是什么问题?

4

3 回答 3

5

您似乎缺少从CallRecordOperatorsto的映射CallRecordOperatorsDto

@Mapper
public interface CallRecordsMapper {

    CallRecordsMapper INSTANCE = Mappers.getMapper(CallRecordsMapper.class);

    @Mapping(source="callRecordOperators",target = "operators")
    CallRecordsDto callRecordsToCallRecordsDto(CallRecords callRecords);

    @Mapping(target = "callRecordsId", source = "callrecord.id")
    @Mapping(target = "operatorId", source = "operator.id")
    @Mapping(target = "operatorName", source = "operator.name")
    @Mapping(target = "currentTime", source = "startTime")
    CallRecordOperatorsDto callRecordOperatorsToDto(CallRecordOperators source);
}
于 2017-01-03T11:15:49.800 回答
1

当您对元素进行 Hibernate 查询时,您可以使用不同的策略A获取集合的相关B元素。bs他们之中有一些是:

  1. 如果您使用 HQL 构建查询,则可以执行JOIN FETCHorLEFT JOIN FETCH来填充bs集合:

    String hql = "SELECT DISTINCT a FROM " + A.class.getName() 
        + " a LEFT JOIN FETCH a.bs WHERE ...";
    

    此查询将使用单个 SQL 查询加载所有数据。

  2. 使用急切获取bs集合,更改@OneToMany注释:

    @OneToMany(fetch=FetchType.EAGER)
    private List<B> bs;
    

    在这种情况下,当您运行A元素查询时,将启动 SQL 查询以检索A数据,并且对于结果中的每个A对象,将执行 SQL 查询以加载相应的bs集合。

  3. 如果您使用Criteria构建查询,您可以bs通过类似于 HQL 的方式更改集合的获取模式JOIN FETCH

    Criteria c = session.createCriteria(A.class);
    c.setFetchMode("bs", FetchMode.JOIN);
    c.setResultTransformer(Criteria.DISTINCT_ROOT_ENTITY);
    
于 2016-12-29T13:49:05.000 回答
-2

换一种稍微不同但性能更好的方法怎么样?通过使用Blaze-Persistence 实体视图,您可以直接在 DTO 类上定义您的映射,并将其应用到查询构建器上,以生成完全适合您的 DTO 结构的高效查询。

@EntityView(CallRecords.class)
public interface CallRecordsDto {
    // The id of the CallRecords entity
    @JsonIgnore
    @IdMapping("id") String getId();

    String getCallerNumber();

    @Mapping("callRecordOperators")
    List<CallRecordOperatorsDto> getOperators();
}

@EntityView(CallRecordOperators.class)
public interface CallRecordOperatorsDto {

    // The id of the CallRecordOperators entity
    @JsonIgnore
    @IdMapping("id") Long getId();

    @Mapping("callrecord.id")
    String getCallRecordId();

    @Mapping("operator.id")
    String getOperatorId();

    @Mapping("operator.name")
    String getOperatorName();

    @Mapping("startTime")
    String getCurrentTime();

    // Whatever properties you want
}

了解如何在 DTO 中映射实体属性?这是查询的代码

EntityManager entityManager = // jpa entity manager
CriteriaBuilderFactory cbf = // query builder factory from Blaze-Persistence
EntityViewManager evm = // manager that can apply entity views to query builders

CriteriaBuilder<User> builder = cbf.create(entityManager, CallRecords.class)
    .where("callerNumber").eq("123456789");
List<CallRecordsDto> result = evm.applySetting(
    builder, 
    EntityViewSetting.create(CallRecordsDto.class)
).getResultList();

请注意,这将大致生成以下优化查询

SELECT 
    c.id, 
    c.callerNumber, 
    o.callrecord.id, 
    o.id,
    o.startTime,
    op.id,
    op.name
FROM CallRecords c
LEFT JOIN c.callRecordOperators o
LEFT JOIN o.operator op
WHERE c.callerNumber = :param_1
于 2017-02-15T17:15:53.610 回答