What is the difference between SaveOrUpdateCopy and SaveOrUpdate method in NHibernate?
From nHibernate doc:
Copy the state of the given object
onto the persistent object with the
same identifier. If there is no
persistent instance currently
associated with the session, it will
be loaded. Return the persistent
instance. If the given instance is
unsaved or does not exist in the
database, save it and return it as a
newly persistent instance. Otherwise,
the given instance does not become
associated with the session.
There is one nice blog post on the subject.
Related
Is there a way to find saved items when using SaveOrUpdate method in hibernate. Basically, I need to know if an item is just updated or it is actualy inserted within one session.
Thansk.
Have a look at NHibernate interceptors.
Implementing NHibernate Interceptors
Interceptors
When you are saving objects, as usual transient objects doesn't has an Id, and you can create a list with all processing objects which has an Id. Then you can iterate objects and find objects with new Id - they've been "saved".
Suppose Object A contains a collection of Object B. Let's say it's a one to many (an object B belongs to only one object A)
Now suppose for some reason I already have all the different Object B's in the session cache already.
Now if I went and fetched an object A from the database (giving me an object A with an uninitalized collection of Object B), is there a way I can tell nhibernate to go into the session cache and find all the object B's that belong to this object A and initialize the collection of object B's within the object A I just got?
By initializing the collection, I mean that when I try to use it, it does not need to send any more queries to the database.
I'm using Nhibernate 3.2.
Thanks!
Short answer: no. It doesn't work that way.
I'm using the Repository/Unit of Work pattern in a project using NHibernate. I have a number of repositories, such as Customer and Account. Each repository is associated with the same ISession (unit of work pattern) by passing in the same ISession object to the constructor of each repository.
I then create a bunch of customers and their associated accounts and add them to their respective repositories. Now I want to save these repositories to the database. However, to save in NHibernate involves calling ISession.Save() and passing in an entity. If I was using the Entity Framework instead, and my ISession was an ObjectContext, I would be able to call ObjectContext.SaveChanges() and the contents of all my repositories would be saved to the database.
How can I do this in NHibernate? It seems odd that I have to save each object individually, rather than the ISession as a whole. I also have to save them in a specific order, since a Customer must have an account (AccountID FK in customer table cannot be null) I must therefore save the account first, followed by the customer.
What have I missed?
You don't need to save your objects explicitly. Hibernate has been built with so called "Persistence Ignorance" in mind. Just flush the session and all changes to all known entitys will be persisted.
So just call Session.Flush();
You missed the part where you associate a new object with the ISession (session.Save()). Otherwise how does NH know that some object you created needs to be persisted? If you are only modify existing objects (that you've fetched from the ISession) then all you need to do is commit the transaction/flush the session.
For the customer -> account relationship, you could make the customer cascade saves on it's account.
I have some class with associated list. I want this list not to be loaded when I retrieve the entity, but I want to have an opportunity to load this list later, outside the session in which I cought the entity.
Can NHibernate's lazy mechanism do this?
Thanks!
In theory you can implement your own IBytecodeProvider / ProxyFactory and do whatever you want. But that's quite complex, so you'll want to stick to regular NHibernate usage, which dictates that lazy loading requires an active session. It can be the originating session or you can reattach an entity from a previous session using ISession.Lock()
From outside of the session, you will always get an exception when you access an not-yet loaded object.
There is a way to get your objects from a new session. What you want to do is known as "Remote Lazy Loading". See http://www.theserverside.com/news/1363571/Remote-Lazy-Loading-in-Hibernate
For my current project, we use a nHibernate session to retrieve the object, and another session to update the changes we've made to the object in between the two session. If we use SaveOrUpdate(), nHibernate will typically do a select-then-update (so, two calls to the database).
However, our own business objects already keep track of changes. So, we'd ideally want to intercept within nHibernate and vote whether the object has been changed or not (without letting nHibernate do the select-statement).
Would that be possible with an interceptor?
You can use your own custom Persister.
Check this - https://www.hibernate.org/161.html
with the interceptor you can intercept all querys and change then or add some sql to
don't now if you can stop nhibernate for doing a select query