NHibernate, map a field to a table in another database - nhibernate

I would like to ask if we can map a field in a table to a table in another database?
Suppose my databases structure is as follow:
dbA -> table A -> field A
dbB -> tableB
Suppose we have ClassA and ClassB corresponding to a record in tableA and tableB, how can I write the map between fieldA and tableB so that we save tableB's name in fieldA, rather than a record in tableB?
Thank you

Not fully sure about the intention...
"...write the map between fieldA and tableB so that we save tableB's name in fieldA, rather than a record in tableB..."
But for example with MS SQL Server, if the connection would be like:
<property name="connection.connection_string">Data Source=MySERVER;Database=dbA;...
we can have a mapping
<class name="ClassA" table="[dbo].[TableA]" ...
and also, if access rights will support it, we can do
<class name="ClassB" table="[dbB].[dbo].[TableB]" ...
In extreme, there could even be a linked object with its catalogue:
<class name="ClassC" table="[LinkedServer].[dbC].[dbo].[TableC]" ...
NOTE: I am not challenging the correctness, just showing the possibility how to interact among DB and even DB Servers (linked object) while using one/single connection

Related

NHibernate join query over multiple databases

I am working on a legacy app and need to query over two tables from two different databases.
Bar the option of selecting both sets of data, then filtering the results are there any other options or best practices for this?
Ideally I would like to run a single query and output the results to a DTO.
Thanks
In your mapping document, specify the table name for the class and prefix it w/ the database name and schema name:
<class name="BvCustomProduct" table="RelaunchCustom.dbo.bvc_Product">
...
</class>
NH should join between the two DBs w/ no problem. This example is for SQL Server, I've not tried it on other dialects but it likely works the same.

Get ID for a lazy many-to-one object withot database acces?

I wonder if this is possible,
i have a Class A with Id property of class B connected with lazy many-to-one relation.
And i want to get A.B.ID without connecting to database(For sure without loading whole B entity). Is is possible in NhibernatE?
A is many and b is One :)
Thx.
Yes this is the default behavior. If you retrieve A from the database then access A.B.Id this will not hit the database. If you access any other property besides the Id field it will cause NHibernate to retrieve B from the database.

NHibernate Query Generation

Some of the NHibernate queries are forming as below, I am just using the NHinernate Map files
select installmen0_.Index as Index1_,installmen0_.accountnumber from Account installmen0_
where installmen0_.accountstatus = 'active'
but I do not have any column called "Index" either in DB table or Map file, Where is NHibernate picking up this column? Please, sujjest where I might be wrong
it is the default columnname for list indexes. Probably you have mapped in Fluent Hasmany(...).AsList() or in xml <list name="mylist"></list>. it is used to persist the index of the element in the list. If you dont care in which order they are in the collection then use Hasmany(...).AsBag() or <bag name="mylist"></bag>

NHibernate update causes a select of a row's computed columns

I've got a User class that I want to update. There's a User table in my database with two columns among others: FirstName and Lastname and there's a computed column called DisplayName that concatenates the two with a space in the middle. The NHibernate mapping for DisplayName is
<property name="DisplayName" type="string" generated="always"/>
When I update the User object and commit the transaction, NHibernate runs an extra select statement just on the DisplayName property, I presume to keep the object and the DB row in sync. I don't need that as the object goes out of scope right afterwards.
Is there something I can do to tell NHibernate that there's no need to get the updated DisplayName at this time?
Regards,
F.
generated="always" means exactly that: "this is a value generated by the DB every time I modify this row; please refresh it for me".
NH does not have a concept like "only refresh this if a particular column changed".
My opinion is that you should generate that value in your domain model instead of the DB.

NHibernate HiLo generation and SQL 2005/8 Schemas

I have an issue on my hands that I've spent several days searching for an answer to no avail...
We're using HiLo Id generation, and everything seems to be working fine, as long as the entity table is in the same schema as the hibernate_unique_key table.
The table structure is pretty simple. I have my hi value table in the db as dbo.hibernate_unique_key. Several entity table are also in the dbo schema, and they work without issue. Then we have tables under the "Contact" schema (such as Contact.Person and Contact.Address).
In the Person Mapping file:
<class name="Person" table="Person" schema="Contact">
<id name="Id" unsaved-value="0">
<generator class="hilo">
<param name="max_lo">100</param>
</generator>
</id>
...
When I try to insert a Person entity, I get an error of "Invalid object name 'Contact.hibernate_unique_key'. That error is certainly clear enough. So I add:
<param name="schema">dbo</param>
to my mapping file/generator element. Now, when the SessionFactory is built, I get a "An item with the same key has already been added." error. So now I'm a bit stuck. I can't leave the HiLo generator without a schema, because it picks up the schema from the Class, and I can't specify the schema because it's already been added (presumably because it's my "default_schema" as identified in my XML cfg file).
Am I completely hosed here? Must I either
A) Keep all my tables in the dbo schema or
B) Create a separate HiLo Key table for each unique schema in the DB?
Neither of those scenarios is particularly palatable for my application, so I'm hoping that I can "fix" my mapping files to address this issue.
Only one such table per database should exist. Such data table should imply the following columns (let's call this table Parameters):
HiLoId
TableName
ParamName
HiLoAssigned
In addition to be used as a HiLo assignment data table, this could be used as a parameter table. As such, the ParamName field is required. This could contain data such as:
HiLoId | TableName | ParamName | HiLoAssigned
---------------------------------------------
1 | Parameters| HiLoId | 3
2 | Customers | CustomerId| 9425
3 | Invoices | InvoiceId | 134978
And when you need some other parameters, such as a parameter for a job that would prune your tables for history, then an age parameter for record could be inserted into it.
Well, I'm a little further in the subject than what you actually asked. Just sharing some additional thoughts in database design/architecture.
Take an eye out this question, and see my answer there. This might answer your question as well, and bring further information to this answer.
Have you tried specifying the schema with the table name on all generators (including the ones already in the dbo schema? I.e.
<param name="table">dbo.hibernate_unique_key</param>
The hilo generator looks for a '.' in the table name, and qualifies it (with schema) only if one isn't there.
I don't think there's anything wrong with solution B. Behavior will be pretty much the same.