0
我有一個值對象,我想要映射我的本機查詢結果集。 我使用@SQLResultSetMapping
映射對象字段,但是當任何列的值是null
,例外的是拋出:SQLResultSetMapping在出現空列時拋出異常
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(可爲空)替換爲查詢中的靜態值,則映射完美無缺。實施有問題嗎?由於映射空值看起來像是一種常見的情況。
它的工作!現在我想到了,構造函數參數的類類型信息將需要通過反射來獲得適當的構造。可空列需要明確提及jpa的工作方式。畢竟這可能不是一個錯誤。 – grizzly