我有三个班级:
xyz/url/core/datastore/ObjectBase.java
xyz/url/core/test/hibernate/BaseClass.java
xyz/url/core/test/hibernate/ChildClass.java
他们的代码:
对象库
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;
}
}
基类
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);
}
}
子类
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;
}
}
目前我使用隐式多态性;我有一个 HBM.XML 文件,用于唯一的“具体”类 ( ChildClass
),名为“ChildClass.hbm.xml”:
<?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>
看到上面了吗?我将三个类的所有属性汇总到一张表中。
我想做和上面一样的事情,得到一个上面的表(“child_class”),但是把它分成三个 HBM.XML 文件。
我希望 Hibernate (v4.1) 支持某种“导入”关键字,因此我可以创建三个 HBM.XML 文件,每个类一个,然后将它们全部链接成一个。
不幸的是,他们“太聪明了”,让事情变得更加复杂。如果我错了,请赐教!
请注意ObjectBase
和BaseClass
是abstract
类。
另一件值得一提的是,当我从数据库中获取一个对象时,我确切地知道它应该是什么类型,所以也许我不应该使用“鉴别器”......?
这是我测试当前拥有的东西时的一些控制台输出,我希望它保持这种状态,我的意思是,创建了一个表,读取时获取了一个表(而不是我在 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 分解为三个不同的文件,这样我就不必在每个具体类的描述符中编写相同的属性。就这样。
谢谢!