This week's book giveaway is in the Servlets forum.
We're giving away four copies of Murach's Java Servlets and JSP and have Joel Murach on-line!
See this thread for details.
The moose likes Object Relational Mapping and the fly likes complex one-to-many Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Databases » Object Relational Mapping
Bookmark "complex one-to-many" Watch "complex one-to-many" New topic
Author

complex one-to-many

Rajan Nath
Greenhorn

Joined: Nov 27, 2007
Posts: 22
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 ]
Mark Spritzler
ranger
Sheriff

Joined: Feb 05, 2001
Posts: 17249
    
    6

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


Perfect World Programming, LLC - Two Laptop Bag - Tube Organizer
How to Ask Questions the Smart Way FAQ
Rajan Nath
Greenhorn

Joined: Nov 27, 2007
Posts: 22
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
Sheriff

Joined: Feb 05, 2001
Posts: 17249
    
    6

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

Joined: Nov 27, 2007
Posts: 22
Thanks once again Mark,Now I can go on right direction.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: complex one-to-many
 
Similar Threads
Auto Generator in composite key
Illegal attempt to associate a collection with two open sessions
HBM mapping Problem
updating association table
Hibernate causing Concurrent Modification Exception