⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 testo2m.java

📁 Hibernate中的映射例子
💻 JAVA
字号:
package test.o2m.demo;

import org.hibernate.Session;
import org.hibernate.Transaction;

import test.o2m.pojo.Jd;
import test.o2m.pojo.Qx;
import test.o2m.sf.HibernateSessionFactory;

public class TestO2M<T> {

	/**
	 * @param args
	 */
	public static void main(String[] args) {
		// TODO Auto-generated method stub
		//添加数据
//		Qx qx=new Qx();
//		qx.setQxid(2);
//		qx.setQx("山南区");
//		new TestO2M().add(qx);
//		
//		Jd jd1=new Jd();
//		jd1.setJdid(7);
//		jd1.setJd("和平路");
//		jd1.setQx(qx);
//		Jd jd2=new Jd();
//		jd2.setJdid(8);
//		jd2.setJd("八一路");
//		jd2.setQx(qx);
//		Jd jd3=new Jd();
//		jd3.setJdid(9);
//		jd3.setQx(qx);
//		jd3.setJd("五四大道");
//		TestO2M test=new TestO2M();
//		test.add(jd1);
//		test.add(jd2);
//		test.add(jd3);
//		many一方删除数据
//		Session session=HibernateSessionFactory.getSession();
//		Transaction tx=session.beginTransaction();
//		Jd jd=(Jd)session.get(Jd.class, 14);
//		session.delete(jd);
//		tx.commit();
//		HibernateSessionFactory.closeSession();
//		System.out.println(jd.getJd()+"be deleted.");
		//one一方删除数据
		Session session=HibernateSessionFactory.getSession();
		Transaction tx=session.beginTransaction();
		Qx qx=(Qx)session.get(Qx.class, 2);
		session.delete(qx);
		tx.commit();
		HibernateSessionFactory.closeSession();
		System.out.println(qx.getQx()+"be deleted.");
		//修改关联关系
//		Session session=HibernateSessionFactory.getSession();
//		Transaction tx=session.beginTransaction();
//		Jd hepl=(Jd)session.get(Jd.class, 7);
//		Qx shannq=(Qx)session.get(Qx.class, 2);
//		Qx haidq=(Qx)session.get(Qx.class, 1);
//		shannq.getJds().remove(hepl);
//		haidq.getJds().add(hepl);
//		hepl.setQx(haidq);
//		session.update(hepl);
//		tx.commit();
//		HibernateSessionFactory.closeSession();
	}
	public void add(T t){
		Session session;
		Transaction tx;
		session=HibernateSessionFactory.getSession();
		tx=session.beginTransaction();
		try{
			session.save(t);
			tx.commit();
		}catch(Exception e){
			tx.rollback();
			e.printStackTrace();
		}finally{
			session.close();
		}
	}


}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -