-->
These old forums are deprecated now and set to read-only. We are waiting for you on our new forums!
More modern, Discourse-based and with GitHub/Google/Twitter authentication built-in.

All times are UTC - 5 hours [ DST ]



Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 5 posts ] 
Author Message
 Post subject: Discriminator not working
PostPosted: Sun Nov 11, 2007 12:00 pm 
Newbie

Joined: Sun Nov 11, 2007 11:44 am
Posts: 1
Hello,

I've 2 sqlserver 2005 tables :
- COMPANIES (COMP_ID, COMP_NAME, COMP_GLOBAL_DEP_ID, ...)
- DEPARTMENTS (DEP_ID, DEP_NAME, DEP_COMP_ID, DEP_GLOBAL...)

Also have a Company class and 2 classes (NormalDepartment, GlobalDepartment) heritating from the abstract Department class.

A company is composed of 1 global department and a random number of "normal" departments.

So i got the following mapping :

<class name="Company, App_Code.vie2qrca" lazy="false" table="COMPANIES" >
<id name="Id" column="COMP_ID">
<generator class="native" />
</id>
<property name="Name">
<column name="COMP_NAME" />
</property>
<many-to-one name="GlobalDepartment" class="GlobalDepartment, App_Code.vie2qrca" column="COMP_GLOB_DEP_ID" unique="true" />
<bag name="Departments" inverse="false">
<key column="DEP_COMP_ID" />
<one-to-many class="NormalDepartment, App_Code.vie2qrca" />
</bag>
</class>


and

<class name="Department, App_Code.vie2qrca" lazy="false" table="DEPARTMENTS" discriminator-value="2" >
<id name="Id" column="DEP_ID">
<generator class="native" />
</id>
<discriminator column="DEP_GLOBAL" type="Int32" force="true" />
<property name="Name">
<column name="DEP_NAME" />
</property>
</class>
<subclass name="GlobalDepartment, App_Code.vie2qrca" extends="Department, App_Code.vie2qrca" discriminator-value="1">
<one-to-one name="Company" class="Company, App_Code.vie2qrca" constrained="true" property-ref="GlobalDepartment" />
</subclass>
<subclass name="NormalDepartment, App_Code.vie2qrca" extends="Department, App_Code.vie2qrca" discriminator-value="0">
<many-to-one name="Company" column="DEP_COMP_ID" not-null="true" />
</subclass>

The DEP_GLOBAL field in DEPARTMENTS table is a flag indicating if the department is global or no.

My problem is : when i load a company object, the discriminator just doesn't seem to work: all departments from a company are loaded in the "Departments" list (even the departments with DEP_GLOBAL=1), whereas the GlobalDepartment field remains null...
Log says:
loading collection: [Company.Departments#42]
SELECT [all fields] FROM DEPARTMENTS department0_ WHERE department0_.DEP_COMP_ID=@p0; @p0 = '42'

I guess my mapping is inaccurate, or maybe i miss something (being kinda new to nhibernate).
Anyone can help ?

I have another question: having a foreign key DEP_COMP_ID in my DEPARTMENTS table, is there a way to have a bidirectional linking Company<->GlobalDepartment without the foreign key COMP_GLOBAL_DEP_ID in table COMPANIES ?
I added this one after reading the NHibernate in action...

Thx


Top
 Profile  
 
 Post subject:
PostPosted: Mon Nov 12, 2007 8:51 am 
Senior
Senior

Joined: Thu Feb 09, 2006 1:30 pm
Posts: 172
what you are seeing is actually correct behavior. You need to manually add the condition for the discriminator yourself with a "where" attribute in your mapping file for your bag collection.

Note that this is a change from releases prior to 1.2.0.GA. Before 1.2 you did not have to add the discriminator condition yourself.

See the 1.2 migration guide:
http://www.hibernate.org/407.html

Pay special attention to the section:
"One-to-many association to a subclass requires explicit where attribute."


Top
 Profile  
 
 Post subject:
PostPosted: Tue Dec 04, 2007 5:30 pm 
Beginner
Beginner

Joined: Tue Sep 04, 2007 12:36 pm
Posts: 23
jchapman wrote:
what you are seeing is actually correct behavior. You need to manually add the condition for the discriminator yourself with a "where" attribute in your mapping file for your bag collection.

Note that this is a change from releases prior to 1.2.0.GA. Before 1.2 you did not have to add the discriminator condition yourself.

See the 1.2 migration guide:
http://www.hibernate.org/407.html

Pay special attention to the section:
"One-to-many association to a subclass requires explicit where attribute."


I want to know why this is by design, the discriminator is already specified in the mappings for the subclass, why should a one-to-many mapping have a where clause that repeats the value of the discriminator, this is redundant information isnt it?!

_________________
^^If this post helped you, make sure to rate it Thanks!


Top
 Profile  
 
 Post subject:
PostPosted: Tue Dec 04, 2007 8:26 pm 
Senior
Senior

Joined: Thu Feb 09, 2006 1:30 pm
Posts: 172
PeteWeissbrod wrote:
I want to know why this is by design, the discriminator is already specified in the mappings for the subclass, why should a one-to-many mapping have a where clause that repeats the value of the discriminator, this is redundant information isnt it?!


Now, I can't actually tell you why this is by design, I can only guess as to why it is by design.

My guess is that a bug was found with the old 1.0.x logic in automatically filling in the discriminator values, and then because of the complexity of that bug is was decided to remove the feature rather than fix it. Now, maybe there is even an issue why they can't resolve it.

Do think about this issue for a second though. Sure, you may have a simple inheritance structure, but that does not mean that everyone else does as well. There could be any number of potential items which match that many-to-one. Every potential object which could be of that type must be added to the discriminator in a IN (list of discriminators) fashion.

It still seems like that logic could work (I thought that's what 1.0.x did after all) but there must be a complex case that won't work.

Everything you've seen above is a guess as to why it does not exist. It certainly is a more complex feature than it first appears though.


Top
 Profile  
 
 Post subject:
PostPosted: Thu Dec 06, 2007 12:41 am 
Expert
Expert

Joined: Fri May 13, 2005 11:13 am
Posts: 292
Location: Rochester, NY
This comes up periodically. See http://jira.nhibernate.org/browse/NH-877 . The answer seems mainly to be: "Hibernate works that way."


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 5 posts ] 

All times are UTC - 5 hours [ DST ]


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum

Search for:
© Copyright 2014, Red Hat Inc. All rights reserved. JBoss and Hibernate are registered trademarks and servicemarks of Red Hat, Inc.