public class SaveDataDTO {
public static void main(String[] args) {
// Film Objects
FilmDTO fdto1 = SpringGetBean.getFilmDTOBean();
fdto1.setfName("Avengers");
FilmDTO fdto2 = SpringGetBean.getFilmDTOBean();
fdto2.setfName("Exodus Gods And Kings");
FilmDTO fdto3 = SpringGetBean.getFilmDTOBean();
fdto3.setfName("Fast And The Furious 7");
// Genre Objects
GenreDTO gdto1 = SpringGetBean.getGenreDTOBan();
gdto1.setgName("Action");
GenreDTO gdto2 = SpringGetBean.getGenreDTOBan();
gdto2.setgName("Science Fiction");
GenreDTO gdto3 = SpringGetBean.getGenreDTOBan();
gdto3.setgName("Fiction");
// Adding The Films To List
List<FilmDTO> flaef = new ArrayList<FilmDTO>();
flaef.add(fdto2);
List<FilmDTO> flae = new ArrayList<FilmDTO>();
flae.add(fdto1);
List<FilmDTO> flf = new ArrayList<FilmDTO>();
flf.add(fdto3);
// Adding The Genre To List
List<GenreDTO> glasf = new ArrayList<GenreDTO>();
glasf.add(gdto1);
glasf.add(gdto2);
glasf.add(gdto3);
List<GenreDTO> glas = new ArrayList<GenreDTO>();
glas.add(gdto1);
glas.add(gdto2);
List<GenreDTO> glf = new ArrayList<GenreDTO>();
glf.add(gdto3);
// Association Of Objects In Both Directions
fdto1.setGenres(glas);
fdto2.setGenres(glasf);
fdto3.setGenres(glf);
gdto1.setFilms(flae);
gdto2.setFilms(flae);
gdto3.setFilms(flf);
// Hibernate Methods
Session session = MyHibernateSession.getSession();
Transaction tx = session.beginTransaction();
try {
session.save(fdto1);
session.save(fdto2);
session.save(fdto3);
tx.commit();
} catch (HibernateException e) {
e.printStackTrace();
} finally {
session.close();
}
}
}
这工作正常,但如何动态地制作物品,实际上每件事都在这里编码,我也希望根据用户选择映射事物。所以如果任何人都可以帮助我在这个代码
答案 0 :(得分:0)
我为你重构了代码:)
public static void main(String[] args) {
FilmDTO fdto1 = SpringGetBean.getFilmDTOBean();
fdto1 .setGenres(addGenre("Action", "Science Fiction"));
FilmDTO fdto2 = SpringGetBean.getFilmDTOBean();
fdto2.setGenres(addGenre("Action", "Science Fiction", "Fiction"));
FilmDTO fdto3 = SpringGetBean.getFilmDTOBean();
fdto3.setGenres(addGenre("Fiction"));
GenreDTO gdto1 = SpringGetBean.getGenreDTOBan();
gdto1.setFilms(addFilm("Avengers"));
GenreDTO gdto2 = SpringGetBean.getGenreDTOBan();
gdto2.setFilms(addFilm("Exodus Gods And Kings"));
GenreDTO gdto3 = SpringGetBean.getGenreDTOBan();
gdto3.setFilms(addFilm("Fast And The Furious 7"));
// Hibernate Methods
Session session = MyHibernateSession.getSession();
Transaction tx = session.beginTransaction();
try {
session.save(fdto1);
session.save(fdto2);
session.save(fdto3);
tx.commit();
} catch (HibernateException e) {
e.printStackTrace();
} finally {
session.close();
}
}
private static List<GenreDTO> addGenre(String... genre) {
List<GenreDTO> list = new ArrayList<GenreDTO>();
for (String gn : genre) {
GenreDTO g = SpringGetBean.getGenreDTOBan();
g.setgName(gn);
list.add(g);
}
return list;
}
private static List<FilmDTO> addFilm(String film) {
List<FilmDTO> filmList = new ArrayList<FilmDTO>();
FilmDTO f = SpringGetBean.getFilmDTOBean();
f.setfName(film);
return filmList;
}
当然我们可以进一步重构这一点,但这看起来更接近你的逻辑。现在用户只需指定电影名称或其类型,您可以将它们传递到您的代码中以使其动态
<强>更新强>
你可以进一步制作这样的动态电影对象,在这里你只需要传递电影名称列表
List<String> films = Arrays.asList("Avengers", "Exodus Gods And Kings", "Fast And The Furious 7");
// Hibernate Methods
Session session = MyHibernateSession.getSession();
Transaction tx = session.beginTransaction();
try {
for (String film : films) {
GenreDTO g = SpringGetBean.getGenreDTOBan();
g.setFilms(addFilm(film));
session.save(g);
}
} catch (HibernateException e) {
e.printStackTrace();
} finally {
session.close();
}