-->
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.  [ 8 posts ] 
Author Message
 Post subject: dynamic mapping
PostPosted: Mon Feb 21, 2005 7:52 pm 
Newbie

Joined: Tue Jan 18, 2005 10:41 pm
Posts: 15
Hi

I have a situation where the system administrator can add extra columns to a table whenever necessary. A set of “meta data” tables are used to define what the columns represent.

For example the administrator may define an extra column for the client table called “firstContact”. He would then add information to the “meta data” tables stating that the “firstContact” column on the client table represents the ID code of the user who made the first contact with the client, and can be linked to the table that defines the user.

I was thinking of representing these extra fields as a collection of “custom field” objects owned by my Client object. Each custom field object would map to a column in the table (instead of the normal row in a table), The actual mapping would be determined at run time by reading the meta data, and then creating a mapping data (via the classes in net.sf.hibernate.mapping)

Is this the right approach?

Is there a simpler way of doing this?

Or am I expecting too much from hibernate?



(Technically I believe that loading a set of components from a single row is called a “pivot” table.)


Top
 Profile  
 
 Post subject:
PostPosted: Mon Feb 21, 2005 9:10 pm 
Hibernate Team
Hibernate Team

Joined: Tue Sep 09, 2003 2:10 pm
Posts: 3246
Location: Passau, Germany
You can also look at dynamic-component and dynamic-class (this one in H3 only) - Not very well documented, but useful


Top
 Profile  
 
 Post subject:
PostPosted: Mon Feb 21, 2005 11:04 pm 
Newbie

Joined: Tue Jan 18, 2005 10:41 pm
Posts: 15
Forgetting for the moment that this has to be done at run time :)

I have a table “Client” with the following columns

Code:
ID                      integer      - Identity column
FistContact          integer      - ID value of the first user who contacted the client
FirstContactDate  Date          -Date  of the first contact with the client.


I which to map to the following class
Code:
Public class Client {
   Private int ID;
   Set field;
}


Where Fields will contain a list of all the custom fields. So I have an interface
Code:
Public ICustomField{
   Private Object value;
}
and I create a class for each type of custom field.

My mapping looks like
Code:
<hibernate-mapping>

     <class name="client " table="client "  >
     
     
      <id name="ID ">
         …
          </id > 

      <set name = "fields" >
This is where I have the problem. How do I add two elements, one for FirstContact and the second for FirstContactDate   
                            </set>
     </class>


</hibernate-mapping>



It would have been nice if the design had used a simple one to many table, but instead a pivot table was used!


Top
 Profile  
 
 Post subject:
PostPosted: Thu Mar 03, 2005 8:02 am 
Newbie

Joined: Wed Mar 02, 2005 11:01 am
Posts: 5
I have the exact same problem, did you find a solution for this?


Top
 Profile  
 
 Post subject:
PostPosted: Thu Mar 03, 2005 4:00 pm 
Newbie

Joined: Tue Jan 18, 2005 10:41 pm
Posts: 15
I followed the man's advice and used hibernate V3 which supports dynamic classes and components.

I then wrote some code to turn the map that hibernate produces into an array of usefull field classes.

Beware, use habernate V3 beta, not V3rc1 as dynamic mapping in 3rc1 is currently not working (a known issue).


Top
 Profile  
 
 Post subject:
PostPosted: Fri Mar 04, 2005 2:12 am 
Newbie

Joined: Wed Mar 02, 2005 11:01 am
Posts: 5
Thanks, I'll give that a go.


Top
 Profile  
 
 Post subject:
PostPosted: Mon Mar 07, 2005 4:00 am 
Newbie

Joined: Wed Mar 02, 2005 11:01 am
Posts: 5
Hi,

Sorry for bothering again. But I am still not really sure what to do.

Here is my mapping file :
Code:
<hibernate-mapping>
    <dynamic-class entity-name="TestObject" table="test_object">
        <id name="id" type="string">
            <generator class="uuid.hex"/>
        </id>
        <property name="field1" type="string" />
    </dynamic-class>
</hibernate-mapping>


The map that is produces contains values for id and field1, but our application can insert another column into the table, that is not reflected in the map until we add it to the mapping file.

Is there any way to specify that it should load all column values into the map?

Thanks.[/code]


Top
 Profile  
 
 Post subject:
PostPosted: Mon Mar 07, 2005 3:58 pm 
Newbie

Joined: Tue Jan 18, 2005 10:41 pm
Posts: 15
serlank wrote:

Is there any way to specify that it should load all column values into the map?



Not that I know of. We run some SQL to get the metadata of the tables and their foreign keys and then generate a hibernate mapping DOM. This DOM is then added to the hiberante configuration object and a new SessionFactor is then built.


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