NHibernate, obsolete database, foreign keys that are not

The project I'm working on has an outdated database with a lot of information that was used to change the behavior of the application. Basically I am stuck in something that I have to be very careful about changing.

Into my problem. There is a table in this database, and a column in this table. This column contains integers, and most pre-existing data has a value of 0 for this column.

The problem is that this column is actually a foreign key reference to another object; it was never defined as such in the database schema.

Now in my new code, I have defined the Fluent-NHibernate mapping to handle this column as a link, so I donโ€™t have to deal with the object identifier directly in my code. This works fine until I meet an entity that has a value of 0 in this column.

NHibernate considers the value 0 to be a valid reference. When my code tries to use this reference object, I get an ObjectNotFoundException, because obviously in my database there is no object with identifier 0.

How can I either through matching or with some kind of convention (I use Fluent-nhibernate) so that NHibernate will handle an identifier equal to 0, as if it were NULL?

+4
source share
2 answers

I found an API to tell NHibernate to ignore links that were not found (NotFound.Ignore ()) and not throw an exception. I was confused by all the mentions of SetAttribute () that I found on the Internet, which is for an older version of fluent-nhibernate than I use.

+1
source

I am in the same situation. The problem with not-found = ignore is that it will require a relationship every time you try to access it, even if it was already running in the original request. In principle, sleep mode does not preserve the fact that there is no record for the other side of the relationship. This can be seen in the debug log. Here is an example from my current project.

loading entity: attempting to resolve: object not resolved in any cache: Fetching entity: loading entity: Opened new IDbCommand, open IDbCommands: 1 Building an IDbCommand object for the SqlString: SELECT townshipdo0_.TOWNSHIP_CODE as TOWNSHIP1_203_0_, townshipdo0_.TOWNSHIP_NAME as TOWNSHIP2_203_0_, townshipdo0_.TOWNSHIP_TYPE_CODE as TOWNSHIP3_203_0_, townshipdo0_.TOWN_ACTIVE_FLAG as TOWN4_203_0_, townshipdo0_.VERS as VERS203_0_ FROM VTTOW_TOWN_CODE townshipdo0_ WHERE townshipdo0_.TOWNSHIP_CODE=? binding ' ' to parameter: 0 SELECT townshipdo0_.TOWNSHIP_CODE as TOWNSHIP1_203_0_, townshipdo0_.TOWNSHIP_NAME as TOWNSHIP2_203_0_, townshipdo0_.TOWNSHIP_TYPE_CODE as TOWNSHIP3_203_0_, townshipdo0_.TOWN_ACTIVE_FLAG as TOWN4_203_0_, townshipdo0_.VERS as VERS203_0_ FROM VTTOW_TOWN_CODE townshipdo0_ WHERE townshipdo0_.TOWNSHIP_CODE=:p0 

You can see that it is trying to bind, '' the database for this application uses empty space to represent null (dumb I know). But every time nhibernate encounters this, he will try to find it from the database, because he cannot find the entry in the cache and does not know that it is actually NULL.

It would be nice if we could specify a default value to ignore in the configuration. Currently, the only way around this is to use requests to load your relationships, rather than relying on the requests requested by nhibernate.

0
source

Source: https://habr.com/ru/post/1306499/


All Articles