<html><head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Chapter 5. Basic O/R Mapping</title><link rel="stylesheet" href="../shared/css/html.css" type="text/css"><meta name="generator" content="DocBook XSL Stylesheets V1.65.1"><link rel="home" href="index.html" title="HIBERNATE - Relational Persistence for Idiomatic Java"><link rel="up" href="index.html" title="HIBERNATE - Relational Persistence for Idiomatic Java"><link rel="previous" href="persistent-classes.html" title="Chapter 4. Persistent Classes"><link rel="next" href="collections.html" title="Chapter 6. Collection Mapping"></head><body bgcolor="white" text="black" link="#0000FF" vlink="#840084" alink="#0000FF"><div class="navheader"><table width="100%" summary="Navigation header"><tr><th colspan="3" align="center">Chapter 5. Basic O/R Mapping</th></tr><tr><td width="20%" align="left"><a accesskey="p" href="persistent-classes.html">Prev</a> </td><th width="60%" align="center"> </th><td width="20%" align="right"> <a accesskey="n" href="collections.html">Next</a></td></tr></table><hr></div><div class="chapter" lang="en"><div class="titlepage"><div><div><h2 class="title"><a name="mapping"></a>Chapter 5. Basic O/R Mapping</h2></div></div><div></div></div><div class="sect1" lang="en"><div class="titlepage"><div><div><h2 class="title" style="clear: both"><a name="mapping-declaration"></a>5.1. Mapping declaration</h2></div></div><div></div></div><p>
Object/relational mappings are usually defined in an XML document. The mapping
document is designed to be readable and hand-editable. The mapping language is
Java-centric, meaning that mappings are constructed around persistent class
declarations, not table declarations.
</p><p>
Note that, even though many Hibernate users choose to write the XML by hand,
a number of tools exist to generate the mapping document, including XDoclet,
Middlegen and AndroMDA.
</p><p>
Lets kick off with an example mapping:
</p><a name="mapping-declaration-ex1"></a><pre class="programlisting"><?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC
"-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
<hibernate-mapping package="eg">
<class name="Cat"
table="cats"
discriminator-value="C">
<id name="id">
<generator class="native"/>
</id>
<discriminator column="subclass"
type="character"/>
<property name="weight"/>
<property name="birthdate"
type="date"
not-null="true"
update="false"/>
<property name="color"
type="eg.types.ColorUserType"
not-null="true"
update="false"/>
<property name="sex"
not-null="true"
update="false"/>
<property name="litterId"
column="litterId"
update="false"/>
<many-to-one name="mother"
column="mother_id"
update="false"/>
<set name="kittens"
inverse="true"
order-by="litter_id">
<key column="mother_id"/>
<one-to-many class="Cat"/>
</set>
<subclass name="DomesticCat"
discriminator-value="D">
<property name="name"
type="string"/>
</subclass>
</class>
<class name="Dog">
<!-- mapping for Dog could go here -->
</class>
</hibernate-mapping></pre><p>
We will now discuss the content of the mapping document. We will only describe the
document elements and attributes that are used by Hibernate at runtime. The mapping
document also contains some extra optional attributes and elements that affect the
database schemas exported by the schema export tool. (For example the <tt class="literal">
not-null</tt> attribute.)
</p><div class="sect2" lang="en"><div class="titlepage"><div><div><h3 class="title"><a name="mapping-declaration-doctype"></a>5.1.1. Doctype</h3></div></div><div></div></div><p>
All XML mappings should declare the doctype shown. The actual DTD may be found
at the URL above, in the directory <tt class="literal">hibernate-x.x.x/src/org/hibernate
</tt> or in <tt class="literal">hibernate3.jar</tt>. Hibernate will always look for
the DTD in its classpath first. If you experience lookups of the DTD using an
Internet connection, check your DTD declaration against the contents of your
claspath.
</p><div class="sect3" lang="en"><div class="titlepage"><div><div><h4 class="title"><a name="mapping-declaration-entity-resolution"></a>5.1.1.1. EntityResolver</h4></div></div><div></div></div><p>
As mentioned previously, Hibernate will first attempt to resolve DTDs in its classpath. The
manner in which it does this is by registering a custom <tt class="literal">org.xml.sax.EntityResolver</tt>
implementation with the SAXReader it uses to read in the xml files. This custom
<tt class="literal">EntityResolver</tt> recognizes two different systemId namespaces.
</p><div class="itemizedlist"><ul type="disc"><li><p>
a <tt class="literal">hibernate namespace</tt> is recognized whenever the
resolver encounteres a systemId starting with
<tt class="literal">http://hibernate.sourceforge.net/</tt>; the resolver
attempts to resolve these entities via the classlaoder which loaded
the Hibernate classes.
</p></li><li><p>
a <tt class="literal">user namespace</tt> is recognized whenever the
resolver encounteres a systemId using a <tt class="literal">classpath://</tt>
URL protocol; the resolver will attempt to resolve these entities
via (1) the current thread context classloader and (2) the
classloader which loaded the Hibernate classes.
</p></li></ul></div><p>
An example of utilizing user namespacing:
</p><pre class="programlisting"><?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC
"-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd" [
<!ENTITY types SYSTEM "classpath://your/domain/types.xml">
]>
<hibernate-mapping package="your.domain">
<class name="MyEntity">
<id name="id" type="my-custom-id-type">
...
</id>
<class>
&types;
</hibernate-mapping></pre><p>
Where <tt class="literal">types.xml</tt> is a resource in the <tt class="literal">your.domain</tt>
package and contains a custom <a href="mapping.html#mapping-types-custom" title="5.2.3. Custom value types">Section 5.2.3, “Custom value types”</a>.
没有合适的资源?快使用搜索试试~ 我知道了~
资源详情
资源评论
资源推荐
收起资源包目录
Hibernate.Spring.doc.rar (41个子文件)
hibernate.Doc
en
html
transactions.html 59KB
associations.html 24KB
architecture.html 19KB
best-practices.html 12KB
objectstate.html 54KB
example-mappings.html 23KB
xml.html 11KB
toolsetguide.html 22KB
persistent-classes.html 22KB
performance.html 63KB
filters.html 8KB
querysql.html 30KB
events.html 13KB
mapping.html 159KB
collections.html 51KB
components.html 17KB
inheritance.html 22KB
querycriteria.html 18KB
example-weblog.html 13KB
index.htm 39KB
tutorial.html 72KB
index.html 39KB
session-configuration.html 69KB
preface.html 6KB
example-parentchild.html 15KB
batch.html 18KB
queryhql.html 48KB
shared
css
html.css 1KB
images
overview.gif 8KB
lite.gif 7KB
EmployerEmployee.gif 10KB
full_cream.gif 9KB
CustomerOrderProduct.gif 6KB
AuthorWork.gif 7KB
spring.Doc
Overview (Spring Framework API 2_5)_files
allclasses-frame_files
stylesheet.css 1KB
overview-summary_files
stylesheet.css 1KB
overview-frame_files
stylesheet.css 1KB
allclasses-frame.htm 483KB
overview-summary.htm 90KB
overview-frame.htm 56KB
Overview (Spring Framework API 2_5).htm 2KB
共 41 条
- 1
JohnsonElizeee
- 粉丝: 59
- 资源: 35
上传资源 快速赚钱
- 我的内容管理 展开
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
最新资源
- 信息办公个人求职管理系统-jobgljsp.rar
- 信息办公一流网络JSP网络管理系统 v1.0-yljsp10.rar
- chirpstack学习
- 管家婆辉煌、财贸、工贸、服装,食品,千方模拟狗
- 基于python开发的工业环境老鼠检测+源码+文档(毕业设计&课程设计&项目开发)
- USB转以太网的芯片SR9900全套设计资料包括(参考设计原理图PCB+ Linux -Windows驱动程序+量产工具)
- 信息办公XML考试系统-xmlks.rar
- 基于python开发的无人机图像目标检测+实验数据+开发文档+操作流程+源码(毕业设计&课程设计&项目开发)
- 全球智能商品管理与优化系统
- IDM下载器(电脑小工具)
资源上传下载、课程学习等过程中有任何疑问或建议,欢迎提出宝贵意见哦~我们会及时处理!
点击此处反馈
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功
评论0