Can I use nHibernate with a legacy database without referential integrity?

If I have a legacy database without referential integrity or keys, and it uses stored procedures for all external access, does it make sense to use nHibernate to persist objects (object graphs)?

In addition, the SP not only contains CRUD operations but also business logic ...

I'm starting to think sticking to a custom ado.net DAL would be easier :(

Greetings

Ollie

+2


source to share


2 answers


You are most likely CAN . But you probably shouldn't :-)

Hibernate doesn't care about referential integrity per se; while it is obvious that it has some kind of relationship between related tables, it doesn't matter if there is an actual FK constraint. For example, if Product

displayed as many-to-one

- Vendor

, the table PRODUCTS

should be available VENDOR_ID

, but it should not be FK.



Depending on your SP signatures, you may or may not be able to use them as a custom CRUD in your mappings; if the SP does have business logic in them that is applied during all CRUD operations, this might be your first potential problem.

Finally, if your SPs are indeed being used for ALL CRUD operations (including all possible queries), it is probably just not worth trying to inject Hibernate into the mix - you get next to nothing and you have another layer to work with.

+1


source


Ok, an example of the problem is this:

SP uses a sql statement like the following to select the next ID to be inserted into the "Id" column of the table (this column is only an int column, not an identity column),

: 'select @cus_id = max (id) + 1 from clients,

so as soon as the next id is calculated, it is inserted into table A with different data, then a row is inserted into table B where there is a reference to table A (no foreign key constraint) in another column from table A, then finally the row is inserted into table C using the same ref to table A.

When I mapped this to NH using a cursory NH, the map generated the correct "insert" sql statement for the first table, but when the second table was displayed as "Reference" the sql update statement was generated. expecting to see an "insert" instruction ...



Now the fact of no identity columns, no keys, and no referential means for me that I cannot guarantee relationships - one-to-one, one-to-many, etc ...

If that's the way NH (fluent) is set up either ...

Greetings

Ollie

0


source







All Articles