Preambula:
I have a number of objects such as Product, Document, etc..
It is needed to classify them by some characteristic.
Different users can use their own model to classify object. They can classify by UNSPSC, eClass or whatever else.
The examples of Classification is "teapot", "cell phone", "books with hard cover", whatever.
Object maps to Classification in relationship of M:N as it is possible to classify Object by different classification models.
Now DB schema responsible for classification looks like
CLASSIFICATION (Teapot, book, car, ...)
PRODUCT
PRODUCT2CLASSIFICATION
DOCUMENT
DOCUMENT2CLASSIFICATION
...
SOMEOBJECT
SOMEOBJECT2CLASSIFICATION
It is inconveniently to keep many linkage table with ConcreteObject-Classification relationship.
I would like to make more consistent DB model and move all Object-Classification data to one table.
Something like that
OBJECT2CLASSIFICATION
object_type -- discriminator ('P' for product, 'D' for document, 'P' for Person ...)
object_sn -- Object Identifier (ProductSN, DocumentSN)
classificationSN
And at the end code should looks like
productObj.getClassifications()
--Hibernate : select from obj2classific where object_type='P' and object_sn = ?
documentObj.getClassifications()
--Hibernate : select from obj2classific where object_type='D' and object_sn = ?
productObj.getClassifications().add(classObje)
--Hibernate : insert into obj2classific (object_type, object_sn, classification_sn) values ('P',?,?)
I need only unidirectional relationship, at least for now.
It looks like many-to-many association with discriminator.
Does Hibernate allows it or other way to solve polymorphic many-to-many relationship??
What is the best practice in such case???
|