Hibernate映射问题

问题  微软雅黑练习一:保存一个雇主a
练习二:保存一个雇员x,
练习三:已知雇主a的id和雇员x的id,让a和x产生雇佣关系
练习四:保存一个雇佣关系by,级联保存一个雇主b,和一个雇员y
练习五:保存一个雇主c,级联保存雇佣关系cx和cy
练习六:已知雇员x的id,查找他受聘于哪些雇主,雇用时间和薪水分别是多少
练习七:已知雇主c的id,查找他的雇员,打印雇员的详细姓名
练习八:解除雇主c和所有人的雇佣关系
test类测试代码如下:(如下代码为自己编写的思路,仅供参考)

package com.puckasoft.test;





import java.util.HashSet;
import java.util.Set;

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


import junit.framework.TestCase;

import com.puckasoft.po.Employee;
import com.puckasoft.po.Employer;
import com.puckasoft.po.Employment;
import com.puckasoft.po.Name;
import com.puckasoft.util.HibernateCallback;
import com.puckasoft.util.HibernateTemplate;

public class TestEmploy extends TestCase{
//练习一:保存一个雇主a
    public void testSaveEmployer() throws Exception {
        Employer employer = new Employer();
        employer.setName("a");
        HibernateTemplate.getInstance().save(employer);
    }
//练习二:保存一个雇员x,
    public void testSaveEmployee() throws Exception {
        Employee employee = new Employee();
        Name name = new Name("x", '.', "x");
        employee.setName(name);
        HibernateTemplate.getInstance().save(employee);
        
    }
    //练习三:已知雇主a的id和雇员x的id,让a和x产生雇佣关系
    public void testSaveEmployment() throws Exception {
        HibernateTemplate.getInstance().execute(new HibernateCallback() {
            
            @Override
            public Object doInHibernate(Session session) throws Exception {
                Transaction tx = null;
                try {
                    tx =  session.beginTransaction();
                    Employee employee = (Employee) session.load(Employee.class, 1l);
                    Employer employer = (Employer) session.load(Employer.class, 1l);
                    Employment employment = new Employment(null, null, null, employee, employer);
                    HibernateTemplate.getInstance().save(employment);
                    tx.commit();
                    
                } catch (Exception e) {
                    if(tx != null){
                        tx.rollback();
                    }
                }
                return null;
                
            }
        });
        
    }
//练习四:保存一个雇佣关系by,级联保存一个雇主b,和一个雇员y
    public void testSaveEmployment1() throws Exception {
        final Employer employer = new Employer("b", null);
        final Employee employee = new Employee();
        Name name = new Name("x", '.', "y");
        employee.setName(name);
        
        HibernateTemplate.getInstance().execute(new HibernateCallback() {
            
            @Override
            public Object doInHibernate(Session session) throws Exception {
                Transaction tx = null;
                try {
                    tx = session.beginTransaction();
                    session.save(employer);
                    session.save(employee);
                    Employment employment = new Employment(null, null, null, employee, employer);
                    session.save(employment);
                    tx.commit();
                } catch (Exception e) {
                    if(tx != null){
                        tx.rollback();
                    }
                }
                return null;
            }
        });
    }
//练习五:保存一个雇主c,级联保存雇佣关系cx和cy
    public void testSaveEmployment3() throws Exception {
        final Employer employer = new Employer("c",new HashSet<Employment>());
        HibernateTemplate.getInstance().execute(new HibernateCallback() {
            
            @Override
            public Object doInHibernate(Session session) throws Exception {
                Transaction tx = null;
                try {
                    tx = session.beginTransaction();
                    Employee employee1 = (Employee) session.load(Employee.class, 1l);
                    Employee employee2 = (Employee) session.load(Employee.class, 2l);
                    Employment employment1 = new Employment(null, null, null, employee1, employer);
                    Employment employment2 = new Employment(null, null, null, employee2, employer);
                    employer.getEmployment().add(employment1);
                    employer.getEmployment().add(employment2);
                    employment1.setEmployer(employer);
                    employment2.setEmployer(employer);
                    session.save(employer);
                    tx.commit();
                    
                } catch (Exception e) {
                if(tx != null){
                    tx.rollback();
                }
                }
                return null;
            }
        });
    }
    //练习六:已知雇员x的id,查找他受聘于哪些雇主,雇用时间和薪水分别是多少
    public void testGet1() throws Exception {
        HibernateTemplate.getInstance().execute(new HibernateCallback() {
            @Override 
            public Object doInHibernate(Session session) throws Exception {
                
                try {
                    Employee employee = (Employee) session.get(Employee.class, 1L);
                    Set<Employment> employment= employee.getEmployment();
                    for (Employment employment2 : employment) {
                        System.out.println("雇主为:"+employment2.getEmployer().getName()+ "雇佣薪水为: "+ "雇佣时间为:" + (employment2.getEndDate()));
                    }
                } catch (Exception e) {
                    e.printStackTrace();
                }
                return null;
            }
        });
        
    }
//练习七:已知雇主c的id,查找他的雇员,打印雇员的详细姓名
    public void testGet2() throws Exception {
        HibernateTemplate.getInstance().execute(new HibernateCallback() {
            
            @Override
            public Object doInHibernate(Session session) throws Exception {
            Employer employer = (Employer) session.get(Employer.class, 3L);
            Set<Employment> employment = employer.getEmployment();
            for (Employment employment2 : employment) {
                System.out.println(employment2.getEmployee().getName().getFirstName() + employment2.getEmployee().getName().getI_initial() + employment2.getEmployee().getName().getLastName());
            }
            
            
                return null;
            }
        });
    }
练习八:解除雇主c和所有人的雇佣关系
    public void testDeleteEmployment() throws Exception {
        HibernateTemplate.getInstance().execute(new HibernateCallback() {
            
            @Override
            public Object doInHibernate(Session session) throws Exception {
            Transaction tx = null;
            try {
                tx = session.beginTransaction();
                Employer employer = (Employer) session.get(Employer.class, 3L);
                Set<Employment> employment = employer.getEmployment();
                for (Employment employment2 : employment) {
                    session.delete(employment2);
                }
                employment.clear();
                tx.commit();
            } catch (Exception e) {
                if(tx != null){
                    tx.rollback();
                }
            }
                return null;
            }
        });
    }
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值