NHibernate - Select query in Formula attribute of Property element is not working - nhibernate

I am new to NHibernate.
I am using following property elements in my hbm file...
<property name="CountryId" length="4" />
<property name="CountryForCustomer"
formula="(SELECT *
FROM SystemCountry
WHERE SystemCountry.CountryId = CountryId)" />
Here I am trying to get Country details from the CountryId that I am having in an other table.
Property "CountryForCustomer" is of custom type "SystemCountry".
But this query in formula is not working.. So someone please help me solving this issue.
Thanks in Advance....

Should it not be
SELECT SystemCountry.Country FROM SystemCountry WHERE...
as the * would return multiple columns from the query
edit If you want to return the whole object back then the property element is not your friend here. What you want to do is look at the many-to-one element, note that this is just one way to do this.
<many-to-one name="CmsTemplate" column="TemplateId" ..
e.g. here or here

the Formula mapping does not map its value from the database ,it maps its value from the dataset which is loaded in the session

Related

how to persist a String/Object array such that indices and values are stored into separate columns in eclipselink

Am using EclipseLink 2.4.2 with derby database. I have an Entity class which has a String array. Now, i need to map this array to a separate table 'MY_ARGS' such that index must be mapped to one column 'ARRAY_INDEX' and the value (at index) must be mapped to another column 'ARG'.
In hibernate we have 'array' element through which we can do this. Like below :
<array name="args" table="MY_ARGS" cascade="all">
<key column="PARENT_ID"/>
<list-index column="ARRAY_INDEX" />
<element type="string" column="ARG" length="16384" />
</array>
But in eclipse-link am not able to find any such element (am using eclipselink-orm.xml) through which i can achieve this!
Does Eclipselink support such array to table(multi-column) mapping?
I read about #Converter through which we can convert the data type while storing to/retrieving from DB. But it looks like converter deals with single column and cannot store to two columns simultaneously.
Is there a way that i can do this through converter or any such work around? (I prefer not to use collections)
Any quick help is greatly appreciated!
Thanks in Advance!
-Alekhya

NHibernate mapping customization

I'm using Nhibernate 3.3.1 and I need to ensure that none of my string columns would have lengths smaller than 15 i.e.
I'm trying to check it no AfterMapProperty/BeforeMapProperty events of ModelMapper, but as I know Length property is private of PropertyMapper class or some base class of it.
I'm tryign to avoid to use Reflection to access private property to get Length and check it.
Can you help me?
You can use the check attribute to achieve this.. In your mapping file you need to define something like this:
<property name="Foo" type="string">
<column name="foo" check="DATALENGTH(foo) > 15"/>
</property>
This will create a check constraint.. I am not too sure about the DATALENGTH method but you can confirm that..
Refer section 20.1.1 of NH docs here: http://nhibernate.info/doc/nh/en/index.html

Querying by property of mapped subclass in NHibernate

I'm very new to NHibernate so this may be fairly trivial, but searching is leaving me confused.
I have an AddOnAmount table as follows:
AddOnID | AddOnTypeID | Period | Amount
where AddOnTypeID is a FK. The rows have a unique constraint on AddOnTypeID and Period.
The mapping looks like this:
<id name="Id" column="AddOnId" type="long">
<generator class="native" />
</id>
<many-to-one name="AddOnType" column="AddOnTypeID" class="AddOnTypeStatic" not-null="true" />
<property name="Period" />
etc.
The AddOnTypeStatic class/table just has an Id, which is the numerical value stored on the table, and a descriptive Name.
I'm trying to write a query that will search by AddOnTypeId and Period, so I can validate the existence (or not) of a row before attempting to add a duplicate from my front end, but I'm not sure how to do that as the AddOnAmountStatic class has a subclass whereas the table has just an Id.
So far I've written:
public AddOnAmountStatic FindByAddOnTypeAndPeriod(long addOnType, string period)
{
return FindOne(CreateCriteria()
.Add(Restrictions.Eq("AddOnTypeId", addOnType))
.Add(Restrictions.Eq("Period", period))
.SetCacheable(true));
}
which does not work, as AddOnTypeId isn't a property of AddOnAmountStatic. Not sure how to access the property of the subclass in this context.
My mapping works, as I've been using it so far with no problems.
Solved my problem - it was simple but thought I'd add the solution here in case it helps anyone else.
I'd been thinking of constructing the query from the table's perspective (i.e., with the AddOnTypeID), whereas what I should have done is look at it from the entity's perspective. In other words, I just needed to pass in an AddOnTypeStatic object.
What I did was take my AddOnTypeID parameter, check it exists through NHibernate (returning either an AddOnTypeStatic object or null) then passed that through to the original query. Final query is simply
return FindOne(CreateCriteria()
.Add(Restrictions.Eq("AddOnType", addOnType))
.(Restrictions.Eq("Period", period))
.SetCacheable(true));
Hope this helps another newbie!

NHibernate query against the key field of a dictionary (map)

I have an object model where a Calendar object has an IDictionary<MembershipUser, Perms> called UserPermissions, where MembershipUser is an object, and Perms is a simple enumeration. This is in the mapping file for Calendar as
<map name="UserPermissions" table="CalendarUserPermissions" lazy="true" cascade="all">
<key column="CalendarID"/>
<index-many-to-many class="MembershipUser" column="UserGUID" />
<element column="Permissions" type="CalendarPermission" not-null="true" />
</map>
Now I want to execute a query to find all calendars for which a given user has some permission defined. The permission is irrelevant; I just want a list of the calendars where a given user is present as a key in the UserPermissions dictionary. I have the username property, not a MembershipUser object. How do I build that using QBC (or HQL)? Here's what I've tried:
ISession session = SessionManager.CurrentSession;
ICriteria calCrit = session.CreateCriteria<Calendar>();
ICriteria userCrit = calCrit.CreateCriteria("UserPermissions.indices");
userCrit.Add(Expression.Eq("Username", username));
return calCrit.List<Calendar>();
This constructed invalid SQL -- the WHERE clause contained WHERE membership1_.Username = #p0 as expected, but the FROM clause didn't include the MemberhipUsers table.
Also, I really had to struggle to learn about the .indices notation. I found it by digging through the NHibernate source code, and saw that there's also .elements and some other dotted notations. Where's a reference to the allowed syntax of an association path? I feel like what's above is very close, and just missing something simple.
Just trying to do this myself and it looks like this can be done with HQL but not the Criteria API.
https://nhibernate.jira.com/browse/NH-1795
To do it in HQL:
http://ayende.com/Blog/archive/2009/06/03/nhibernate-mapping-ndash-ltmapgt.aspx
Specifically look for Ayende's comment:
It is something like:
select 1 from Profile p join p.Entries e
where index(e) = 'HasCats' and e = 'true'

Map a property to the latest entry in NHibernate

Let's say my domain looks like this:
I have an object, Vehicle, that has an OdometerReading property.
An OdometerReading has the Miles & Date (when it was read).
I need to keep a history of all OdometerReadings for the Vehicle in the database, but don't want the entire odometer history to belong to the Vehicle object. What I would like is for the OdometerReading property map to the most recent OdometerReading entry out of the database.
I thought about mapping the whole collection of OdometerReadings to the Vehicle, and having a dynamic property called CurrentOdometerReading that would order them and return the latest one, but I don't need the whole collection under the Vehicle in my domain, and it seems like I would be getting more data out of the database than I need.
Is that possible with NHibernate? How would I map such a thing?
There are a few ways of doing this depending on what you want your domain model to look like. My preferred choice is to use a custom collection type, for example IOdometerReadingCollection, which you can add extra methods to. In this case it might be something like:
public interface IOdometerReadingCollection : IList<OdometerReading>
{
OdometerReading Latest { get; }
}
This way you can do:
OdometerReading reading = vehicle.OdometerReadings.Latest;
which I prefer to:
OdometerReading reading = vehicle.LatestOdometerReading;
There's lots of documentation about custom collections, which you can find with a simple google search.
If this approach isn't for you there are other options. You may be able to use a property with a formula (I'm not sure if that works with complex types?), or a regular NHibernate association where you'd have the key of the latest OdometerReading on your Vehicle mapping. As you also mentioned you could just load all the OdometerReadings, which depending on your use case might actually be fine.
I hope this helps, or at least points you in the right direction.
There is a "where" clause that you can put in your collection mapping. Check the reference documentation.
I would map the OdometerReading property as a component, then use a named query to ensure it's populated with the latest reading out of the database. (In this example, you'd have a sql-query with a name of "vehicle" that does the SQL to load the Vehicle columns along with the latest Odometer reading)
<class name="Vehicle">
<property name="Type" not-null="true"/>
<component name="OdometerReading">
<property name="Miles" />
<property name="Date" />
</component>
<loader query-ref="vehicle"/>
</class>