<hibernate-mapping ><class name="mypack.Company" table="COMPANIES" ><id name="id" type="long" column="ID"><generator class="increment"/></id><PRoperty name="name" type="string" column="NAME" /></class></hibernate-mapping>
<hibernate-mapping > <class name="mypack.HourlyEmployee" table="HOURLY_EMPLOYEES"> <id name="id" type="long" column="ID"> <generator class="increment"/> </id> <property name="name" type="string" column="NAME" /> <property name="rate" column="RATE" type="double" /> <many-to-one name="company" column="COMPANY_ID" class="mypack.Company" /> </class> </hibernate-mapping>
SalariedEmployee.hbm.xml文件用于把SalariedEmployee类映射到SE表,在这个映射文件中,除了需要映射SalariedEmployee类本身的salary属性,还需要映射从Employee类中继承的name属性,此外还要映射从Employee类中继承的与Company类的关联关系。例程14-3是SalariedEmployee.hbm.xml文件的代码。
例程14-3 SalariedEmployee.hbm.xml
由于Employee类没有相应的映射文件,因此在初始化Hibernate时,只需向Configuration对象中加入Company类、HourlyEmployee类和SalariedEmployee类:
Configuration config = new Configuration();config.addClass(Company.class).addClass(HourlyEmployee.class).addClass(SalariedEmployee.class);
14.1.2 操纵持久化对象
这种映射方式不支持多态查询,在本书第11章的11.1.6节(多态查询)介绍了多态查询的概念。对于以下查询语句:
List employees=session.find("from Employee");
<property name="source.root" value="14.1/src"/><property name="class.root" value="14.1/classes"/><property name="lib.dir" value="lib"/><property name="schema.dir" value="14.1/schema"/>
ant -file build1.xml run
public class BusinessService{public static SessionFactory sessionFactory;static{try{Configuration config = new Configuration();config.addClass(Company.class).addClass(HourlyEmployee.class).addClass(SalariedEmployee.class);sessionFactory = config.buildSessionFactory();}catch(Exception e){e.printStackTrace();}}public void saveEmployee(Employee employee) throws Exception{……}public List findAllEmployees() throws Exception{……}public Company loadCompany(long id) throws Exception{……}public void test() throws Exception{List employees=findAllEmployees();printAllEmployees(employees.iterator());Company company=loadCompany(1);printAllEmployees(company.getEmployees().iterator());Employee employee=new HourlyEmployee("Mary",300,company);saveEmployee(employee);}private void printAllEmployees(Iterator it){while(it.hasNext()){Employee e=(Employee)it.next();if(e instanceof HourlyEmployee){System.out.println(((HourlyEmployee)e).getRate());}elseSystem.out.println(((SalariedEmployee)e).getSalary());}}public static void main(String args[]) throws Exception {new BusinessService().test();sessionFactory.close();}}BusinessService的main()方法调用test()方法,test()方法依次调用以下方法。findAllEmployees():检索数据库中所有的Employee对象。loadCompany():加载一个Company对象。saveEmployee():保存一个Employee对象。
List results=new ArrayList();tx = session.beginTransaction();List hourlyEmployees=session.find("from HourlyEmployee");results.addAll(hourlyEmployees);List salariedEmployees=session.find("from SalariedEmployee");results.addAll(salariedEmployees);tx.commit();return results;
select * from HOURLY_EMPLOYEES;select * from COMPANIES where ID=1;
select * from SALARIED_EMPLOYEES;
tx = session.beginTransaction();Company company=(Company)session.load(Company.class,new Long(id));List hourlyEmployees=session.find("from HourlyEmployee h where h.company.id="+id);company.getEmployees().addAll(hourlyEmployees);List salariedEmployees=session.find("from SalariedEmployee s where s.company.id="+id);
company.getEmployees().addAll(salariedEmployees);tx.commit();return company;
tx = session.beginTransaction();session.save(employee);tx.commit();
Employee employee=new HourlyEmployee("Mary",300,company);saveEmployee(employee);
insert into HOURLY_EMPLOYEES(ID,NAME,RATE,CUSTOMER_ID)values(3, 'Mary',300,1);
(出处:http://www.VeVb.com)
新闻热点
疑难解答