-->
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.  [ 11 posts ] 
Author Message
 Post subject: <many-to-one not-null="false"/> question
PostPosted: Tue Sep 02, 2003 12:44 am 
Newbie

Joined: Mon Sep 01, 2003 9:48 pm
Posts: 17
Hi all,

I'm having a question about a many-to-one relationship where not-null is set to be false. What does it affect to set not-null="false"? I'm trying to set null in there, and it doesn't seem to make a difference that the flag is there.

    There was 1 error:
    1) testCreate(test.JunkTest)java.lang.NullPointerException: attempted to save null
    at net.sf.hibernate.impl.SessionImpl.save(SessionImpl.java:580)
    at net.sf.hibernate.id.ForeignGenerator.generate(ForeignGenerator.java:73)
    at net.sf.hibernate.impl.SessionImpl.save(SessionImpl.java:599)
    at net.sf.hibernate.impl.SessionImpl.saveOrUpdate(SessionImpl.java:1202)
    at net.sf.hibernate.engine.Cascades$4.cascade(Cascades.java:88)
    at net.sf.hibernate.engine.Cascades.cascade(Cascades.java:258)
    at net.sf.hibernate.engine.Cascades.cascade(Cascades.java:341)
    at net.sf.hibernate.impl.SessionImpl.doSave(SessionImpl.java:693)
    at net.sf.hibernate.impl.SessionImpl.save(SessionImpl.java:605)
    at test.JunkTest.testCreate(JunkTest.java:50)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
    at com.intellij.rt.execution.junit.TextTestRunner.main(TextTestRunner.java:12)


Any ideas? I would think that SessionImpl.save() either shouldn't throw an exception if the schema says that null is okay, or ForeignGenerator.generate() should not be calling session.save() if null is okay.

What am I missing?

Thanks


Top
 Profile  
 
 Post subject:
PostPosted: Tue Sep 02, 2003 12:49 am 
Hibernate Team
Hibernate Team

Joined: Tue Aug 26, 2003 12:50 pm
Posts: 5130
Location: Melbourne, Australia
A class with the "foreign" id generation strategy requires a reference to its parent object (who it gets the id from) before it can be made persistent.


Actually, foreign id generation doesn't really make any sense for a nullable one-to-one.


Top
 Profile  
 
 Post subject:
PostPosted: Tue Sep 02, 2003 3:26 am 
Newbie

Joined: Mon Sep 01, 2003 9:48 pm
Posts: 17
It would seem to not make sense, but I am looking for polymorphism through an any pointer. This is the only way to get it, or so it seems.


Top
 Profile  
 
 Post subject:
PostPosted: Tue Sep 02, 2003 3:47 am 
Hibernate Team
Hibernate Team

Joined: Tue Aug 26, 2003 12:50 pm
Posts: 5130
Location: Melbourne, Australia
I don't understand. I don't see what "foreign" generation has to do with <any>. Any supports polymorphism just fine...


Top
 Profile  
 
 Post subject:
PostPosted: Tue Sep 02, 2003 3:48 am 
Newbie

Joined: Mon Sep 01, 2003 9:48 pm
Posts: 17
Ah, a misunderstanding. I think this is clean, but I am having another problem now. I have two objects, one using native generator, the other foreign. Cascade is set to "save-delete", which seems to be equivilent to "all" in this case.

When I save the native object, the foreign object is cascade saved. But the native generator needs to know the pk after the object has been saved. Because the foreign object is saved under cascade, it's a chicken/egg problem.

When I set cascade="none", the objects don't actually save out, but Hibernate doesn't throw an exception either.

Code:
        Inode i = new Inode();
        Folder f = new Folder();
        i.setObject(f);
        f.setInode(i);
        session.save(i);

        f.setName("root");
        session.save(f);

        session.close();


The mappings

Code:
<hibernate-mapping>
    <class
        name="modules.general.entity.Folder"
        dynamic-update="false"
        dynamic-insert="false"
    >

        <id
            name="id"
            column="id"
            type="java.lang.Long"
            unsaved-value="null"
        >
            <generator class="foreign">
                <param name="property">inode</param>
            </generator>
        </id>

        <one-to-one
            name="inode"
            class="modules.general.entity.Inode"
            cascade="none"
            outer-join="auto"
            constrained="false"
        />

        <property
            name="name"
            type="java.lang.String"
            update="true"
            insert="true"
            column="name"
        />

        <!--
            To add non XDoclet property mappings, create a file named
                hibernate-properties-Folder.xml
            containing the additional properties and place it in your merge dir.
        -->

    </class>

</hibernate-mapping>

Code:
<hibernate-mapping>
    <class
        name="modules.general.entity.Inode"
        dynamic-update="false"
        dynamic-insert="false"
    >

        <id
            name="id"
            column="id"
            type="java.lang.Long"
            unsaved-value="null"
        >
            <generator class="native">
            </generator>
        </id>

        <many-to-one
            name="folder"
            class="modules.general.entity.Folder"
            cascade="none"
            outer-join="auto"
            update="true"
            insert="true"
            column="folder"
        />

            <any name="object" id-type="long" cascade="save-update">
          <column name="clazz" length="100"/>
          <column name="gen_id"/>
        </any>

    </class>

</hibernate-mapping>


Any ideas? The architecture has to stay this way for a multitude of reasons that I can get into if necessary, but I'm wondering if this can be made to work.

Thanks...


Top
 Profile  
 
 Post subject:
PostPosted: Tue Sep 02, 2003 3:56 am 
Hibernate Team
Hibernate Team

Joined: Tue Aug 26, 2003 12:50 pm
Posts: 5130
Location: Melbourne, Australia
This is supposed to Just Work.


Have you had a look at src/net/sf/hibernate/test/XY.hbm.xml, which has an example of there _should_ be no chicken/egg problem.


I still don't completely understand where the <any> comes into this though ....


Top
 Profile  
 
 Post subject:
PostPosted: Tue Sep 02, 2003 4:00 am 
Hibernate Team
Hibernate Team

Joined: Tue Aug 26, 2003 12:50 pm
Posts: 5130
Location: Melbourne, Australia
Oh, I get it ;)


The "foreign" id generator goes on the side with constrained="true". Makes perfect sense if you think about it.


Hah!


Top
 Profile  
 
 Post subject:
PostPosted: Tue Sep 02, 2003 4:04 am 
Hibernate Team
Hibernate Team

Joined: Tue Aug 26, 2003 12:50 pm
Posts: 5130
Location: Melbourne, Australia
For clarity: It should work, even if you don't have a constrained="true" end of the association. Just move the "foreign" generator to the other class.

ie. the foreign key points to the generated id. This is absolutely necessary, of course.


Top
 Profile  
 
 Post subject:
PostPosted: Tue Sep 02, 2003 5:00 am 
Newbie

Joined: Mon Sep 01, 2003 9:48 pm
Posts: 17
gavin wrote:
For clarity: It should work, even if you don't have a constrained="true" end of the association. Just move the "foreign" generator to the other class.

ie. the foreign key points to the generated id. This is absolutely necessary, of course.


My goal was to have the generated ID of an Inode be the id of the Folder. Since there were going to be a lot of different item types, this would technically form a sequential UID. (If you remember my desire to expand Hibernate to handle several dozen subclasses, this is the hack around that...)

But as I read over what you wrote and thought about what I was doing, it wasn't strictly necessary. I suspect I am going to kick myself over it someday, but BushCo could start WWIII before I finish this thing (at the rate life has been letting me get things done lately), so maybe it's better to worry about that when it comes along. So I dropped the one-to-one, just went with what I have.

Thanks again!


Top
 Profile  
 
 Post subject:
PostPosted: Tue Sep 02, 2003 5:10 am 
Hibernate Team
Hibernate Team

Joined: Tue Aug 26, 2003 12:50 pm
Posts: 5130
Location: Melbourne, Australia
I have a funny feeling that all you needed to do was set constrained="true" instead of constrained="false".

Can a Folder exist w/o an Inode?

It doesn't sound like it!



I think its that simple :)


Top
 Profile  
 
 Post subject: foreign
PostPosted: Mon Sep 15, 2003 6:14 pm 
Beginner
Beginner

Joined: Wed Aug 27, 2003 6:44 pm
Posts: 20
foreign key generation for one-to-one releation is not explained it properly in the doc.can someone give me an example how to use that.

what do you meant by constrained="true"?


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 11 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.