Hibernate version:
3.1.1/3.1.3
Mapping documents:
Code:
<hibernate-mapping>
<class
name="model.Person"
schema="org"
table="v_org_contactpersons"
discriminator-value="3">
<id name="orgrId" column="orgr_id">
<generator class="sequence">
<param name="sequence">r_orga_orga_orgr_id_seq</param>
</generator>
</id>
<discriminator type="integer" column="relt_id" />
<property name="relationshipType" column="relt_id" generated="always" />
<property name="orgaId" column="orga_id" generated="always" />
<property name="lastName" column="pers_lastname" />
<property name="firstName" column="pers_firstname" />
</class>
</hibernate-mapping>
<hibernate-mapping>
<joined-subclass
name="model.User"
extends="model.Person"
schema="org"
table="org_vdbusers">
<key column="orga_id" property-ref="orgaId"/>
<property name="login" column="user_login" />
<property name="password" column="user_password" />
</joined-subclass>
</hibernate-mapping>
Name and version of the database you are using:postgresql (7.4)
The generated SQL (show_sql=true):Code:
select
this_.orga_id as orgr1_37_0_,
this_1_.relt_id as relt2_37_0_,
this_1_.orga_id as orga3_37_0_,
this_1_.pers_lastname as pers4_37_0_,
this_1_.pers_firstname as pers5_37_0_,
this_.user_login as user8_9_0_,
this_.user_password as user9_9_0_
from org.org_vdbusers this_
inner join org.v_org_contactpersons this_1_ on [b]this_.orga_id=this_1_.orgr_id[/b]
where this_1_.relt_id=3
order by this_1_.pers_lastname asc
Hi, I'm quite new to Hibernate, so I maybe wrong with my assumptions.
We have a class hierarchy which looks like AbstractOrganisation > Person > User, which resembles in some ways the underlying datamodel. For severals reasons we have to stick with the current db architecture so I just want to model our classes onto it. We have a updateable view v_org_contactpersons (which we have to use for some hysterical reasons) which contains all persons which are contactpersons to an organisation. a person by itself is a contactperson to a virtual organisation "private persons" which is further marked by a special relationshiptype "3" (relt_id), all other contactpersons have other relationshiptypes and also other parents. so there is the id of the organisation itself (orga_id) and there is also the id of the table which contains all the relationships of the organisations (orgr_id). therefore a orga_id together with a relt_id is unique and the orgr_id is unique as well. A person is always also a organisation (this allows us to model all relationships between a person/organisation organisation/organisation or person/person in one table) but that just for explanation.
for instance if a person exists as a contactperson the view will always return 2 rows if I search by orga_id, the person itself, and the person in its function as contactperson for lets say a specific company.
We also have users in our database which are linked through the orga_id with the person. In our application always the orga_id is used to reference persons or organisations. Therefore I have to design the mapping in a way that I can use the orga_id as a key. The mapping above does in my opinion exactly that. I also have used the orgr_id only because I was not able to convince Hibernate to use a composite key made of a constant and a sequence. But within the sql statement the orga_id from the one table is wrongfully joined with the orgr_id from the other table.
I want to have the following query
Code:
...
inner join org.v_org_contactpersons this_1_ on [b]this_.orga_id=this_1_.orga_id[/b]
where this_1_.relt_id=3
...
which would be also correct according to the mapping. So my guess is, that this is a bug in hibernate itself, it should use the column given by property-ref instead the primary key (at least the documentation says so).
But maybe I'm totally wrong and there is a better way to do it, I appreciate all help :-)
Two more things:
1. the first line of the sql statement is a bit frightening ... this_.orga_id as orgr1_37_0_ ..., it should be this_.orga_id as orga1_37_0_
2. Is there any magic within hibernate which will always use a property "id" of a class as a primary key even if I say a different property should be primary key?