私には3つのクラスがあります:
xyz / url / core / datastore / ObjectBase.java
xyz / url / core / test / hibernate / BaseClass.java
xyz / url / core / test / hibernate / ChildClass.java
彼らのコード:
ObjectBase
package xyz.url.core.datastore;
import java.util.Date;
public abstract class ObjectBase {
private final long m_id;
private final long m_version;
private final Date m_creation_time;
public ObjectBase() {
this.m_id = 0;
this.m_version = 0;
this.m_creation_time = new Date();
}
public long get_id() {
return this.m_id;
}
public long get_version() {
return this.m_version;
}
public Date get_creation_time() {
return this.m_creation_time;
}
}
BaseClass
package xyz.url.core.test.hibernate;
import java.util.Date;
import xyz.url.core.datastore.ObjectBase;
public abstract class BaseClass extends ObjectBase {
private final Date m_another_time;
public BaseClass() {
this.m_another_time = new Date();
}
public void say_something() {
final Class<?> my_class = this.getClass();
final String output = String.format(
"Hello from the `%s` class! My id is %d!", my_class.getName(),
this.get_id());
System.out.println(output);
}
}
ChildClass
package xyz.url.core.test.hibernate;
public class ChildClass extends BaseClass {
private String m_text;
public ChildClass(final String text) {
this.m_text = text;
}
public void set_text(final String text) {
this.m_text = text;
}
public String get_text() {
return this.m_text;
}
}
現在、私は暗黙のポリモーフィズムを使用しています。ChildClass
「ChildClass.hbm.xml」という名前の、唯一の「concrete」クラス()用のHBM.XMLファイルが1つあります。
<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC
"-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://www.hibernate.org/dtd/hibernate-mapping-3.0.dtd">
<hibernate-mapping package="xyz.url.core.test.hibernate"
default-access="field">
<class name="ChildClass" table="child_class">
<!-- Attributes of ObjectBase -->
<id name="m_id" column="id">
<generator class="increment" />
</id>
<version name="m_version" column="version" type="long" />
<property name="m_creation_time" column="creation_time" type="date" />
<!-- Attributes of BaseClass -->
<property name="m_another_time" column="another_time" type="date" />
<!-- Attributes of ChildClass -->
<property name="m_text" column="text" type="string" />
</class>
</hibernate-mapping>
上記を見ますか?3つのクラスのすべての属性を1つのテーブルに集約します。
上記と同じことを行い、上記の1つのテーブル( "child_class")を取得しますが、それを3つのHBM.XMLファイルに分割します。
Hibernate(v4.1)が何らかの「インポート」キーワードをサポートすることを期待していたので、クラスごとに1つずつ、3つのHBM.XMLファイルを作成し、それらをすべて1つにリンクできます。
残念ながら、彼らは「あまりにも賢く」なり、物事をはるかに複雑にしました。私が間違っているなら、私に教えてください!
ObjectBase
とBaseClass
はabstract
クラスであることに注意してください。
言及する価値のあるもう1つのことは、データベースからオブジェクトを取得するとき、それがどのタイプであるかを正確に知っているので、「ディスクリミネーター」を使用すべきではないということです...?..
これが私が現在持っているものをテストするときのコンソール出力です、そしてそれがこのようにとどまることを望みます、つまり、1つのテーブルが作成され、1つのテーブルが読み取られるときにフェッチされます(Hibernateで読んだ「結合戦略」の代わりに)ドキュメント):
Hibernate:
drop table child_class if exists
Hibernate:
create table child_class (
id bigint not null,
version bigint not null,
creation_time date,
another_time date,
text varchar(255),
primary key (id)
)
APR 14, 2012 8:49:47 AM org.hibernate.tool.hbm2ddl.SchemaExport execute
INFO: HHH000230: Schema export complete
Hibernate:
select
max(id)
from
child_class
Hibernate:
/* insert xyz.url.core.test.hibernate.ChildClass
*/ insert
into
child_class
(version, creation_time, another_time, text, id)
values
(?, ?, ?, ?, ?)
繰り返しになりますが、HBM.XMLを3つの異なるファイルに分割するだけなので、すべての具象クラスの記述子に同じプロパティを書き込む必要はありません。それで全部です。
ありがとうございました!