📄 example-mappings.html
字号:
<property name="alias"/> <one-to-one name="person" constrained="true"/> <set name="works" table="author_work" inverse="true" lazy="true"> <key column="author_id"/> <many-to-many class="Work" column="work_id"/> </set> </class> <class name="Person" table="persons"> <id name="id" column="id"> <generator class="native"/> </id> <property name="name"/> </class></hibernate-mapping></pre><p> 在这个映射中有四个表。<tt class="literal">works</tt>,<tt class="literal">authors</tt>和<tt class="literal">persons</tt>分别存放著作、作者以及人的数据。<tt class="literal">author_work</tt>是关联表,把作者与著作关联起来。以下是由<tt class="literal">SchemaExport</tt>生成的表结构。 </p><pre class="programlisting">create table works ( id BIGINT not null generated by default as identity, tempo FLOAT, genre VARCHAR(255), text INTEGER, title VARCHAR(255), type CHAR(1) not null, primary key (id))create table author_work ( author_id BIGINT not null, work_id BIGINT not null, primary key (work_id, author_id))create table authors ( id BIGINT not null generated by default as identity, alias VARCHAR(255), primary key (id))create table persons ( id BIGINT not null generated by default as identity, name VARCHAR(255), primary key (id))alter table authors add constraint authorsFK0 foreign key (id) references personsalter table author_work add constraint author_workFK0 foreign key (author_id) references authorsalter table author_work add constraint author_workFK1 foreign key (work_id) references works</pre></div><div class="sect1" lang="zh-cn"><div class="titlepage"><div><div><h2 class="title" style="clear: both"><a name="example-mappings-customerorderproduct"></a>18.3. 客户/订单/产品(Customer/Order/Product)</h2></div></div><div></div></div><p> 接下来的例子是关于<tt class="literal">Customer</tt>、<tt class="literal">Order</tt>、<tt class="literal">LineItem</tt>和<tt class="literal">Product</tt>。<tt class="literal">Customer</tt>和<tt class="literal">Order</tt>之间是一对多的关联。那么<tt class="literal">Order</tt>/<tt class="literal">LineItem</tt>/<tt class="literal">Product</tt>之间的关联怎么表示呢?我们可以把<tt class="literal">LineItem</tt>作为关联表来表示<tt class="literal">Order</tt>和<tt class="literal">Product</tt>之间多对多关联,在Hibernate里,它被称为组合元素(composite element)。 </p><div class="mediaobject" align="center"><img src="../shared/images/CustomerOrderProduct.gif" align="middle"></div><p> 映射文档: </p><pre class="programlisting"><hibernate-mapping> <class name="Customer" table="customers"> <id name="id"> <generator class="native"/> </id> <property name="name"/> <set name="orders" inverse="true" lazy="true"> <key column="customer_id"/> <one-to-many class="Order"/> </set> </class> <class name="Order" table="orders"> <id name="id"> <generator class="native"/> </id> <property name="date"/> <many-to-one name="customer" column="customer_id"/> <list name="lineItems" table="line_items" lazy="true"> <key column="order_id"/> <index column="line_number"/> <composite-element class="LineItem"> <property name="quantity"/> <many-to-one name="product" column="product_id"/> </composite-element> </list> </class> <class name="Product" table="products"> <id name="id"> <generator class="native"/> </id> <property name="serialNumber"/> </class></hibernate-mapping></pre><p> <tt class="literal">customers</tt>、<tt class="literal">orders</tt>、<tt class="literal">line_items</tt>和<tt class="literal">products</tt>分别存放客户、订单、订单项以及产品的数据。<tt class="literal">line_items</tt>作为关联表,把订单和产品关联起来。 </p><pre class="programlisting">create table customers ( id BIGINT not null generated by default as identity, name VARCHAR(255), primary key (id))create table orders ( id BIGINT not null generated by default as identity, customer_id BIGINT, date TIMESTAMP, primary key (id))create table line_items ( line_number INTEGER not null, order_id BIGINT not null, product_id BIGINT, quantity INTEGER, primary key (order_id, line_number))create table products ( id BIGINT not null generated by default as identity, serialNumber VARCHAR(255), primary key (id))alter table orders add constraint ordersFK0 foreign key (customer_id) references customersalter table line_items add constraint line_itemsFK0 foreign key (product_id) references productsalter table line_items add constraint line_itemsFK1 foreign key (order_id) references orders</pre></div></div><div class="navfooter"><hr><table width="100%" summary="Navigation footer"><tr><td width="40%" align="left"><a accesskey="p" href="example-weblog.html">上一页</a> </td><td width="20%" align="center"><a accesskey="u" href="index.html">上一级</a></td><td width="40%" align="right"> <a accesskey="n" href="best-practices.html">下一页</a></td></tr><tr><td width="40%" align="left" valign="top">第 17 章 示例:Weblog 应用程序 </td><td width="20%" align="center"><a accesskey="h" href="index.html">起始页</a></td><td width="40%" align="right" valign="top"> 第 19 章 最佳实践(Best Practices)</td></tr></table></div></body></html>
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -