nhibernate update schema : relationships not removed - nhibernate

im using nhibernate (version 3.1.0) with ms compact edition 4.0 and have a 'many to one' relationship for some tables.
works fine, constraints are checked correctly.
i've changed the relationship by adding 'not-found="ignore"' in the hbm.xml, so that i will not have an error when the parent needs a child.
the database is updated by using 'NHibernate.Tool.hbm2ddl.SchemaUpdate', but the relationship is still there ?!
Anyone an idea how to remove these relationships in an update?
thanks.

By default SchemaUpdate will do everything so that you don't lose your data. Deleting something from your schema may have such an impact. So NHibernate won't do that.
But you can do SchemaExport to recreate all tables with constraints.

Related

How to generate automatically relationships with JPA with IntelliJ?

I found this solution for a problem I have: how to generate entities with JPA annotations from a given database.
IntelliJ IDEA 10 generate entity (POJO) from DB model
Now with IntelliJ I'm given the possibility to create relationships between entities manually. Is there a way to generate them automatically as it did with entities?
I used the REFERENCES keyword when needed while creating the database. I suppose there should be an automatic mapping of relationships as well!
When Generating entities from DB Schema in dialog there is an option to 'Show default relationships' which when selected will display FK relationships when selecting tables to generate entities from:

Hibernate Envers SQL auditing

I was wondering if anyone had succeeded in auditing a native query (SQL) with Hibernate Envers? I know this is probably just wrong, but it would spare me a lot of refactoring time.
Cheers
Nick
I just want to leave my thoughts here so others might benefit when they choose to Envers. We tried Hibernate envers in one our recent project and it did not work out. Below are the reason
Hibernate Envers captures the Audit information only when the updates happen through Persistence Context.
We did not like one audit table for each entity. It was too much schema pollution.
We have lot of batch jobs and data synchronization scripts that updates data directly using sql queries. Any update that is happening outside the persistence context will not be captured in these Hibernate ENvers created Audit tables.
SO we went with Database trigger appraoach with just only one AUDIT table which will capture the table_name, column_name, primary_key, old_value and new_value. It worked for us.

Foreign Keys cascading in JPA or database Schema

I have a separate script that creates the database and tables for each database that we are supporting. I am using JPA to manipulate the data in the database, but JPA does not create the database or the tables.
I want to add a foreign key to a new table with a cascade property so that when a row is deleted in the parent table, the corresponding rows in the child table are also deleted.
I am aware of the annotations necessary to do this in JPA, however I can create the foreign keys and the cascade statements in the script I am using to create the databases.
My question is, since I am using a separate script to create the database tables, can I just add the foreign key / cascade statements in the script and then ignore all of the JPA relationship annotations? Is there advantages/disadvantages to adding this information in both the database script as well as in the JPA code?
You should always have a 2 level check. if you do not use the features of JPA, then it's a big waste of the functionality JPA provides. you should actually make sure that you JPA relations match your DB relations as closely as possible. It will help you a lot as JPA can cache data and even prevent unnecessary calls to DB.
eg if u have a not null constraint and you persist with no JPA constraint, your DB has to do all the work and throw the exception back.
normally in an application, the network and DB are the slowest factors in the app. so you should try mimicking the constraints in JPA to avoid unnecessary overhead.
also using such constraints you can form bidirectional relationships and have collections of associated entities and many more such advantages.

Work around for envers auditing for bulk update

In the application which I am working on I use spring, hibernate and envers for auditing. envers works with calls like, hibernateTemplate.insert, hibernateTemplate.save, hibernateTemplate.saveOrUpdate. But it doesnt seem to work when i call hibernateTemplate.bulkUpdate.
I googled for solutions and found that envers doesnt support bulkUpdate.
A work around has been provided in the link below but i am not able to get it.
Envers Bulk insert/updates
It would be of help if someone can provide a workaround/sample for this.
Thanks
The documentation is correct. HQL and native SQL operations are not audited.
Since the performance of the bulk update will be affected by auditing, you may wish to change your design - for example, if you have a parent entity with related children, and you are performing a bulk update on the child records, you could update attributes of the parent record and then call saveOrUpdate after doing the bulk update.
Another option, is to manually perform the bulk update on the audit table(s) also, but its not going to be elegant. I managed to get around the issue in my case by changing the design as per the above.

Make (Fluent) NHibernate Recreate only specific tables in a database

Is there a way to tell NHibernate to leave some tables alone? I have the ASPNET Membership tables, and while I would like to be able to access them using NHibernate, I don't want SchemaExport to delete them and recreate them while recreating the other tables. (Goodness knows what may happen)
You need to use SchemaAction.None()
More information here: http://www.lostechies.com/blogs/rodpaddock/archive/2010/06/29/using-fluent-nhibernate-with-legacy-databases.aspx