一方:
public class TbExam implements java.io.Serializable {
// Fields
private Long examid;
private String name;
private Long zoneId;
private Long year;
private Long type;
private java.util.List<TbQuestion> questions;
}
hbm:
<list
name="questions"
table="TB_QUESTION"
inverse="true"
cascade="save-update"
lazy="true"
>
<cache usage="read-write" />
<key column="EXAM_ID"/>
<index column="INERORDER" type="java.lang.Long"></index>//排序
<one-to-many class="com.jeecms.exam.entity.TbQuestion"/>
</list>
多方:
public class TbQuestion implements java.io.Serializable {
private String alternativeanswer;
private TbExam examId;
private String explain;
private Long useranswer;
}
<many-to-one class="com.jeecms.exam.entity.TbExam" name="examId"
column="EXAM_ID"/>
其中list可以排序,set好像是不能排序的
使用配置文件lazy="true"获取TbExam 的时候,其中questions对象策略为延迟加载。
也就是当访问questions对象的时候,从数据库去获取数据。
有一个问题是,如果questions对象很多的话,这样会产生很多select TbQuestion对象的语句。
可使用fetch="join"配置,用一句关联语句替代,但是这样不管什么时候都把 TbQuestion对象加载进去了。
当只需TbExam ,不需要TbQuestion信息时,这样效率不好。所以不加fetch="join"
当需要获取一个TbExam 对象,同事加载其中所有questions对象的时候
使用String hql = "from TbExam e inner join fetch e.questions q where e.examid=?";
亦可获取。
本文来自优快云博客,转载请标明出处:http://blog.youkuaiyun.com/xiaojun1a/archive/2009/06/04/4242461.aspx