示例:复杂映射实例 本章展示了一些较为复杂的关系映射。 Employer(雇主)/Employee(雇员) 下面关于EmployerEmployee的关系模型使用了一个真实的实体类 (Employment)来表述,这是因为对于相同的雇员和雇主可能会有多个雇佣时间段。 对于金额和雇员姓名,用Components建模。 映射文件可能是这样: employer_id_seq employment_id_seq employee_id_seq ]]> SchemaExport生成表结构。 Author(作家)/Work(作品) 考虑下面的Work,AuthorPerson模型的关系。 我们用多对多关系来描述WorkAuthor, 用一对一关系来描述AuthorPerson, 另一种可能性是Author继承Person 下面的映射文件正确的描述了这些关系: ]]> 映射中有4个表。works, authorspersons 分别保存着work,author和person的数据。author_work是authors和works的关联表。 表结构是由SchemaExport生成的。 Customer(客户)/Order(订单)/Product(产品) 现在来考虑Customer,OrderLineItemProduct关系的模型。CustomerOrder之间 是一对多的关系,但是我们怎么来描述Order / LineItem / Product呢? 我可以把LineItem作为描述OrderProduct 多对多关系的关联类,在Hibernate,这叫做组合元素。 映射文件如下: ]]> customers, orders, line_itemsproducts 分别保存着customer, order, order line item 和 product的数据。 line_items也作为连接orders 和 products的关联表。 杂例 这些例子全部来自于Hibernate的test suite,同时你也可以找到其他有用的例子。 可以参考Hibernate的src目录。 TODO: put words around this stuff "Typed" one-to-one association name 'HOME' name 'MAILING' ]]> Composite key example ( select sum(li.quantity*p.price) from LineItem li, Product p where li.productId = p.productId and li.customerId = customerId and li.orderNumber = orderNumber ) ( select sum(li.quantity) from LineItem li where li.productId = productId ) ]]> Content based discrimination case when title is not null then 'E' when salesperson is not null then 'C' else 'P' end ]]> Associations on alternate keys ]]>