nHibernate - How to HQL for joined data - nhibernate

I am having some troubles crafting the HQL for a given nHibernate mapping file (nHibernate 1.2). This SQL Fiddle example shows the table structures and the results I'm wanting. Basically, I'll pass in a Jurisdiction and I want all Titles with no jurisdiction or that specific jurisdiction. The current process pulls everything back and filters in code; I'm working to improve this so it filters at the SQL level.
Mapping file looks very similar to this.
<?xml version="1.0" encoding="utf-8" ?>
<hibernate-mapping xmlns="urn:nhibernate-mapping-2.2"
assembly="MyApp.Business"
namespace="Ap.Bus.Entity"
default-access="field.camelcase-underscore"
default-cascade="save-update" >
<class name="Titles" table="dbo.Titles" mutable="true" lazy="true">
<!--Primary Key-->
<id column="TitleID" name="Id" unsaved-value="0">
<generator class="identity">
</generator>
</id>
<property column="TITLE" name="Title"/>
<property column="Enabled" name="Enabled" />
<bag name="Jurisdictions" table="dbo.Jurisdictions" lazy="false" cascade="none">
<key column="TitleID" />
<many-to-many class="Ap.Shared.Jurisdiction, Ap.Shared" column="JurisdictionID"/>
</bag>
</class>
I've tried so many different HQLs and I could never get it to work when I started joining to the Jurisdictions. It was working when only returning enabled Titles without the join.
So, how can I write the HQL to accomplish this?

select t
from Title t left outer join t.Jurisdictions as j
where j is null or j = :someJurisdiction
Possibly you should add ".JurisdictionID" to the last two "j" above.

Related

Lazy load a self-referencing table

I'm trying to map a self-referencing table with NHibernate 3.2.0.4000. However, whenever I get an instance of DomainObject, it eagerly loads the subsequent versions. I'd rather not have to put an extra column my table, though that is an option.
Can I have NHiberante not eagerly load all of the subsequent versions without maintaining the relationship on both sides?
<?xml version="1.0" encoding="utf-8" ?>
<hibernate-mapping assembly="NHibernateHierarchyTest" namespace="NHibernateHierarchyTest" xmlns="urn:nhibernate-mapping-2.2">
<class name="DomainObject" table="DOMAIN_OBJECT" lazy="true" >
<id name="DomainObjectId" column="DOMAIN_OBJECT_ID">
<generator class="identity" />
</id>
<property name="Property">
<column name="PROPERTY" />
</property>
<many-to-one name="PreviousVersion" class="DomainObject" >
<column name="PREVIOUS_VERSION_DOMAIN_OBJECT_ID" />
</many-to-one>
<!--<many-to-one name="SubsequentVersion" class="DomainObject">
<column name="SUBSEQUENT_VERSION_DOMAIN_OBJECT_ID" />
</many-to-one>-->
<one-to-one name="SubsequentVersion" class="DomainObject" property-ref="PreviousVersion" />
</class>
</hibernate-mapping>
The one-to-one mapping will be always loaded eagarly with NHibernate. Not sure if this is a feature or bug, but that is how it works. If you need lazy load, use many-to-one or one-to-many. Not the best answer I know, but if you can add new column...

NHibernate generator for a property value (either from a sequence or autoincrement)

I have the following nHibernate Mapping and a corresponding class. The mapping works fine.
I want the value of the field 'RfpId' to be auto inserted starting from '1'.
<?xml version="1.0" encoding="utf-8"?>
<hibernate-mapping namespace="IBeam.Core.Models" assembly="IBeam.Core" xmlns="urn:nhibernate-mapping-2.2">
<class name="Rfp" table="EST_MRFP" schema="test$tran">
<id name="Id" type="long">
<generator class="sequence" >
<param name="sequence">test$masters.global_sequence</param>
</generator>
</id>
<property name="RfpId" column="RfpId" type="String" not-null="true" />
<property name="Title" column="Title" type="String" not-null="true" />
</class>
</hibernate-mapping>
How can I do this? Right now I have created an oracle sequence 'RfpSequence'. And before every new record of type Rfp is inserted, I fetch the NEXTVAL from RfpSequence by running a query. Is there any way where nHibernate can do this for me, just like it does for the Id field.
Is the database responsible for the increment? If so try:-
<property name="RfpId" generated="always" update="false" insert="false" />
There is caveat as NH will need to perform a select straight after the insert to update the value.

Multi Table mapped on one class in NHibernate

we use an external Database where we cant edit table designs only add own tables to extend the core tables.
So I need map two tables on one class, i try this:
<?xml version="1.0" encoding="utf-8" ?>
<hibernate-mapping xmlns="urn:nhibernate-mapping-2.2"
assembly="DaVinci"
namespace="DaVinci.Domain">
<class name="Vorgang" table="VORGANGSKOPF">
<id name="Id" column="ID">
<generator class="native" />
</id>
<property name="Vorgangsnummer" column="VORGANG" />
...
<join table="OWN_VORGANG_WAEHRUNG" optional="true">
<key column="VOR_ID" property-ref="Vorgangsnummer" />
<property name="WaehrungVK_Internet" column="WAEHRUNG" />
<property name="WaehrungsKursVK_Internet" column="KURS" />
<property name="Preis_Internet" column="BETRAG_EURO" />
<property name="PreisFremdWaehrung_Internet" column="BETRAG_FREMD" />
</join>
...
After testing i know now that "property-ref" for joins dosn't work.
Bugreport here
Does anyone know an other way to map two tables on one class?
(I'm struggling a little with the table and column names here.)
Could you use a joined subclass for this? Even if the tables don't follow the logical relationship, if there's a 1-to-1 database relationship between the two tables, then you'd get what you want by ignoring the parent, and doing all your operations on the subclass.

Can I "join" two tables into one class whilst also creating many-to-one relationships using NHibernate?

We have a legacy database schema which I've tried (unsuccessfully) to map with NHibernate. To give a simplified example, say I want a Person class whose first name comes from the "Person" table, but their last name comes from the "Person2" table. The "Person" table also has the Id of the person's Car and I want my Person class to have a Car property. I can map all that using the following;
<hibernate-mapping default-cascade="save-update" xmlns="urn:nhibernate-mapping-2.2" auto-import="true">
<class name="NHibernateMappingTest.Person, NHibernateMappingTest" lazy="false">
<id name="Id" >
<generator class="native" />
</id>
<property name="FirstName" />
<many-to-one name="Car" access="property" class="NHibernateMappingTest.Car, NHibernateMappingTest" column="CarId" cascade="save-update"/>
<join table="Person2">
<key column="PersonId" />
<property name="LastName" />
</join>
</class>
</hibernate-mapping>
The lets me combine the Person and Person2 tables, and the lets me find their Car - everything works fine.
But... if the Person2 table happens to have the person's HouseId, I'd like to be able to add a second element to my mapping...
<many-to-one name="House" access="property" class="NHibernateMappingTest.House, NHibernateMappingTest" column="HouseId" cascade="save-update"/>
...so that my Person class can have a House property.
However this is where it all goes wrong, because the SQL which NHibernate generates assumes that the HouseId column is in the Person table (but it's not, it's in Person2), so I get the following error;
MySql.Data.MySqlClient.MySqlException: #42S22Unknown column 'HouseId' in 'field list'
Is NHibernate able to do what I'm attempting, is there a different way to achieve this (without changing the database schema), or have I just made a beginner's error in my map file?
Vincent - thanks for your response. No I wasn't nesting the tag element inside the element. But following your suggestion, I tried and it works perfectly! Thanks very much for responding.
<hibernate-mapping default-cascade="save-update" xmlns="urn:nhibernate-mapping-2.2" auto-import="true">
<class name="NHibernateMappingTest.Person, NHibernateMappingTest" lazy="false">
<id name="Id" >
<generator class="native" />
</id>
<property name="FirstName" />
<many-to-one name="Car" access="property" class="NHibernateMappingTest.Car, NHibernateMappingTest" column="CarId" cascade="save-update"/>
<join table="Person2">
<key column="PersonId" />
<property name="LastName" />
<many-to-one name="House" access="property" class="NHibernateMappingTest.House, NHibernateMappingTest" column="HouseId" cascade="save-update"/>
</join>
</class>
</hibernate-mapping>

HQL: order all items by a specific item in its map

I am quite new to Hibernate and currently struggling a bit with HQL. I have the following mapping and would like to get all "Industry" entities ordered by the "translation" for a given "culture_id"
Code:
<?xml version="1.0" encoding="utf-8" ?>
<hibernate-mapping xmlns="urn:nhibernate-mapping-2.2">
<class name="Domain.Industry, Core" table="industry">
<id name="ID" unsaved-value="0">
<generator class="identity" />
</id>
<map name="AllNames"
access="nosetter.camelcase-underscore"
table="_dict_industry_name"
cascade="all-delete-orphan">
<key column="record_id"></key>
<index column="culture_id" type="Int32"></index>
<element column="translation" type="String"></element>
</map>
</class>
</hibernate-mapping>
I tried the following:
Code:
from Industry industry order by elements(industry.AllNames[:lcid])
but it does not work...
Thanks for any help!!
As there was no answer posted I have asked it in some other forums. Here are two possible solutions:
https://forum.hibernate.org/viewtopic.php?f=1&t=996853
http://groups.google.com/group/nhusers/browse_thread/thread/1750d64ecdeb72f9
I prefer this one:
from Industry industry
where index(industry) = :lcid
order by industry.AllNames