我在尝试使用TypedQuery
MapDTO
时遇到了一个问题。尽管尝试了几种方法,Map似乎并不像预期的那样工作。我已经确保参数设置正确,并且与子表的关系定义正确。
我需要帮助来识别代码中的错误。请查看以下更改并指出可能导致此问题的任何问题。
我的服务类别:
public OrganizationDetailDTO getOrganizationDetailForAgent(Long anOrganizationId) throws ApplicationException, AccessDeniedException {
// Create DTO and necessary objects
OrganizationDetailDTO organizationDTO;
try {
// Log request information
LOGGER.info("#2 Get OrganizationByIdWithParent request received in Organization Service with OrganizationId: " + anOrganizationId);
organizationDTO = this.findOrganizationDetailDTO(OrganizationDetailDTO.class, anOrganizationId);
// Retrieve organization by ID
// Check if organization exists
if (organizationDTO == null) {
LOGGER.info("No Organization Found Against Id " + anOrganizationId);
throw new Exception("No Organization Found Against Id");
}
}
public <T> T findOrganizationDetailDTO(Class<T> organizationDetailDTOClass, Long id) {
String queryString = "SELECT NEW com.alsharqi.org.organization.OrganizationDetailDTO(o.id, o.squad, o.squadId, o.creditHoldOverride, o.creditLimit, o.creditTermName, " +
"o.creditTerm, o.vatRegNumber, o.isActive, o.accountManagerUUID, o.accountManagerName, o.logisticManagerUUID, o.logisticManagerName, o.escalationManagerUUID, " +
"o.escalationManagerName,o.portalAccess,o.systemActive,o.parent.id) FROM Organization o WHERE o.id = :id";
TypedQuery<T> query = entityManager.createQuery(queryString, organizationDetailDTOClass);
query.setParameter("id", id);
try {
return query.getSingleResult();
} catch (NoResultException e) {
return null;
}
}
安装DTO类:
@Setter
@Getter
@NoArgsConstructor
public class OrganizationDetailDTO {
@Column(name = "organization_id")
private Long id;
private String squad;
private String squadId;
private String creditHoldOverride;
private Double creditLimit;
private String creditTermName;
private Double creditTerm;
private String vatRegNumber;
private boolean isActive;
private Long accountManagerUUID;
private String accountManagerName;
private Long logisticManagerUUID;
private String logisticManagerName;
private Long escalationManagerUUID;
private String escalationManagerName;
private boolean portalAccess;
private int systemActive;
private Long parent;
public OrganizationDetailDTO(Long id, String squad, String squadId, String creditHoldOverride, Double creditLimit, String creditTermName, Double creditTerm,
String vatRegNumber, boolean isActive, Long accountManagerUUID, String accountManagerName, Long logisticManagerUUID,
String logisticManagerName, Long escalationManagerUUID, String escalationManagerName,boolean portalAccess,Integer systemActive,Long parent) {
this.id = id;
this.squad = squad;
this.squadId = squadId;
this.creditHoldOverride = creditHoldOverride;
this.creditLimit = creditLimit;
this.creditTermName = creditTermName;
this.creditTerm = creditTerm;
this.vatRegNumber = vatRegNumber;
this.isActive = isActive;
this.accountManagerUUID = accountManagerUUID;
this.accountManagerName = accountManagerName;
this.logisticManagerUUID = logisticManagerUUID;
this.logisticManagerName = logisticManagerName;
this.escalationManagerUUID = escalationManagerUUID;
this.escalationManagerName = escalationManagerName;
this.portalAccess=portalAccess;
this.systemActive=systemActive;
this.parent=parent;
}
private ContactDTO adminInfo = new ContactDTO();
}
实体类别:
尝试了所有可能的解决方案,但仍然面临这个问题:
com.hp.org.util.ApplicationException: org.hibernate.QueryException: could not instantiate class [com.hp.org.organization.OrganizationDetailDTO] from tuple
at com.hp.org.api.OrganizationService.getOrganizationDetailForAgent(OrganizationService.java:3347) ~[main/:?]
at com.hp.org.api.OrganizationService$$FastClassBySpringCGLIB$$59b45cac.invoke(<generated>) ~[main/:?]
at org.springframework.cglib.proxy.MethodProxy.invoke(MethodProxy.java:204) ~[spring-core-5.0.13.RELEASE.jar:5.0.13.RELEASE]
at org.springframework.aop.framework.CglibAopProxy$CglibMethodInvocation.invokeJoinpoint(CglibAopProxy.java:746) ~[spring-aop-5.0.13.RELEASE.jar:5.0.13.RELEASE]
at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:163) ~[spring-aop-5.0.13.RELEASE.jar:5.0.13.RELEASE]
at org.springframework.transaction.interceptor.TransactionAspectSupport.invokeWithinTransaction(TransactionAspectSupport.java:294) ~[spring-tx-5.0.13.RELEASE.jar:5.0.13.RELEASE]
at org.springframework.transaction.interceptor.TransactionInterceptor.invoke(TransactionInterceptor.java:98) ~[spring-tx-5.0.13.RELEASE.jar:5.0.13.RELEASE]
at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:185) ~[spring-aop-5.0.13.RELEASE.jar:5.0.13.RELEASE]
at org.springframework.aop.framework.CglibAopProxy$DynamicAdvisedInterceptor.intercept(CglibAopProxy.java:688) ~[spring-aop-5.0.13.RELEASE.jar:5.0.13.RELEASE]
at com.hp.org.api.OrganizationService$$EnhancerBySpringCGLIB$$c94a2754.getOrganizationDetailForAgent(<generated>) ~[main/:?]
2条答案
按热度按时间nqwrtyyt1#
我在你的代码或配置中没有看到任何错误。您是否已验证您的
data
是否适用于null values
?尝试检查您的记录,如果它包含空值为下面提到的键。如果是这样,我建议您在DTO中使用Non-Primitive
数据类型。因为你已经使用了primitive data
类型,而它们不接受null values
。使用
Boolean
代替boolean
,使用Integer
代替int
。希望这能解决你的问题。p1iqtdky2#
我试图复制你的问题,但我的项目成功执行。我相信在你的配置中有一些错误,你可以将它们与我的示例进行比较,并检查你的项目中是否有错误。
build.gradle
联系DTO
组织机构
隐私政策
咨询服务
存储库组织
仓库组织详细信息
TypedDemoApplication
TypedDemoApplicationTests
我希望这会有所帮助。祝你好运!