-->
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.  [ 4 posts ] 
Author Message
 Post subject: Hibernate2 one-to-many non-nullable foreign key
PostPosted: Mon Nov 14, 2005 12:51 am 
Newbie

Joined: Fri Oct 28, 2005 10:54 pm
Posts: 12
Running into a problem documented in the reference documentation but with a slight difference. Hoping someone can point me in the right direction!

Chapter 6 warning: Very Important Note: If the <key> column of a <one-to-many> association is declared NOT NULL, Hibernate may cause constraint violations when it creates or updates the association. To prevent this problem, you must use a bidirectional association with the many valued end (the set or bag) marked as inverse="true". See the discussion of bidirectional associations later in this chapter.

And Chapter 16's solution:
http://www.hibernate.org/hib_docs/refer ... hild-bidir

My slight difference is that key to the many end is a composite-id not just a single column. How do I code the mapping so that in the many end where the suggestion documented is to include <many-to-one name="???" column="template_id" not-null="true" /> ???

Hibernate version:Hibernate 2

Mapping document for parent - Template:
Code:
<?xml version="1.0"?>

<!DOCTYPE hibernate-mapping PUBLIC
    "-//Hibernate/Hibernate Mapping DTD 2.0//EN"
    "http://hibernate.sourceforge.net/hibernate-mapping-2.0.dtd">

<hibernate-mapping>
   <class name="Template"
      table="template" dynamic-update="false" dynamic-insert="false">

      <id name="id" column="template_id" type="long" unsaved-value="0">
         <generator class="increment"></generator>
      </id>

       <property name="templateName" column="template_name" type="string" not-null="true" length="32"/>
       
      <set name="templatePreferences" lazy="false" inverse="true" cascade="all-delete-orphan" sort="unsorted">
         <key column="template_id"/>
         <one-to-many class="TemplatePrefXrefBean" />
      </set>

   </class>



Mapping document for TemplatePrefXrefBean:
Code:
</hibernate-mapping>

<?xml version="1.0"?>

<!DOCTYPE hibernate-mapping PUBLIC
    "-//Hibernate/Hibernate Mapping DTD 2.0//EN"
    "http://hibernate.sourceforge.net/hibernate-mapping-2.0.dtd">

<hibernate-mapping>
   <class name="TemplatePrefXrefBean" table="template_pref_xref"
      dynamic-update="false" dynamic-insert="true">

      <composite-id name="key" class="TemplatePrefXrefKey">
         <key-property name="templateId" type="long"  column="template_id"  />
         <key-property name="preferenceId" type="long" column="preference_id" />
      </composite-id>
      <property name="preferenceValue" type="string"
         update="true" insert="true" column="preference_value" />
         
     <!-- how can I get this to work?   <many-to-one name="key" column="template_id" not-null="true" />  -->
         
   </class>

</hibernate-mapping>



Name and version of the database you are using: MySQL 5.0.12

When I uncomment the <many-to-one> mapping in the 2nd file, I get an error: An association from the table template_pref_xref refers to an unmapped class: TemplatePrefXrefKey.

When I moved the two key fields into the TemplatePrefXrefBean class and then referred to them directly, a different error occured, complaining about
an unmapped class 'long' since both keys (templateId and preferenceId) are defined as long.


Top
 Profile  
 
 Post subject:
PostPosted: Thu Nov 17, 2005 11:57 am 
Newbie

Joined: Fri Oct 28, 2005 10:54 pm
Posts: 12
Since my initial post has not generated any responses,
let me try asking this question another way:

How would experienced users map the following:

we are creating new templates that contain a reference to existing
preferences and an 'override' for a preference value.
The preferences all exist and we will not be dynamically
adding new preference or trying to managing the templates
that include a certain preference (not really bi-directional?)

I think the preference_value in the xref table
throws kink in the normal many-to-many join table concept
because of the extra field.

Can a mapping be setup such that when we create a new
template, with a set of new templatePrefXref objects
that we can make one call to hibernate ( object.create() )
to have the new template row inserted and the new associated
template_pref_xref rows inserted as well?

Currently we have to create the template with an empty set,
then iterate thru a set of templatePrefXref objects updating
those with the correct new template_id, put the set back
into the template object and call object.store().


Code:
|--------------|         |----------------------|           |--------------| 
|   template   |         |  template_pref_xref  |           |  preference  | 
|--------------|         |----------------------|           |--------------| 
| template_id  |  <-->   | template_id          |  <-->     | preference_id| 
| (other cols) |         | preference_id        |           | (other cols) | 
|              |         | preference_value     |           |              | 
|--------------|         |                      |           |--------------| 
                         |----------------------| 

[/code]


Top
 Profile  
 
 Post subject:
PostPosted: Sat Nov 19, 2005 12:49 am 
Beginner
Beginner

Joined: Sat Oct 29, 2005 2:05 am
Posts: 21
Location: Kansas City, KS
From your original post, I don't see the association between TemplatePrefXrefBean and Preference. Were you just trying to "delink" them with preferenceId?

I'm having to guess here, since I find the mapping language very hard to read "naturally" and I just deal with it one PITA at a time. However, I do have something similar where I use composite-element instead. How would this work for you?

Code:
<set name="templatePreferences">
   <key column="template_id"/>
   <composite-element class="TemplatePrefXrefBean" >
      <property name="preferenceId" not-null="false"/>
      <property name="preferenceValue" type="string"/>
   </composite-element>
</set>


(I just trashed some attributes I didn't want to copy. Put them back :-)

That still doesn't deal with the TemplatePrefXrefBean <-> Preference association, but it does give you a PK of template_id+preferenceId.[/code]. A step in the right direction, perhaps :-?

BTW, this is "simply" a many-to-many association with additional atributes on the association class. I think there is something in the advanced FAQ on that.


Top
 Profile  
 
 Post subject:
PostPosted: Sat Nov 19, 2005 12:54 am 
Beginner
Beginner

Joined: Sat Oct 29, 2005 2:05 am
Posts: 21
Location: Kansas City, KS
>I think there is something in the advanced FAQ on that.

http://www.hibernate.org/118.html#A11

Perhaps that might help you out with the TemplatePrefXrefBean <-> Preference.


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