在执行save方法时候,如何保存未被持久化的对象。代码如下:
public ClassRoom {
int id
String name
} public Student{
int id
String name
ClassRoom classRoom
//这部中的studnet对象是通过struts2创建的其中studnet.classRoom.id也是通过页面传过来的
此时student对象的内部值是{
student.id=null;
studnet.name= "name1 ";
student.classRoom.id=1;
student.classRoom.name=null;
}
/*
student.setClassRoom(studentDao.get(ClassRoom.class,Studnet.getClassRoom.getId()));
出了这中方式还有其他方式吗?
这种方式每次都要查询数据库一次感觉很不好。而且如果相关联的对象多的话每个对象都得查一下(为了持久化) 岂不是很麻烦
*/
studentDao.save(Student);
save时如果对象不是持久化的会报错,我执行了一次查询就是为了让他变成持久化的,但是这样有点麻烦。
希望大虾们能详细说下解决办法,小弟不甚感激!
解决方案 »
免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货