Hibernate 一对多、多对一、级联、加载、反转

  • X0_545462
    了解作者
  • 6.4MB
    文件大小
  • rar
    文件格式
  • 0
    收藏次数
  • VIP专享
    资源类型
  • 0
    下载次数
  • 2022-06-14 23:27
    上传日期
Hibernate 一对多、多对一、级联、加载、反转
Hibernate_01_2.rar
内容介绍
package com.zuxia.yc42.test; import java.util.HashSet; import java.util.Set; import org.hibernate.Session; import org.hibernate.SessionFactory; import org.hibernate.Transaction; import org.hibernate.cfg.Configuration; import com.zuxia.yc42.po.District; import com.zuxia.yc42.po.Street; public class Main { /** * @param args */ public static void main(String[] args) { //1.加载配置文件 Configuration cfg = new Configuration().configure("/hibernate.cfg.xml"); //2.创建SessionFactory类 SessionFactory factory = cfg.buildSessionFactory(); //3.通过SessionFactory类获取Session对象 Session session = factory.openSession(); //4.通过Session对象打开事务 Transaction tx = session.beginTransaction(); //5.通过Session对象操作数据表 // UserInfo usInfo = (UserInfo) session.get(UserInfo.class, 1); // System.out.println(usInfo.getUsName()); // UserInfo usInfo = new UserInfo(); // usInfo.setUsName("admin"); // usInfo.setUsSex("1"); // session.save(usInfo); // Street street = (Street)session.get(Street.class, 1); // System.out.println(street.getStId()+"\t"+street.getStName());//+"\t"+street.getDistrict().getDsName()); // District dis = (District)session.get(District.class, 1); // System.out.println(dis.getDsName()); // // Set<Street> streets = dis.getStreets(); // for(Street st:streets) // { // System.out.print(st.getStName()+"\t"); // } // // try { // Thread.sleep(2000); // } catch (InterruptedException e) { // // TODO Auto-generated catch block // e.printStackTrace(); // } // // System.out.println(street.getDistrict().getDsName()); District district = new District(); district.setDsName("高新区21"); Set<Street> streets = new HashSet<Street>(); Street st1 = new Street(); st1.setStName("石桥铺街道21"); st1.setDistrict(district); Street st2 = new Street(); st2.setStName("石油路21"); st2.setDistrict(district); streets.add(st1); streets.add(st2); district.setStreets(streets); session.save(district); // District district = (District)session.get(District.class,22); // // session.delete(district); //6.提交事务 tx.commit(); //7.关闭Session session.close(); } }
评论