0

I have 2 identical DB instances containing FOO_TABLE with the same schema. So, currently I have one class definition per DB instance:

<class name="FooTable" table="FOO_TABLE" entity-name="FooTableInstance1">
  <property name="..." column="..." />
  <property name="..." column="..." />
  ....
</class>

<class name="FooTable" table="FOO_TABLE" entity-name="FooTableInstance2">
  <property name="..." column="..." />
  <property name="..." column="..." />
  ....
</class>

The problem is that I don't want to copy-paste the properties, as the tables have the same schema. Is it possible to inherit the 2 classes from a base class which contains all the mappings and in the 2 children classes specify different entity-name?

tuxx
  • 503
  • 1
  • 4
  • 16

2 Answers2

0

Yes, it is possible. Take a look at the relevant documentation: http://docs.jboss.org/hibernate/core/3.3/reference/en/html/inheritance.html

More specifically, check 9.1.5. Table per concrete class. Make the parent class abstract and things should work fine.

According to the documentation you need 3 Java classes:

  1. Foo (abstract, containing all fields you want in both tables)
  2. FooChild1 (concrete, subclass of Foo, containing no new fields)
  3. FooChild2 (concrete, subclass of Foo, containing no new fields)

You will need two tables. One mapping to FooChild1, and another to FooChild2.

Dherik
  • 17,757
  • 11
  • 115
  • 164
Akira
  • 4,001
  • 1
  • 16
  • 24
  • 1
    Do I really need to create an abstract class FooTableBase and put all columns there? And then create an empty child class FooTable. FooTable will be completely empty as all tables have the exact same schema. – tuxx Nov 15 '13 at 20:20
  • I know it looks redundant and it actually is. But the straightforward way to map things in Hibernate is by having classes mapped to db tables. Perhaps your use case does not perfectly match Hibernate assumptions. – Akira Nov 15 '13 at 20:27
  • Just realized there was a misunderstanding about the solution. You do not need an empty table on the database. Check my answer again. I hope it is clear now. – Akira Nov 15 '13 at 20:45
  • What I'm trying to say is that I have the _exact_ same table (table name, table schema, etc. everything) replicated on multiple DB instances. When I want to store an entry, I select the DB instance at runtime (from the entity-name), and store it there. So basically, I have one big table partitioned across many DB instances. Creating a hierarchy of classes seems very redundant. Is this really such an extraordinary use case for Hibernate? – tuxx Nov 18 '13 at 18:51
0

An alternative (and perhaps the correct one if I understand your question correctly) is to use a @MappedSuperclass to define the common mappings. Whether you use this or the suggestion posted previously depends on the data model: for example are these two entities related so that you would like to be able to query across both of them?

e.g. select f from Foo returns all Foo1 and Foo2.

This cannot be done when Foo is a MappedSuperclass.

See here for further details:

Dherik
  • 17,757
  • 11
  • 115
  • 164
Alan Hay
  • 22,665
  • 4
  • 56
  • 110