Hibernate中用逆轉(zhuǎn)工程實(shí)現(xiàn)多對(duì)多關(guān)系映射
據(jù)庫.jpg)
history中的c_id與s_id分別是外鍵。
在hibernate中用myeclicpse逆轉(zhuǎn)工程實(shí)現(xiàn)的多對(duì)多

將 CourseDAO 中的save方法 改成:
public void save(Course transientInstance) {
log.debug("saving Course instance");
Session session=null;
Transaction tx=null;
try {
session=getSession();
tx=session.beginTransaction();
session.save(transientInstance);
tx.commit();
log.debug("save successful");
} catch (RuntimeException re) {
log.error("save failed", re);
if(tx!=null){
tx.rollback();
}
throw re;
}
}
將 Student DAO 中的save方法 改成:
public void save(Student transientInstance) {
log.debug("saving Student instance");
Session session=null;
Transaction tx=null;
try {
session=getSession();
tx=session.beginTransaction();
session.save(transientInstance);
log.debug("save successful");
} catch (RuntimeException re) {
log.error("save failed", re);
if(tx!=null){
tx.rollback();
}
throw re;
}
}
將 HistoryDAO 中的save方法 改成:
public void save(History transientInstance) {
log.debug("saving History instance");
Session session=null;
Transaction tx=null;
try {
session=getSession();
tx=session.beginTransaction();
session.save(transientInstance);
tx.commit();
log.debug("save successful");
} catch (RuntimeException re) {
log.error("save failed", re);
if(tx!=null){
tx.rollback();
}
throw re;
}
}
測(cè)試類如下:
public class Test {
public static void main(String[] args) {
Course course=new Course();
CourseDAO cdao=new CourseDAO();
Student stu=new Student();
StudentDAO sdao=new StudentDAO();
History his=new History();
HistoryId hid=new HistoryId();
HistoryDAO hdao=new HistoryDAO();
course.setCName("c++");
course.setCTer("張三");
stu.setSName("張同學(xué)");
stu.setSAge(22);
hid.setCourse(course);
hid.setStudent(stu);
his.setId(hid);
cdao.save(course);
sdao.save(stu);
hdao.save(his);
}
}
posted on 2012-05-13 18:04 何云隆 閱讀(249) 評(píng)論(0) 編輯 收藏 所屬分類: Hibernate