0

据我所知,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 设计模式呢?

4

1 回答 1

1

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

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

让我们考虑这两种情况:

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

这个列表可以使用

session.createQuery("from Sections sections");

返回中的Sectionspresentlist将不包含associations.

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

session.createQuery("从Sections中的sections左加入获取sections.association-name");

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

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

于 2013-09-08T06:05:57.700 回答