<many-to-one name="a"class="ClassA"column="A_ID"cascade="save-update" />
tx = session.beginTransaction();ClassD d=(ClassD)session.get("ClassD",id);ClassA a=d.getA();if(a instanceof ClassB) System.out.PRintln(((ClassB)a).getB1());if(a instanceof ClassC) System.out.println(((ClassC)a).getC1());tx.commit();
<many-to-one name="a"class="ClassA"column="A_ID"lazy="true"cascade="save-update" />
ClassA a=d.getA();ClassB b=(ClassB)a; //抛出ClassCastException
ClassA a=d.getA();ClassB b=(ClassB)session.load(ClassB.class,a.getId());System.out.println(b.getB1());
tx = session.beginTransaction();ClassD d=(ClassD)session.createCriteria(ClassD.class) .add(EXPression.eq("id",id)) .setFetchMode("a",FetchMode.EAGER) .uniqueResult();ClassA a=d.getA();if(a instanceof ClassB) System.out.println(((ClassB)a).getB1());if(a instanceof ClassC) System.out.println(((ClassC)a).getC1());tx.commit();
<any name="a"meta-type="string"id-type="long"cascade="save-update"><meta-value value="B" class="ClassB" /><meta-value value="C" class="ClassC" /><column name="A_TYPE" /><column name="A_ID" /></any>
public class HourlyEmployee extends Employee{private double rate; /** 完整构造方法*/public HourlyEmployee(String name, double rate,Company company) {super(name,company);this.rate=rate;}/** 默认构造方法*/public HourlyEmployee() {}……}
(出处:http://www.VeVb.com)
新闻热点
疑难解答