-->
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.  [ 7 posts ] 
Author Message
 Post subject: many-to-one relationship from a joined-subclass possible?
PostPosted: Wed Jun 23, 2004 11:51 am 
Newbie

Joined: Mon Jun 21, 2004 2:38 pm
Posts: 3
Usuf Arif: uarif@tiaa-cref.org

I have posted this two days ago, but no replies yet, may be I did not explain it properly. Will try again.

We have a scenario where Child extends Parent and Child has many-to-one relationship with another table. When I try to insert a Child, hibernate complains that it can not insert null value in the foreign key of the Child table. I have written code exactly as the documentation preaches, and forum is the only place I can get the answer. Any help will be greatly appreciated.

Plan extends BasePlan.
Plan has many-to-one relationship with PlanLkup.

BasePlan.hbm.xml file has a <joined-subclass> attribute for Plan, inside this attribute, there is many-to-one relationship with PlanLkup.

================BasePlan.hbm.xml=================
<class
name="persistence.BasePlan"
table="BASE_PLAN"
>
<!--................Other attributes of BasePlan table here ........-->
<joined-subclass name="persistence.inst7307.Plan"
table="PLAN">
<key column="PLAN_ID"/>

<many-to-one
name="PlanLkup"
class="persistence.inst7307.lookup.PlanLkup"
not-null="true">
<column name="LOOK_UP_ID" />
</many-to-one>
</joined-subclass>

</class>
=====================================

================PlanLkup.hbm.xml=========
<hibernate-mapping>

<class
name="persistence.inst7307.lookup.PlanLkup"
table="PLAN_LKUP"
>

<id
name="lookUpId"
type="int"
column="LOOK_UP_ID"
>
<generator class="sequence" />
</id>
<!--........................Other properties.......................................-->
<!-- associations -->
<!-- bi-directional one-to-many association to Plan -->
<set
name="plans"
lazy="true"
inverse="true"
>
<key>
<column name="LOOK_UP_ID" />
</key>
<one-to-many
class="persistence.inst7307.Plan"
/>
</set>

</class>
</hibernate-mapping>
=====================================



Plan has a many-to-one relationship with PlanLkup with LOOK_UP_ID.

In the Java code, I have established bidirectional relationship between Plan and PlanKkup as follows:

/****************************************************

plan.setPlanLkup(planLkup); //chlild.set(Parent)
planLkup.getPlans().add(plan); //parent.getChildren.add(child)

****************************************************/

The above code is supposed to establish relationship between Plan and PlanLkup with LOOK_UP_ID. PlanLkup is not null. I was expecting that hibernate will pickup the LOOK_UP_ID from the PlanLkup and insert it into the Plan.

//////////////////////Error I get://///////////////////////////////////////////
150062 [main] ERROR net.sf.hibernate.util.JDBCExceptionReporter - ORA-01400: cannot insert NULL into ("CCS01"."PLAN"."LOOK_UP_ID")
///////////////////////////////////////////////////////////////////


Any recommendations are highly appreciated!
Email: uarif@tiaa-cref.org


Top
 Profile  
 
 Post subject:
PostPosted: Wed Jun 23, 2004 6:37 pm 
Regular
Regular

Joined: Tue Oct 07, 2003 10:20 am
Posts: 77
Could you please post your Java code for the getters and setters of the ID properties, as well as the bidirectional relationship methods, of the Plan and PlanLkup beans. Also the code you use to save the Plan object would be useful (opening of Hibernate session, etc).

I'm assuming you've saved the parent object already, since you don't have any cascades set on your relationship.

Also, what version of Hibernate are you using, and what database/version (see the big red box when you post for a list of useful things to include when asking for help).


Top
 Profile  
 
 Post subject:
PostPosted: Thu Jun 24, 2004 4:23 am 
Regular
Regular

Joined: Tue Oct 07, 2003 10:20 am
Posts: 77
Hmm, sorry - I did reply again to you last night, but my post seems to have gone astray.

The problem you have is with your mapping. By stating that your constraining column of the many-to-one is in fact your primary key column of table A, Hibernate will always try and resolve an entity B since the many-to-one column will never be empty.

As this relationship is a one-to-one relationship, you can fix this problem by mapping it is as such. You will need a foreign key placed on your primary key column of table B, to the primary key column of table A. If you're using the schema-export tool, then this can be achieved by making the one-to-one relationship bidirectional, and making the link from B->A constrained.

The mapping for table A would be:

Code:
<class name="persistence.BasePlan" table="BASE_PLAN">
<!--................Other attributes of BasePlan table here ........-->
    <joined-subclass name="persistence.inst7307.Plan" table="PLAN">
        <key column="PLAN_ID"/>

        <one-to-one name="PlanLkup" class="persistence.inst7307.lookup.PlanLkup" cascade="none" constrained="false" not-null="true"/>
    </joined-subclass>
</class>


Top
 Profile  
 
 Post subject:
PostPosted: Thu Jun 24, 2004 4:25 am 
Regular
Regular

Joined: Tue Oct 07, 2003 10:20 am
Posts: 77
Oh ffs, ignore that post completely - wrong topic.


Top
 Profile  
 
 Post subject:
PostPosted: Thu Jun 24, 2004 4:53 am 
Regular
Regular

Joined: Tue Oct 07, 2003 10:20 am
Posts: 77
Ok, the only way I can get the error your obtaining is if the returned PlanLkup from Plan is null. Have a good check at your setter and getter methods for this property and make sure that the object returned from your getPlanLkup() method isn't returning null.


Top
 Profile  
 
 Post subject:
PostPosted: Thu Jun 24, 2004 4:46 pm 
Newbie

Joined: Mon Jun 21, 2004 2:38 pm
Posts: 3
Thanks a bunch for the replies. The forum is the best place to find complicated problems.

I was getting null from the Plan for getPlanLkup.
I have not yet tried with the settings that you suggested, but will give it a try shortly and let know the results. Appreciated.

Usuf Arif


Top
 Profile  
 
 Post subject:
PostPosted: Fri Jun 25, 2004 4:33 am 
Regular
Regular

Joined: Tue Oct 07, 2003 10:20 am
Posts: 77
As I said, I'd ignore my post which has the mapping in - that was just me getting confused and posting on the wrong topic.


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