gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-02 03:07:34 27 4
gpt4 key购买 nike

我有一个值对象,我想为其映射我的 native 查询结果集。我正在使用 @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/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com