在空列的情况下,SQLResultSetMapping抛出异常

时间:2017-01-09 13:44:14

标签: java eclipselink jpa-2.1

我有一个值对象,我想要映射我的原生查询结果集。 我正在使用@SQLResultSetMapping来映射对象字段,但是当任何列的值为null时,抛出异常:

Exception [EclipseLink-6177] (Eclipse Persistence Services - 2.6.1.v20150916-55dc7c3): org.eclipse.persistence.exceptions.QueryException
Exception Description: The column result [custom_properties] was not found in the results of the query.

我的实体类和映射

@Entity
@Multitenant(MultitenantType.TABLE_PER_TENANT)
@Table(name = "account_master")
@SqlResultSetMapping(name = "DTO_MAPPING", classes = @ConstructorResult(
    targetClass = AccountDTO.class,
    columns = {@ColumnResult(name = "id"),
        @ColumnResult(name = "name"),
        @ColumnResult(name = "custom_properties")
    })
)
public class Account implements Serializable {
// fields, setters and getters

}

价值对象:

public class AccountDTO {

  public AssetDTO(){
  }

  public AssetDTO(int id, String name, String customProperties) {
    this.id = id;
    this.name = name;
    this.customProperties = customProperties;
  }

}

执行声明

List<AccountDTO> accList = entityManager.createNativeQuery("SELECT id, name, custom_properties FROM account_master WHERE acc_hierarchy <@ 2.3", "DTO_MAPPING").getResultList()

如果查询中的静态值替换了custom_properties(可以为空),则映射完全正常。实施有什么问题吗?由于映射空值似乎是一种常见的情况。

1 个答案:

答案 0 :(得分:1)

这是作为EclipseLink中的错误提交的:https://bugs.eclipse.org/bugs/show_bug.cgi?id=484276

作为解决方法,您可以在

中指定类型
  

@ColumnResult

注释

因此,您应该将代码更改为:

@Entity
@Multitenant(MultitenantType.TABLE_PER_TENANT)
@Table(name = "account_master")
@SqlResultSetMapping(name = "DTO_MAPPING", classes = @ConstructorResult(
    targetClass = AccountDTO.class,
    columns = {@ColumnResult(name = "id"),
        @ColumnResult(name = "name"),
        @ColumnResult(name = "custom_properties", type=String.class)
    })
)
public class Account implements Serializable {
// fields, setters and getters

}