I have three classes:
xyz/url/core/datastore/ObjectBase.java
xyz/url/core/test/hibernate/BaseClass.java
xyz/url/core/test/hibernate/ChildClass.java
Their code:
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;
}
}
Currently I use implicit polymorphism; I have one HBM.XML file for the only “concrete” class (ChildClass), named “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>
See the above? I aggregate all the attributes of the three classes into one table.
I want to do the same as above, get one table as above (“child_class”), but break it into three HBM.XML files.
I was hoping Hibernate (v4.1) supports some sort of an “import” keyword, so I can create three HBM.XML files, one for each class, and link them all into one.
Unfortunately they got “too smart” and made things a lot more complicated. If I’m wrong, enlighten me please!
Take note that ObjectBase and BaseClass are abstract classes.
Another thing worth mentioning is that when I get an object from the database I know exactly what type it should be, so maybe I should not use “discriminators”…?..
Here is some console output when I test what I currently have, and I wish it stays this way, I mean, one table is created, one table is being fetched when reading (instead of the “join strategy” I’ve read in Hibernate’s docs):
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
(?, ?, ?, ?, ?)
Again, all I want is to break my HBM.XML to three different files so I don’t have to write the same properties in every concrete class’s descriptor. That’s all.
Thank you!
union-subclassis all I needed. Simple, OOP-like inheritance.