• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

discriminator as member variable of the Class

 
Anonymous
Ranch Hand
Posts: 18944
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi ORM - Hiber-Ranchers,

I have discriminator "CATEGORYID" in say CATEGORY Table, which actually refers to as Foreign Key to the CATEGORY_TYPES Table.


I also inserted the discriminator field as a member variable, so that the abstract class Category has it. The logic behind this decision is that I am at the moment in charge of creating a GUI for the administration of the Categories, so I will create a new Category under a specified Category_Type. (CATEGORY_TYPE is fixed, it is not to be administered)



How can I achieve this, b'cos since the Category class is abstract I cannot just create one, and update the Type field.

One alternative: I might make Category concrete, but I suspect if this is the correct way of handling this problem?

Any suggestions, experience?

Thx in Advance,
Yilmaz Mete
 
Paul Sturrock
Bartender
Posts: 10336
Eclipse IDE Hibernate Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Why create an instance of Category when you only persist sub-types of Category? What discriminator value would Hibernate give to this record? This is why when you use the table-per-class-hierarchy pattern your base type is (usually) abstract, and the disciminator is never exposed outside the Hibernate layer.
 
Anonymous
Ranch Hand
Posts: 18944
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thx for the reply.

But I know at RunTime of what Category_Type the newly to be added Category will be.

say, types are defined in CATEGORY_TYPES Table


and I 'd like to add another row to the CATEGORIES Table, which will be of type FileType (referring to CATEGORY_TYPES.idCatType = 100 and it will have description = "XYZ File Type"

Please note that: Matter File Type, Long File Type, and Alien File Type are of class type FileType which extends the Abstract Class Category. and I cannot alter the DB Schema.

So @ Runtime, before creating a new Element(correspondingly row in the DB) in the Object Model, I know of which type (ex. 'FileType' Class) the new item will be.


Example:
MyHiberUtil.loadbyIdAndClassName(AbstractClassType.class, id(ex.1)); --> gives me a
FileType object, of course with a AbstractClassType reference,
which has description "Long File Type".

But how can I create a new description from scratch? since the ref. class is abstract, new XYZ(); reference.save(); does not work...


And after changing the signature of the Abstract Class to Concrete, I create sth. but it always refers to a from the system determined some constant categoryId, say instead of FileType, even if I explicitly declared and assigned it to be so; I get a "GenderType" in the database...
Confusing for me???
by the way, I am not an Hibernate expert or so.

Hope I have been able to clarify the thing a bit more...

[ December 21, 2005: Message edited by: Yilmaz Mete ]
[ December 21, 2005: Message edited by: Yilmaz Mete ]
 
Paul Sturrock
Bartender
Posts: 10336
Eclipse IDE Hibernate Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

But how can I create a new description from scratch?

Description is just a property. Set it to any value you like. By the way, if your description field of CATEGORIES is going to contain a description that has meaningful information about the CATEGORY_TYPE, then your model is not properly normalized, since you already record that information with the id field (is this a mistake in your DDL, having two columns named the same?).
 
Anonymous
Ranch Hand
Posts: 18944
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
 
Consider Paul's rocket mass heater.
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic