HIBERNATE - Relational Persistence for Idiomatic Java
Hibernate Reference Documentation
Hibernateレファレンスドキュメンテーション
3.2.2
Table of Contents(コンテンツテーブル)
Preface(序文)
1. Introduction to Hibernate(Hibernateの紹介)
2. Architecture(アーキテクチャ)
3. Configuration(設定)
6. Collection Mapping(コレクションのマッピング)
7. Association Mappings(関連マッピング)
8. Component Mapping(コンポーネントのマッピング)
9. Inheritance Mapping(継承マッピング)
10. Working with objects(オブジェクトを扱う)
11. Transactions And Concurrency(トランザクションと並行性)
12. Interceptors and events(インターセプタとイベント)
13. Batch processing(バッチ処理)
15. Criteria Queries(Criteriaクエリ)
17. Filtering data(データのフィルタリング)
18. XML Mapping(XMLマッピング)
18.1. Working with XML data(XMLデータを扱う)
18.1.1. Specifying XML and class mapping together(XMLとクラスのマッピングを同時に指定する)
18.1.2. Specifying only an XML mapping(XMLマッピングだけを指定する)
18.2. XML mapping metadata(XMLマッピングのメタデータ)
18.3. Manipulating XML data(XMLデータを扱う)
Chapter 18. XML Mapping(第18章:XMLマッピング)
Note that this is an experimental feature in Hibernate 3.0 and is under extremely active development.
XMLマッピングはHibernate3.0では試験的な機能であり、非常に活動的に開発中です。
18.1. Working with XML data(XMLデータを扱う)
Hibernate lets you work with persistent XML data in much the same way you work with persistent POJOs. A parsed XML tree can be thought of as just another way to represent the relational data at the object level, instead of POJOs.
Hibernateでは永続性のPOJOを使って作業するのとほぼ同じようなやり方で、永続性のXMLデータを使って作業できます。永続性のXMLツリーは、ちょうどオブジェクトレベル(POJOsの代わりに)で関係のデータを意味するもう一つの方法とみなされることができます。
Hibernate supports dom4j as API for manipulating XML trees. You can write queries that retrieve dom4j trees from the database and have any modification you make to the tree automatically synchronized to the database. You can even take an XML document, parse it using dom4j, and write it to the database with any of Hibernate's basic operations:persist(), saveOrUpdate(), merge(), delete(), replicate() (merging is not yet supported).
HibernateはXMLツリーを操作するためのAPIとしてdom4jをサポートしています。データベースからdom4jのツリーを復元するクエリを書くことができ、ツリーに対して行った修正は自動的にデータベースと同期されます。またXMLドキュメントを取得することができ、dom4jを使ってドキュメントをパースし、Hibernateの任意の基本操作を使ってデータベースへ書き込むことができます。:つまり、persist(), saveOrUpdate(), merge(), delete(), replicate()操作です(マージはまだサポートしていません)。
This feature has many applications including data import/export, externalization of entity data via JMS or SOAP and XSLT-based reporting.
データのインポート/エクスポート、JMSによるエンティティデータの外部化やSOAP、XSLTベースのレポートなど、この機能には多くの用途があります。
A single mapping may be used to simultaneously map properties of a class and nodes of an XML document to the database, or, if there is no class to map, it may be used to map just the XML.
単一のマッピングは、クラスのプロパティとXMLドキュメントのノードを同時にデータベースへマッピングするために使うことができます。またマッピングするクラスがなければ、XMLだけをマッピングするために使うことができます。
18.1.1. Specifying XML and class mapping together(XMLとクラスのマッピングを同時に指定する)
Here is an example of mapping a POJO and XML simultaneously:
これはPOJOとXMLを同時にマッピングする例です。:
<class name="Account"
table="ACCOUNTS"
node="account">
<id name="accountId"
column="ACCOUNT_ID"
node="@id"/>
<many-to-one name="customer"
column="CUSTOMER_ID"
node="customer/@id"
embed-xml="false"/>
<property name="balance"
column="BALANCE"
node="balance"/>
...
</class>
18.1.2. Specifying only an XML mapping(XMLマッピングだけを指定する)
Here is an example where there is no POJO class:
これはPOJOクラスがないマッピングの例です。:
<class entity-name="Account"
table="ACCOUNTS"
node="account">
<id name="id"
column="ACCOUNT_ID"
node="@id"
type="string"/>
<many-to-one name="customerId"
column="CUSTOMER_ID"
node="customer/@id"
embed-xml="false"
entity-name="Customer"/>
<property name="balance"
column="BALANCE"
node="balance"
type="big_decimal"/>
...
</class>
This mapping allows you to access the data as a dom4j tree, or as a graph of property name/value pairs (java Maps). The property names are purely logical constructs that may be referred to in HQL queries.
このマッピングにより、dom4jツリーか、プロパティ名/値の組のグラフ(javaの Map)としてデータにアクセスできます。プロパティの名前は、HQLクエリー内で参照できる純粋な論理構造です。
18.2. XML mapping metadata(XMLマッピングのメタデータ)
Many Hibernate mapping elements accept the node attribute. This let's you specify the name of an XML attribute or element that holds the property or entity data. The format of the node attribute must be one of the following:
多くのHibernateのマッピング要素は node 属性が使用できます。これによりXML属性の名前やプロパティやエンティティデータを保持する要素を指定できます。node 属性のフォーマットは以下の中の1つでなければなりません:
? "element-name" - map to the named XML element("element-name" - 指定したXML要素へマッピングします)
? "@attribute-name" - map to the named XML attribute("@attribute-name" - 指定したXML属性へマッピングします)
? "." - map to the parent element("." - 親要素へマッピングします)
? "element-name/@attribute-name" - map to the named attribute of the named element ("element-name/@attribute-name" - 指定したエレメントの指定した属性へマッピングします)
For collections and single valued associations, there is an additional embed-xmlattribute. If embed-xml="true", the default, the XML tree for the associated entity (or collection of value type) will be embedded directly in the XML tree for the entity that owns the association. Otherwise, if embed-xml="false", then only the referenced identifier value will appear in the XML for single point associations and collections will simply not appear at all.
コレクションと単一の値の関連に対して、おまけの embed-xml 属性があります。デフォルトの embed-xml="true" と設定した場合、関連するエンティティ(値型のコレクション)のXMLツリーは、直接関連を所有するエンティティのXMLツリー内に埋め込まれます。反対に、embed-xml="false" と設定した場合、参照される識別子の値だけが多重度1側の関連に対するXMLに現れ、単純にコレクションはまったく現れなくなります。
You should be careful not to leave embed-xml="true" for too many associations, since XML does not deal well with circularity!
あまりに多くの関連に対してembed-xml="true" としたままにするのは注意すべきです。XMLは循環をうまく扱えません。
<class name="Customer"
table="CUSTOMER"
node="customer">
<id name="id"
column="CUST_ID"
node="@id"/>
<map name="accounts"
node="."
embed-xml="true">
<key column="CUSTOMER_ID"
not-null="true"/>
<map-key column="SHORT_DESC"
node="@short-desc"
type="string"/>
<one-to-many entity-name="Account"
embed-xml="false"
node="account"/>
</map>
<component name="name"
node="name">
<property name="firstName"
node="first-name"/>
<property name="initial"
node="initial"/>
<property name="lastName"
node="last-name"/>
</component>
...
</class>
in this case, we have decided to embed the collection of account ids, but not the actual account data. The following HQL query:
この例では、実際のaccountのデータではなく、accountのidのコレクションを埋め込むことにしました。続きのHQLクエリです:
from Customer c left join fetch c.accounts where c.lastName like :lastName
Would return datasets such as this:
このようなデータセットを返すでしょう。
<customer id="123456789">
<account short-desc="Savings">987632567</account>
<account short-desc="Credit Card">985612323</account>
<name>
<first-name>Gavin</first-name>
<initial>A</initial>
<last-name>King</last-name>
</name>
...
</customer>
If you set embed-xml="true" on the <one-to-many> mapping, the data might look more like this:
<one-to-many> マッピングでembed-xml="true" と設定した場合、データはこのようになるでしょう。
<customer id="123456789">
<account id="987632567" short-desc="Savings">
<customer id="123456789"/>
<balance>100.29</balance>
</account>
<account id="985612323" short-desc="Credit Card">
<customer id="123456789"/>
<balance>-2370.34</balance>
</account>
<name>
<first-name>Gavin</first-name>
<initial>A</initial>
<last-name>King</last-name>
</name>
...
</customer>
18.3. Manipulating XML data(XMLデータを扱う)
Let's rearead and update XML documents in the application. We do this by obtaining a dom4j session:
XMLドキュメントを、アプリケーション内で再読み込みや更新をしてみましょう。以下ではdom4jのセッションを取得することで行います:
Document doc = ....;
Session session = factory.openSession();
Session dom4jSession = session.getSession(EntityMode.DOM4J);
Transaction tx = session.beginTransaction();
List results = dom4jSession
.createQuery("from Customer c left join fetch c.accounts where c.lastName like :lastName")
.list();
for ( int i=0; i<results.size(); i++ ) {
//add the customer data to the XML document
Element customer = (Element) results.get(i);
doc.add(customer);
}
tx.commit();
session.close();
Session session = factory.openSession();
Session dom4jSession = session.getSession(EntityMode.DOM4J);
Transaction tx = session.beginTransaction();
Element cust = (Element) dom4jSession.get("Customer", customerId);
for ( int i=0; i<results.size(); i++ ) {
Element customer = (Element) results.get(i);
//change the customer name in the XML and database
Element name = customer.element("name");
name.element("first-name").setText(firstName);
name.element("initial").setText(initial);
name.element("last-name").setText(lastName);
}
tx.commit();
session.close();
It is extremely useful to combine this feature with Hibernate's replicate() operation to implement XML-based data import/export.
XMLベースのデータのインポート/エクスポートを実装するために、Hibernateのreplicate() 操作をこの機能を結びつけるのは極めて有効です。
No comments:
Post a Comment