-->
These old forums are deprecated now and set to read-only. We are waiting for you on our new forums!
More modern, Discourse-based and with GitHub/Google/Twitter authentication built-in.

All times are UTC - 5 hours [ DST ]



Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 3 posts ] 
Author Message
 Post subject: one-to-many and key-many-to-one problem
PostPosted: Tue Jun 08, 2004 6:38 am 
Newbie

Joined: Tue Jun 08, 2004 6:06 am
Posts: 3
Hi all,

First, my hibernate mapping file:

Code:
<hibernate-mapping>
    <class name='eg.SystemMessage' table='SystemMailbox'>
        <id name='message_id' type='int' column='message_id' unsaved-value='-1'>
            <generator class='sequence'>
                <param name='sequence'>SEQ_MSGTOOL_MSGID</param>
            </generator>
        </id>
        <property name='sender' column='sender' type='string' unique='false' not-null='true'/>
        <property name='subject' column='subject' type='string' unique='false' not-null='true'/>
        <property name='body' column='body' type='string' unique='false' not-null='true'/>
        <property name='publish_date' column='publish_date' type='date' unique='false' not-null='true'/>
        <set name='receivers'
             lazy='true'
             inverse='true'
             cascade='all'>
            <key column='message_id'/>
            <one-to-many class='eg.UserAccountMessage'/>
        </set>
    </class>
   
    <class name='eg.UserAccountMessage' table='OwnUserMailbox'>
        <composite-id>
            <key-property name='user_id' column='user_id' type='string'/>
            <key-many-to-one name='message' class='eg.SystemMessage'>
                <column name='message_id'/>
            </key-many-to-one>
        </composite-id>
       
        <property name='scope' column='scope' type='string' unique='false' not-null='true'/>
        <property name='scope_id' column='scope_id' type='string' unique='false' not-null='false'/>
        <property name='visited' column='visited' type='integer' unique='false' not-null='false'/>

    </class>
</hibernate-mapping>



And my java code:

Code:
        SystemMessage msg = new SystemMessage();
        msg.setBody("The message body");
        msg.setMessage_id(-1);
        msg.setPublish_date(new Date(System.currentTimeMillis()));
        msg.setSender("the_sender");
        msg.setSubject("Message subject");
       
        UserAccountMessage umsg = new UserAccountMessage();
       
        umsg.setScope("system");
        umsg.setUser_id("user2");
        umsg.setMessage(msg);
       
        UserAccountMessage umsg_1 = new UserAccountMessage();
       
        umsg_1.setScope("system");
        umsg_1.setUser_id("user1");
        umsg_1.setMessage(msg);
       
        msg.getReceivers().add(umsg);
        msg.getReceivers().add(umsg_1);
       
        Session session = access.getSession();
       
        Transaction tx = null;
        try {
            tx = session.beginTransaction();
           
             //
            session.save(msg);
            /*At this point msg is saved, but the content of receivers is not saved*/
            session.flush();
           
            tx.commit();
           
        }catch (HibernateException he) {
                 ....
         }



And finally the problem.
When the statement session.save() is executed the info for SystemMailbox table is saved correctly (variable msg), but hibernate doesn't save the UserAccountMessage info (the content of attribute receivers in the variable msg).

Any ideas??


Thanks in advance,


[/code]


Top
 Profile  
 
 Post subject:
PostPosted: Tue Jun 08, 2004 7:14 am 
Newbie

Joined: Tue Jun 08, 2004 6:06 am
Posts: 3
More details:

Hibernate exectute the following statements,

insert into SystemMailbox (sender, subject, body, publish_date, message_id) values (?, ?, ?, ?, ?)
update OwnUserMailbox set scope=?, scope_id=?, visited=? where user_id=? and message_id=?

Why doesn't execute the insert into OwnUserMailbox??????


Top
 Profile  
 
 Post subject:
PostPosted: Tue Jun 08, 2004 10:20 am 
Newbie

Joined: Tue Jun 08, 2004 6:06 am
Posts: 3
Hi all,

Finally, i find the solution: there was as simple as put the attribute unsaved-value in the composite-id definition.
The finally mapping for the composite-id looks like this:


<composite-id unsaved-value='any'>
<key-property name='user_id' column='user_id' type='string'/>
<key-many-to-one name='message' class='eg.SystemMessage'>
<column name='message_id'/>
</key-many-to-one>
</composite-id>


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 3 posts ] 

All times are UTC - 5 hours [ DST ]


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum

Search for:
© Copyright 2014, Red Hat Inc. All rights reserved. JBoss and Hibernate are registered trademarks and servicemarks of Red Hat, Inc.