I have a requirement to store histroy of message table. means I can create message or update message, when ever I create/update message, some records should also stroed in History table (means in update also , a new row should be insert in histroy) I am trying to implement through one-to-many relation,as if we change name & subject i.e.two attributes,two row will be entered in history table ( many folks in this forum suggested to use bidirectional,that i have used even I require unidirectional)
If am using inverse true, in log only reuired no. of insert queries execute, but messageId comes null when records insert in Message, on update it is fine as I have that time messageId. If I am not using inverse, along with insert update queries also executes, for first insert it working fine but for update new records properly inserted but the first record (which is edited now) modified with null in History table.
Any suggestion, how to deal with, if I want every time insert record in child on save or update inparent table (using one-to-many)
Thanks, Raj
[ July 05, 2008: Message edited by: Rajan Nath ]
[ July 05, 2008: Message edited by: Rajan Nath ] [ July 05, 2008: Message edited by: Rajan Nath ]
For updating related objects, it is always about cascade options. You will most likely need a cascade attribute on both sides. When you have bi-directional mapping, Hibernate does not know that both map to the same relationship, they are considered seperate, that is what the inverse attribute does. However, Hibernate still needs cascade options for each side , so that if you save from either side, the updates will occur.
Thanks Mark for prompt reply, Let me try to explain the scenario, I have one Message table ---------------
my requirement like if user creates a message then one row should be inserted in History table containing only HistoryId and MessageId. if the same message (Name and Subject) edited by the user then two records should be inserted in History Table Like HistoryId MessageId ChangeValue ------- --------- --------- 1     1 -- | -- | ---- 2     1    NewName --- | ---- | --------- 3     1    NewSubject --- ---- | ----------
where as in Message table one record inserted and then same record updated
So I am trying to implement one-to-many association.If I am using inverse, at the time of update I know the messageId so set the messageId in History object and set the History object in Message, then message updated. But for Insert (new records in Message)I can't get messageId, as Message is going to save first time. and after save in Message table, 1 record is inserted in History with null value in MessageId column (I had kept not null false for messageId) Any suggestion ,is it possible to insert in child table for save or update in Parent table Thanks in advance
[ July 05, 2008: Message edited by: Rajan Nath ]
[ July 05, 2008: Message edited by: Rajan Nath ] [ July 06, 2008: Message edited by: Rajan Nath ]
OK, I think I understand what you are looking for. You are expecting Hibernate to handle an application specific use case. Hibernate can't be used to do something that is application/business logic specific, it isn't built to do that for you.
You will need to handle the business use case in code yourself. I can see two possible ways of doing this. One is in the business logic code of your application, which to me is where it should be handled. The second option is to implement a Hibernate Interceptor, which has methods to intercept CRUD operations, and in those methods you also can see what has changed, so you can add a record to the history yourself with the change. This is like logging changes, which is what is written in the Hibernate docs here