-->
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.  [ 5 posts ] 
Author Message
 Post subject: Maps & Composite Keys
PostPosted: Thu Dec 18, 2003 10:25 pm 
Newbie

Joined: Thu Dec 11, 2003 7:03 pm
Posts: 3
I am having trouble implementing a map in Hibernate. I have checked out the message board, but have not found a close enough match for what I am trying to do. I am working from an existing Oracle DB, so I can not use Hibernate to derive the DDL. Here is my setup:

3 Java Classes
-------------------

UsersTO
- private long sequenceId;
- private String userName;
- private PreferencesTO preferences;

PreferencesTO
- private Map panelsettings;
- private String defaultViewName;

PanelSettingsTO
- private boolean minimized;


4 Tables
-------------------
u_users
- u_id NUMBER(6);
- user_name VARCHAR2(18);

u_preferences
- u_id NUMBER(6);
- default_view_name VARCHAR2(24);

u_panel_settings
- u_id NUMBER(6);
- panel_id NUMBER(4);
- minimized CHAR(1);
- primary key = u_id & panel_id

u_panels
- panel_id NUMBER(6);
- panel_name VARCHAR(17);


hbm.xml
---------------------------
<class name="UserTO" table="u_users"
<id name="sequenceId" column="u_id" type="long" unsaved-value="0">
<generator class="sequence">
<param name="sequence">UID_SEQ</param>
</generator>
</id>
<property name="userName" column="user_name" type="string"/>
<one-to-one name="peferences" class="PeferencesTO" cascade="all"/>
</class>

<class name="PreferencesTO" table="u_preferences"
<id column="u_id" type="long" unsaved-value="0">
<generator class="foreign"></generator>
</id>
<property name="defaultViewName" column="default_view_name" type="string"/>
<map name="panelSettings">
<key column="u_id">
<index column="panel_id" type="long"/>
<composite-element class="PanelSettingsTO"/>
</map>
</class>

<class name="PanelSettingsTO" table="u_panel_settings"
<composite id>
<key-property column="u_id">
<key-property column="panel_id">
</composite id>
<property name="minimized" column="minimized" type="yes_no"/>
</class>


I am unsure of a couple of aspects of the hibernate mapping that I have shown above:

1. The <map> tag.
2. The way that I used the <composite-id> tag in PanelSettingsTO

I when I run the application I get a message saying "ClassNotFound- org/omgd/DMap" . I think this is the first of many errors... I looked in my hibernate jar file, and I do have the Map class definition.

Thank you for any help that you can provide,
Charles


Top
 Profile  
 
 Post subject: Re: Maps & Composite Keys
PostPosted: Fri Dec 19, 2003 12:31 pm 
Hibernate Team
Hibernate Team

Joined: Sun Sep 14, 2003 3:54 am
Posts: 7256
Location: Paris, France
muddywaters wrote:
ClassNotFound- org/omgd/DMap

Add odmg.jar in your CP.

_________________
Emmanuel


Top
 Profile  
 
 Post subject: Thanks
PostPosted: Fri Dec 19, 2003 6:19 pm 
Newbie

Joined: Thu Dec 11, 2003 7:03 pm
Posts: 3
Thank you Emmanuel,

Momentary lapse of reason. I was so unsure of the composite-id and <map> stuff, I didn't think to use a little common sense. I appreciate the help and am making progress with the composite-id and mapping stuff.

Thanks,
Charles


Top
 Profile  
 
 Post subject: Re: Thanks
PostPosted: Mon Dec 22, 2003 4:33 am 
Hibernate Team
Hibernate Team

Joined: Sun Sep 14, 2003 3:54 am
Posts: 7256
Location: Paris, France
muddywaters wrote:
making progress with the composite-id

I recommend you not to make too much progress on composite-id. Prefer surrogate keys rather than business ones. Hibernate mapping will be far more easier, and your DB design will be far more stable (surrogate keys don't change their mind, users/techies do).

_________________
Emmanuel


Top
 Profile  
 
 Post subject: Maps
PostPosted: Mon Dec 22, 2003 4:23 pm 
Newbie

Joined: Thu Dec 11, 2003 7:03 pm
Posts: 3
Emmanuel,

Point well taken.... but the DB came before the current app and I don't have the power to change it!

I am close on the mapping I want to do, but not quite there. Here is what I have so far:


3 Java Classes
-------------------

UsersTO
- private long sequenceId;
- private String userName;
- private PreferencesTO preferences;

PreferencesTO
- private Map panelsettings;
- private String defaultViewName;

PanelSettingsTO
- private boolean minimized;


4 Tables
-------------------
u_users
- u_id NUMBER(6);
- user_name VARCHAR2(18);

u_preferences
- u_id NUMBER(6);
- default_view_name VARCHAR2(24);

u_panel_settings
- u_id NUMBER(6);
- panel_id NUMBER(4);
- minimized CHAR(1);
- primary key = u_id & panel_id

u_panels
- panel_id NUMBER(6);
- panel_name VARCHAR(17);


hbm.xml
---------------------------
Code:
<class name="UserTO" table="u_users"
  <id name="sequenceId" column="u_id" type="long" unsaved-
                   value="0">
    <generator class="sequence">
      <param name="sequence">UID_SEQ</param>
    </generator>
  </id>
  <property name="userName" column="user_name" type="string"/>
  <one-to-one name="peferences" class="PeferencesTO" cascade="all"/>
</class>

<class name="PreferencesTO" table="u_preferences"
  <id column="u_id" type="long" unsaved-value="0">
    <generator class="foreign"></generator>
  </id>
  <property name="defaultViewName" column="default_view_name"
                  type="string"/>
  <map name="panelSettings" table="u_panel_settings">
    <key column="u_id">
    <index column="panel_id" type="long"/>
    <composite-element class="PanelSettingsTO">
      <property name="minimized" type="yes_no"/>
    </composite-element>
  </map>
</class>

<class name="PanelSettingsTO" table="u_panel_settings"
  <composite id name="panelSettings" class="PanelSettingsTO_PK">
    <key-many-to_one name="panel" class="PanelTO">
      <column name="panel_id"/>
    </key-many-to-one>
    <key-many-to_one name="user" class="UserTO">
      <column name="u_id"/>
    </key-many-to-one>
  </composite id>
  <property name="minimized" column="minimized" type="yes_no"/>
  <many-to_one name="panelRef" class="PanelTO" column="panel_id"/>
</class>

<class name="PanelsTO" table="u_panels"
  <id name="paneld" column="panel_id" type="long" unsaved-
                   value="0">
    <generator class="sequence">
      <param name="sequence">PANEL_ID_SEQ</param>
    </generator>
  </id>
  <property name="panelName" column="panel_name" type="string"/>
</class>




The code/mappings above allow me to use the panel_id as a key into the the panelSettings Map. What I would like to do, actually what the app developer wants to do ;), is to use panel_name as the key. The problem is that panel_name is in another table (U_PANELS). In straight SQL, the answer is easy, just join U_PANEL_SETTINGS and U_PANELS on PANEL_ID, but I have not been able to do so with the hibernate mapping. I can get very close. I can either create the java.util.Map with panel_id as the key, or I can get hibernate to try and use panel_name but I get an error because it is trying to find the field in U_PANEL_SETTINGS. The question, is it possible to use a value from another table as the key into a <map>?

Thanks again,
Charles


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