inverse = ?
inverse=false(default)
用于单向onetomany关联
parentgetChildren()add(child) // insert child
parentgetChildren()delete(child) // delete child
inverse=true
用于双向onetomany关联
childsetParent(parent) sessionsave(child) // insert child
sessiondelete(child)
在分层结构的体系中parentDao childDao对于CRUD的封装导致往往直接通过session接口持久化对象而很少通过关联对象可达性
To be the best IT community
onetomany关系单向关系还是双向关系?
parentgetChildren()add(child)对集合的触及操作会导致lazy的集合初始化在没有对集合配置二级缓存的情况下应避免此类操作l select * from child where parent_id = xxx性能口诀
一般情况下避免使用单向关联尽量使用双向关联l 使用双向关联inverse=true
在分层结构中通过DAO接口用session直接持久化对象避免通过关联关系进行可达性持久化
To be the best IT community
manytoone关系
单向manytoone表达了外键存储方灵活运用manytoone可以避免一些不必要的性能问题
manytoone表达的含义是……n many可以是可以是也可以是n也就是说manytoone可以表达一对多一对一多对一关系因此可以配置双向manytoone关系例如l 一桌四人打麻将麻将席位和打麻将的人是什么关系?是双向manytoone的关系To be the best IT community
onetoone通过主键进行关联相当于把大表拆分为多个小表例如把大字段单独拆分出来以提高数据库操作的性能Hibernate的onetoone似乎无法lazy必须通过bytecode enhancement To be the best IT community
集合List/Bag/Set onetomany l List需要维护index column不能被用于双向关联必须inverse=false被谨慎的使用在某些稀有的场合l Bag/Set语义上没有区别l 我个人比较喜欢使用Bag manytomany l Bag和Set语义有区别l 建议使用Set To be the best IT community
集合的过滤l children = sessioncreateFilter(parentgetChildren() where thisage > and thisage < )list()
针对一对多关联当中的集合元素非常庞大的情况特别适合于庞大集合的分页l sessioncreateFilter(parentgetChildren())setFirstResult()setMaxResults()list()To be the best IT community
继承关系当中的隐式多态HQL from Object l 将把所有数据库表全部查询出来l polymorphism=implicit(default)将当前对象和对象所有继承子类全部一次性取出l polymorphism=explicit只取出当前查询对象To be the best IT community
[] []