我有一个要映射其本机查询结果集的值对象。
我正在使用@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(可为空)替换为查询中的静态值,则映射可以正常工作。实施有问题吗?由于映射空值似乎是一种常见情况。
最佳答案
这是作为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
}
关于java - 如果列为空,SQLResultSetMapping将引发异常,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/41549440/