-->
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.  [ 6 posts ] 
Author Message
 Post subject: Dynamic Entities
PostPosted: Mon Nov 06, 2006 8:51 am 
Newbie

Joined: Mon Nov 06, 2006 8:31 am
Posts: 3
Hi all, I've been using Hibernate for a short while on defined java objects but would like to know how I could use it where the fields required for a particular entity are liable to change. I want to implement a feature where a user can add a field name and corresponding data type.

For example, if I have a person object which has the following properties:
birthDate
firstName
lastName

which are mapped to a people table, a user could then add a gender field of type String.

Does anyone have any ideas as to how this could be done?

regards,
Marcus


Top
 Profile  
 
 Post subject:
PostPosted: Mon Nov 06, 2006 6:00 pm 
Regular
Regular

Joined: Sat Jan 07, 2006 8:30 pm
Posts: 68
Use dynamic instantiation
You can define a class to represent
each row of results and use the HQL select new construct:

select new BidRow( item.id, item.description, bid.amount )
from Item item join item.bids bid

This is just for getting data out combining many entities.


Top
 Profile  
 
 Post subject:
PostPosted: Tue Nov 07, 2006 4:56 am 
Newbie

Joined: Mon Nov 06, 2006 8:31 am
Posts: 3
Sorry, not sure if I explained myself properly, the problem is that I have classes that represent a row of data, ie a Person class, which may have 4 attributes, mapped to 4 columns in a corresponding database table. If the application is running and a user wishes to record some extra information regarding a person then the database side is easily implemented by code that adds an extra column to the table, but what about the Java class?

In theory the application could open the .java source file, add a new attribute and get / set methods, recompile the source and then use the modified class, but this seems unnecessarily complicated... or is it?

Is there a simpler way of providing this functionality?


Top
 Profile  
 
 Post subject:
PostPosted: Tue Nov 07, 2006 5:18 am 
Senior
Senior

Joined: Sat Nov 27, 2004 4:13 am
Posts: 137
You can use DynamicModels (4.4. Dynamic models in Hibernate 3.2 docs).
So there will be no class for entities, and you could use a Map for entities.

And it is possible to change the hibernate mapping configurations using SessionFactory's metadata.

_________________
don't forget to credit!

Amir Pashazadeh
Payeshgaran MT
پايشگران مديريت طرح
http://www.payeshgaran.co
http://www.payeshgaran.org
http://www.payeshgaran.net


Top
 Profile  
 
 Post subject:
PostPosted: Tue Nov 07, 2006 11:20 am 
Newbie

Joined: Mon Nov 06, 2006 8:31 am
Posts: 3
Thanks for the info pasha.
I have implemented it so there is a java class backing the data with some 'fixed' properties and a custom properties map...

Code:
public class Person {
   // Instance properties
   protected Long id;
   protected String foreNames;
   protected String surname;
   protected String title;
   
   protected Map customProperties;   
   
   // Instance methods.... get, set etc.
}


and in the mapping document...
Code:
<class name="Person" table="PEOPLE">
   <id name="id" column="id">
      <generator class="native" />
   </id>
   <property name="title" />
   <property name="foreNames" />
   <property name="surname" />
   <dynamic-component name="customProperties">
      <property name="gender" type="string" />
      <property name="age" type="integer" />
   </dynamic-component>
</class>

If a user wishes to record another field for a Person, then the application simply adds another property element to the dynamic-component element of that class in the mapping document, and hibernate creates the relevant columns in the database.


Top
 Profile  
 
 Post subject:
PostPosted: Sat Nov 11, 2006 6:10 pm 
Newbie

Joined: Thu Aug 25, 2005 5:33 pm
Posts: 10
Hi,

I read the documentation about the dynamic models. My question is, can they only be used with components, or can they be used with first class domain-objects that are backed by a database table?


row


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