• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Tim Cooke
  • paul wheaton
  • Paul Clapham
  • Ron McLeod
Sheriffs:
  • Jeanne Boyarsky
  • Liutauras Vilda
Saloon Keepers:
  • Tim Holloway
  • Carey Brown
  • Roland Mueller
  • Piet Souris
Bartenders:

complex one-to-many

 
Greenhorn
Posts: 22
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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)

the entry in hbm files are like

<?xml version="1.0"?>
<hibernate-mapping package="nl.inspiring.hibernate.test" default-access="field">
<class name="Message">
<id name="messageId">
<generator class="native"/>
</id>
<property name="name" column="name"/>
<property name="subject" column="sub"/>
<set name="histories" cascade="save-update" inverse="true"/>
<key column="messageId" />
<one-to-many class="History"/>
</set>
</class>
<class name="History">
<id name="historyId">
<generator class="native"/>
</id>
<property name="name" unique="true"/>
<many-to-one name="message" column="messageId" class="Message"/>
</class>
</hibernate-mapping>

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 ]
 
ranger
Posts: 17347
11
Mac IntelliJ IDE Spring
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I am not sure what your question is here. But I am guessing things just aren't working out how you expect them to be.

Anyway.

First, we have a CODE button below that will post your code and xml with indentations remaining. Without it, it becomes very difficult to read.

For bi-directional this is the exact mapping.
http://www.hibernate.org/hib_docs/v3/reference/en/html/associations.html#assoc-bidirectional-m21

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.

Mark
 
Rajan Nath
Greenhorn
Posts: 22
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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 ]
 
Mark Spritzler
ranger
Posts: 17347
11
Mac IntelliJ IDE Spring
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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

http://www.hibernate.org/hib_docs/v3/reference/en/html/events.html#objectstate-interceptors

Good Luck

Mark
 
Rajan Nath
Greenhorn
Posts: 22
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks once again Mark,Now I can go on right direction.
 
Consider Paul's rocket mass heater.
reply
    Bookmark Topic Watch Topic
  • New Topic