History | Log In     View a printable version of the current page.  
Issue Details (XML | Word | Printable)

Key: NHCD-22
Type: Patch Patch
Status: Closed Closed
Resolution: Fixed
Priority: Major Major
Assignee: Unassigned
Reporter: Alexei
Votes: 20
Watchers: 19
Operations

If you were logged in you would be able to see more operations.
NHibernate.ContribDrivers

Add join mapping element to map one class to several tables

Created: 18/Nov/05 10:57 AM   Updated: 20/Aug/08 02:32 PM
Component/s: JetDrive
Affects Version/s: 1.2.1GA
Fix Version/s: None

File Attachments: 1. Text File JetCaseFragment_r2826.patch (0.6 kb)
2. Text File NH466_against_r2594.patch (126 kb)
3. Text File NH466_against_r2680.patch (135 kb)
4. Text File NH466_CompositeKey_r2820.patch (8 kb)
5. Text File NH466_r2826.patch (8 kb)

Issue Links:
Related
 


 Description  « Hide
It would be nice to be able to map properties of one class to several tables as Hibernate 3.0 does with a <join> element.
An important feature when working with legacy data models shared by existing applications.

 All   Comments   Change History      Sort Order: Ascending order - Click to sort in descending order
Taavi Kõosaar - 21/Nov/06 01:07 AM
How would nhibernate developers feel about taking this into nhibernate 1.2 release ? (although i think new features are not to be added anymore ??)
This would be a very valuable addition and a long waited addition.

And to sum things up, great work with the API !!! Love to use it !!

Sergey Koshcheyev - 21/Nov/06 01:19 AM
If you had a patch, we could discuss it.

Karl Chu - 28/Jan/07 07:46 PM
Hi Sergey, I have attached a patch against r2594. It includes tests ported over from H3 plus some others. I couldn't get the "optional" attribute to work. Frankly, I am not too sure how it is supposed to behave. Can you shed some light on this?

By the way, I would consider this patch incomplete because of the "optional" attribute described above. I also need to write some tests to test the "inverse" attribute.

Thanks in advance.

Sergey Koshcheyev - 29/Jan/07 12:51 AM
Thanks for the patch, Karl. I will only add this after 1.2 final is released though.

Karl Chu - 30/Jan/07 10:22 AM
I need a clarification on the "inverse" attribute. Assume the following mapping:

<class name="Person" ...>
    ...
    <join table="Stuff" inverse="true">
        <key .../>
        <property name="StuffName"/>
    </join>
</class>

I understand from the H3 documentation that "If enabled, Hibernate will not try to insert or update the properties defined by this join". However, when a Person is deleted, should the delete cascade down to the "Stuff" table?

Sergey Koshcheyev - 30/Jan/07 10:35 AM
Judging from the code, delete will not cascade to inverse joins.

Karl Chu - 10/Mar/07 03:42 PM
Updated the last patch. This one was created from r2680.

This patch added tests to test the "inverse" attribute. When "inverse" is true, the joined table is not updated, inserted to, or deleted from (see previous comment). Can someone confirm that it behaves the same way in Hibernate 3.2 please? Thanks.

Taavi Kõosaar - 22/Mar/07 12:28 AM
Hello Karl,

I cannot tell you if this works as it should in 3.2, but i am going to put the patch in the version of nhibernate we use currently and let you know how this goes ;)

Taavi

Karl Chu - 22/Mar/07 06:15 AM
Thanks Taavi. Please let me know if this needs more work.

Karl

Ayende Rahien - 24/Apr/07 12:12 AM
Applied to trunk, thanks.

Sergey Koshcheyev - 24/Apr/07 12:22 AM
Changing fix version to TRUNK since this wasn't applied to the 1.2.x branch.

Andrew Tregonning - 22/May/07 07:57 PM
This doesn't seem to work for composite keys.

I tried a mapping file similar to the one below, and it produced an error in NHibernate.SqlCommand.ANSIJoinFragment.AddJoin because that method only had one element in the pkColumns array, as opposed to the two in the fkColumns array.

Thanks,
Andrew

<?xml version="1.0" encoding="utf-8" ?>
<hibernate-mapping xmlns="urn:nhibernate-mapping-2.2" assembly="Core" namespace="Core.Domain">
<class name="Employee" table="DLEPAPPT" lazy="false" where="emp_number = emp_number_main">
<composite-id name="Pk" class="EmployeePk">
<key-property name="Company" column="coy" />
<key-property name="EmpNumber" column="emp_number_main" />
</composite-id>

<property name="StartDate" column="emp_start_date" />

<join table="DLEPEM">
<key>
<column name="coy" />
<column name="emp_number" />
</key>
<property name="FirstName" column="emp_first_name" />
<property name="Surname" column="emp_surname" />
<property name="OtherNames" column="emp_other_names" />
<property name="Title" column="emp_title" />
</join>
</class>
</hibernate-mapping>

Karl Chu - 22/May/07 10:45 PM
Hi all, I will have a look at the problem with composite key.

Karl Chu - 22/May/07 11:16 PM
Andrew, may be you need to use <key-many-to-one> as in the following?

<class name="Employee" ...>
    <composite-id name="Pk" class="EmployeePk">
        <key-many-to-one name="Company" column="coy" />
        <key-property name="EmpNumber" column="emp_number_main" />
    </composite-id>
    ...
</class>

Can you give that a try?

Andrew Tregonning - 23/May/07 03:41 PM
Hi Karl

Thanks for looking into this.

Changing the mapping to "key-many-to-one" doesn't help in this situation. The "Company" and "EmpNumber" properties are not complex types - they're just ints and Nhibernate now complains that "An association from the table DLEPAPPT refers to an unmapped class: Int32".

Thanks,
Andrew

Karl Chu - 25/May/07 09:38 PM
NH466_CompositeKey_r2820.patch

Problem with composite-key fixed.

Ayende Rahien - 10/Jun/07 11:15 AM
Karl Chu,
Can you regenerate the patch from the trunk, I have been unable to apply it.

Karl Chu - 10/Jun/07 02:58 PM
Hi Ayende, I recreated the patch against r2826 (attached).

There was also a problem with NHibernate.JetDriver.JetCaseFragment (or is it really a problem of the base class NHibernate.SqlCommand.CaseFragment?) At any rate, the easiest thing for me to do so I can run the tests again before re-uploading the patch was to change JetCaseFragment. I will upload a separate patch because it really isn't an issue having to do with this <join> patch.

Karl Chu - 10/Jun/07 02:59 PM
Submitted patch to fix NHibernate.JetDriver.JetCaseFragment re previous note.

Ayende Rahien - 23/Jun/07 11:44 AM
I applied the first patch, the JetCaseFragement was already solved by Sergey, I think.
Thanks!

Dirc Khan-Evans - 03/Jul/07 01:27 AM
Using this within a subclass causes an exception: It seems to want to apply this join to the base class:

My mapping:

<?xml version="1.0" encoding="utf-8" ?>
<hibernate-mapping xmlns="urn:nhibernate-mapping-2.2" assembly="Narrowstep.Entities" namespace="Narrowstep.Entities">
<subclass discriminator-value="5" extends="Vlip" name="DownloadPackage" >
<join table="dspChannelServer">
<key>
<column name="VlipID" />
</key>
<property name="IsEnabled" column="IsEnabled"/>
<many-to-one name="PaymentRule" column="PBRuleID" class="PaymentRule" foreign-key="VlipID" />
</join>
</subclass>
</hibernate-mapping>

However, when I try to access the base class, it tries to bring back the extended properties of the subclass and this throws a SqlException:

SQL Generated:


SELECT vlip0_.PKid as PKid3_0_, vlip0_.ProjectID as ProjectID3_0_, vlip0_.MetaDataID as MetaDataID3_0_, vlip0_.Title as Title3_0_, vlip0_.Abstract as Abstract3_0_, vlip0_.ShortAbstract as ShortAbs7_3_0_, vlip0_.Author as Author3_0_, vlip0_.Copyright as Copyright3_0_, vlip0_.Keywords as Keywords3_0_, vlip0_.Series as Series3_0_, vlip0_.Episode as Episode3_0_, vlip0_.Part as Part3_0_, vlip0_.VlipLength as VlipLength3_0_, vlip0_.StartTime as StartTime3_0_, vlip0_.Duration as Duration3_0_, vlip0_.CreateDate as CreateDate3_0_, vlip0_.EventDate as EventDate3_0_, vlip0_.ExpiryDate as ExpiryDate3_0_, vlip0_.LinkUrl as LinkUrl3_0_, vlip0_.ThumbID as ThumbID3_0_, vlip0_.ThumbUrl as ThumbUrl3_0_, vlip0_.Notes as Notes3_0_, vlip0_.AudioInfo as AudioInfo3_0_, vlip0_.VideoInfo as VideoInfo3_0_, vlip0_.Rating as Rating3_0_, vlip0_.RatingDescription as RatingD27_3_0_, vlip0_1_.IsEnabled as IsEnabled1_0_, vlip0_1_.PBRuleID as PBRuleID1_0_, vlip0_.ContentTypeID as ContentT2_0_
FROM Vlips vlip0_
WHERE vlip0_.PKid=@p0; @p0 = '40845'

Exception:

System.Data.SqlClient.SqlException: The multi-part identifier "vlip0_1_.IsEnabled" could not be bound.
As you can see it is not included in the FROM clause, but the column names are incorrectly included.






Karl Chu - 03/Jul/07 08:48 AM
Dirc,

There are already test cases testing for the usage pattern you are describing, and the tests pass. Since this issue has long been closed, I suggest you create a failing test and post it in a new JIRA issue.