HibernateMap问题:无法使用TypedQueryMapDTO

093gszye  于 2023-10-23  发布在  其他
关注(0)|答案(2)|浏览(165)

我在尝试使用TypedQueryMapDTO时遇到了一个问题。尽管尝试了几种方法,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/:?]
nqwrtyyt

nqwrtyyt1#

我在你的代码或配置中没有看到任何错误。您是否已验证您的data是否适用于null values?尝试检查您的记录,如果它包含空值为下面提到的键。如果是这样,我建议您在DTO中使用Non-Primitive数据类型。因为你已经使用了primitive data类型,而它们不接受null values

private Boolean portalAccess;
    private Integer  systemActive;
    private Integer  isActive;

使用Boolean代替boolean,使用Integer代替int。希望这能解决你的问题。

p1iqtdky

p1iqtdky2#

我试图复制你的问题,但我的项目成功执行。我相信在你的配置中有一些错误,你可以将它们与我的示例进行比较,并检查你的项目中是否有错误。

build.gradle

plugins {
    id 'java'
    id 'org.springframework.boot' version '3.1.3'
    id 'io.spring.dependency-management' version '1.1.3'
}

group = 'com.example'
version = '0.0.1-SNAPSHOT'

java {
    sourceCompatibility = '17'
}

repositories {
    mavenCentral()
}

dependencies {
    implementation 'org.springframework.boot:spring-boot-starter-data-jpa'
    implementation  'org.projectlombok:lombok:1.18.28'
    testImplementation 'org.springframework.boot:spring-boot-starter-test'
    implementation 'com.h2database:h2'
    annotationProcessor 'org.projectlombok:lombok:1.18.20'
}

tasks.named('test') {
    useJUnitPlatform()
}

联系DTO

package com.example.typeddemo;

import jakarta.persistence.Column;
import jakarta.persistence.Entity;
import jakarta.persistence.GeneratedValue;
import jakarta.persistence.GenerationType;
import jakarta.persistence.Id;
import lombok.Getter;
import lombok.Setter;

@Entity
@Getter
@Setter
public class ContactDTO {

    @Id
    @GeneratedValue(strategy = GenerationType.SEQUENCE)
    @Column(name = "id", nullable = false)
    private Long id;

}

组织机构

package com.example.typeddemo;

import jakarta.persistence.Column;
import jakarta.persistence.Entity;
import jakarta.persistence.GeneratedValue;
import jakarta.persistence.GenerationType;
import jakarta.persistence.Id;
import jakarta.persistence.JoinColumn;
import jakarta.persistence.ManyToOne;
import lombok.Getter;
import lombok.Setter;
import lombok.ToString;

@Entity
@Getter
@Setter
@ToString
public class Organization {

    @Getter
    @Id
    @GeneratedValue(strategy = GenerationType.SEQUENCE)
    @Column(name = "id", nullable = false)
    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;
    @ManyToOne
    @JoinColumn(name = "parent_id")
    private Organization parent;

}

隐私政策

package com.example.typeddemo;

import jakarta.persistence.Column;
import jakarta.persistence.Entity;
import jakarta.persistence.GeneratedValue;
import jakarta.persistence.GenerationType;
import jakarta.persistence.Id;
import jakarta.persistence.JoinColumn;
import jakarta.persistence.OneToOne;
import jakarta.persistence.Transient;
import lombok.Getter;
import lombok.Setter;
import lombok.ToString;

@Setter
@Getter
@Entity
@ToString
public class OrganizationDetailDTO {
    @Column(name = "organization_id")
    @Id
    @GeneratedValue(strategy = GenerationType.SEQUENCE)
    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() {

    }
    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;

    }

    @OneToOne
    @JoinColumn(name = "admin_info_id")
    @Transient
    private ContactDTO adminInfo = new ContactDTO();

}

咨询服务

package com.example.typeddemo;

import jakarta.persistence.EntityManager;
import jakarta.persistence.NoResultException;
import jakarta.persistence.TypedQuery;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service
public class OrganizationService {

    Logger LOGGER = LoggerFactory.getLogger(OrganizationService.class);

    @Autowired
    EntityManager entityManager;

    public OrganizationDetailDTO getOrganizationDetailForAgent(Long anOrganizationId)
            throws Exception {
        // Create DTO and necessary objects
        OrganizationDetailDTO organizationDTO;
        // 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");
        }

        return organizationDTO;
    }

    public <T> T findOrganizationDetailDTO(Class<T> organizationDetailDTOClass, Long id) {
        String queryString =
                "SELECT NEW com.example.typeddemo.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;
        }
    }
}

存储库组织

package com.example.typeddemo;

import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.stereotype.Repository;

@Repository
public interface RepositoryOrganization extends JpaRepository<Organization, Long> {

}

仓库组织详细信息

package com.example.typeddemo;

import org.springframework.data.jpa.repository.JpaRepository;

@org.springframework.stereotype.Repository
public interface RepositoryOrgDetail extends JpaRepository<OrganizationDetailDTO, Long> {

}

TypedDemoApplication

package com.example.typeddemo;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

@SpringBootApplication
public class TypedDemoApplication {

    public static void main(String[] args) {
        SpringApplication.run(TypedDemoApplication.class, args);
    }

}

TypedDemoApplicationTests

package com.example.typeddemo;

import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;

@SpringBootTest
class TypedDemoApplicationTests {

    @Autowired
    OrganizationService organizationService;
    @Autowired
    RepositoryOrgDetail repositoryOrgDetail;
    @Autowired
    RepositoryOrganization repositoryOrganization;

    @Test
    void contextLoads() throws Exception {

        Organization parent = new Organization();
        repositoryOrganization.saveAndFlush(parent);
        OrganizationDetailDTO organisation = new OrganizationDetailDTO(parent.getId(),
                "squad",
                "sq1",
                "credit",
                Double.valueOf(20),
                "term",
                Double.valueOf(10),
                "vat",
                false,
                10L,
                "account",
                10L,
                "log",
                10L,
                "esc",
                false,
                10,
                parent.getId());
        repositoryOrgDetail.saveAndFlush(organisation);

        OrganizationDetailDTO organizationDetailForAgent = organizationService.getOrganizationDetailForAgent(parent.getId());
        Assertions.assertNotNull(organizationDetailForAgent);
    }

}

我希望这会有所帮助。祝你好运!

相关问题