I'm facing a problem while trying to map a DTO
using TypedQuery
. Despite trying several approaches, the mapping doesn't seem to work as expected. I have ensured that the parameters are correctly set, and the relationship with the child table is appropriately defined.
I need assistance in identifying the mistakes in my code. Please review the following changes and point out any issues that might be causing the problem.
My Service class:
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;
}
}
OrganizationDTO class:
@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();
}
Tried all possible solutions but still facing this issue:
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/:?]
I see no error in your code or configurations. Have you verified your
data
fornull values
? Try checking your record if it contains null values for the below mentioned keys. If it does, I will suggest that you useNon-Primitive
data types in your DTO. Since you've usedprimitive data
types and they don't acceptnull values
.Use
Boolean
instead ofboolean
andInteger
instead ofint
. Hopefully this should resolve your issue.