java - 如何编写实用程序类以便我可以使用 DTO 设计模式执行延迟加载?

标签 java hibernate lazy-loading dto

据我所知,DTO 用于将对象从一层传输到其他层。 在我的项目中,我使用 DTO 将对象从持久层传输到服务层。

这是我的实用程序类文件,它可以帮助我将持久性对象转换为 DTO。

属性实用程序

    package com.cac.util;

import java.util.HashSet;
import java.util.Iterator;
import java.util.Set;

import com.cac.hibernate.Answers;
import com.cac.hibernate.Members;
import com.cac.hibernate.Questions;
import com.cac.hibernate.Sections;
import com.cac.to.AnswersTO;
import com.cac.to.MembersTO;
import com.cac.to.QuestionsTO;
import com.cac.to.SectionsTO;
public class PropertyUtil {
    public static Sections getSectionsFromSectionsTO(SectionsTO sectionsto) {
        Sections sections=new Sections();
        sections.setSection_id(sectionsto.getSection_id());
        sections.setSection_name(sectionsto.getSection_name());
        sections.setSection_desc(sectionsto.getSection_desc());
        sections.setThreads(sectionsto.getThreads());
        sections.setPosts(sectionsto.getPosts());
        sections.setLast_post(sectionsto.getLast_post());
        sections.setLast_post_by(PropertyUtil.getMembersFromMembersTO(sectionsto.getLast_post_by()));
        sections.setQuestion_id(sectionsto.getQuestion_id());
        return sections;
    }
    public static SectionsTO getSectionsTOFromSections(Sections sections) {
        SectionsTO sectionsto=new SectionsTO();
        sectionsto.setSection_id(sections.getSection_id());
        sectionsto.setSection_name(sections.getSection_name());
        sectionsto.setSection_desc(sections.getSection_desc());
        sectionsto.setThreads(sections.getThreads());
        sectionsto.setPosts(sections.getPosts());
        sectionsto.setLast_post(sections.getLast_post());
        sectionsto.setLast_post_by(PropertyUtil.getMembersTOFromMembers(sections.getLast_post_by()));
        sectionsto.setQuestion_id(sections.getQuestion_id());
        Set<Questions> questions_set=sections.getQuestions_set();
        Set<QuestionsTO> questions_set_to=new HashSet<QuestionsTO>();
        Iterator<Questions> it=questions_set.iterator();
        while(it.hasNext()) {
            QuestionsTO questionsto=PropertyUtil.getQuestionsTOFromQuestions(it.next());
            questions_set_to.add(questionsto);
        }
        return sectionsto;
    }

    public static Members getMembersFromMembersTO(MembersTO membersto) {
        Members members=new Members();
        members.setMember_id(membersto.getMember_id());
        members.setUsername(membersto.getUsername());
        members.setPassword(membersto.getPassword());
        members.setEmail(membersto.getEmail());
        members.setGender(membersto.getGender());
        members.setBdate(membersto.getBdate());
        members.setImage_path(membersto.getImage_path());
        members.setAddress(membersto.getAddress());
        members.setBest_at(membersto.getBest_at());
        members.setPosition(membersto.getPosition());
        members.setJoin_date(membersto.getJoin_date());
        //Set<Members> memset=new HashSet<Members>();

        //members.setQuestions_set(membersto.getQuestionsto_set());
        //mem
        return members;
    }
    public static MembersTO getMembersTOFromMembers(Members members) {
        MembersTO membersto=new MembersTO();
        membersto.setMember_id(members.getMember_id());
        membersto.setUsername(members.getUsername());
        membersto.setPassword(members.getPassword());
        membersto.setEmail(members.getEmail());
        membersto.setGender(members.getGender());
        membersto.setBdate(members.getBdate());
        membersto.setImage_path(members.getImage_path());
        membersto.setAddress(members.getAddress());
        membersto.setBest_at(members.getBest_at());
        membersto.setPosition(members.getPosition());
        membersto.setJoin_date(members.getJoin_date());
        Set<Questions> questions_set=members.getQuestions_set();
        Set<QuestionsTO> questions_set_to=new HashSet<QuestionsTO>();
        Iterator<Questions> it=questions_set.iterator();
        while(it.hasNext()) {
            QuestionsTO questionsto=PropertyUtil.getQuestionsTOFromQuestions(it.next());
            questions_set_to.add(questionsto);
        }
        Set<Answers> answers_set=members.getAnswers_set();
        Set<AnswersTO> answers_set_to=new HashSet<AnswersTO>();
        Iterator<Answers> answers_it=answers_set.iterator();
        while(answers_it.hasNext()) {
            AnswersTO answersto=PropertyUtil.getAnswersTOFromAnswers(answers_it.next());
            answers_set_to.add(answersto);
        }
        //members.setQuestions_set(membersto.getQuestionsto_set());
        //mem
        return membersto;
    }
    public static QuestionsTO getQuestionsTOFromQuestions(Questions questions) {
        QuestionsTO questionsto=new QuestionsTO();
        questionsto.setQuestion_id(questions.getQuestion_id());
        questionsto.setQuestion_title(questions.getQuestion_title());
        questionsto.setQuestion_desc(questions.getQuestion_desc());
        questionsto.setQ_post_date(questions.getQ_post_date());
        questionsto.setReplies(questions.getReplies());
        questionsto.setViews(questions.getViews());
        questionsto.setLast_post_by(questions.getLast_post_by());
        questionsto.setLast_post_date(questions.getLast_post_date());

        Set<Answers> answers_set=questions.getAnswers_set();
        Set<AnswersTO> answers_set_to=new HashSet<AnswersTO>();
        Iterator<Answers> answers_it=answers_set.iterator();
        while(answers_it.hasNext()) {
            AnswersTO answersto=PropertyUtil.getAnswersTOFromAnswers(answers_it.next());
            answers_set_to.add(answersto);
        }
        return questionsto;
    }
    public static Questions getQuestionsFromQuestionsTO(QuestionsTO questionsto) {
        Questions questions=new Questions();
        questions.setQuestion_id(questionsto.getQuestion_id());
        questions.setQuestion_title(questionsto.getQuestion_title());
        questions.setQuestion_desc(questionsto.getQuestion_desc());
        questions.setQ_post_date(questionsto.getQ_post_date());
        questions.setReplies(questionsto.getReplies());
        questions.setViews(questionsto.getViews());
        questions.setLast_post_by(questionsto.getLast_post_by());
        questions.setLast_post_date(questionsto.getLast_post_date());

        return questions;
    }

    public static AnswersTO getAnswersTOFromAnswers(Answers answers) {
        AnswersTO answersto=new AnswersTO();
        answersto.setAnswer_id(answers.getAnswer_id());
        answersto.setAnswer_desc(answers.getAnswer_desc());
        answersto.setA_post_date(answers.getA_post_date());

        return answersto;
    }
    public static Answers getAnswersFromAnswersTO(AnswersTO answersto) {
        Answers answers=new Answers();
        answers.setAnswer_id(answersto.getAnswer_id());
        answers.setAnswer_desc(answersto.getAnswer_desc());
        answers.setA_post_date(answersto.getA_post_date());

        return answers;
    }
}

问题是,每当我必须执行延迟加载时,但是当我使用 getSectionsTOFromSection() 时,它将加载所有持久性类。 。 那么如何编写这个 PropertyUtil 类以便我可以同时使用延迟加载和 DTO 设计模式?

最佳答案

延迟获取策略可让您决定在第一次数据库命中时应加载多少对象图以及应延迟加载哪些关联。而你使用DTO将对象从持久层转移到服务层。这是两个完全不同的概念。一种是从DB加载数据,另一种是在不同层之间传输数据。

现在,对于延迟加载,推荐的方法是在映射文件中配置延迟获取的所有关联(使用 lazy="true" ),然后可以在运行时覆盖这些关联(无论何时需要急切获取)。

让我们考虑这两种情况:

  • 可能需要获取Sections的列表。 (但您不希望加载各个 associations 对象的 Sections):

可以使用以下方式获取此列表

session.createQuery("from Sections sections");

其中Sections存在于返回的list中不会包含associations .

  • 您可能再次想要获取 Sections 的列表与急切的联想。这可以通过使用 fetch 的外连接来实现from 中的关键字子句(覆盖默认的获取行为):

session.createQuery("from Sections sections left join fetch sections.association-name");

执行时,这将返回一个Section实例列表,其关联已完全初始化。

现在,一旦加载了数据,您就可以使用 DTO 将数据传输到服务层。

关于java - 如何编写实用程序类以便我可以使用 DTO 设计模式执行延迟加载?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18680353/

相关文章:

java - 将嵌入式 HSQL 数据库保存到文件

java - Mockito 非 stub 函数

javac : invalid target release: 1. 8

hibernate - 这个错误 java.lang.IllegalStateException : Trying to return an unknown connection2? 的原因是什么

Angular 2延迟加载导致 View 更改之间的延迟

c# - Entity Framework Core 5 关闭加载NavigationProperties

c# - Lazy<T> 延迟加载错误 : A field initializer cannot reference the non-static field, 方法或属性

java - 通过快捷方式启动可执行 jar 时出现错误的目录

java - Android Java 将 long 强制转换为枚举问题

java - Hibernate @事务 session