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