-->
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: Runtime Manipulation of <dynamic-component>
PostPosted: Tue Jun 12, 2007 5:38 am 
Newbie

Joined: Tue Jun 12, 2007 5:22 am
Posts: 3
Hi!
Chapter 8.5 http://www.hibernate.org/hib_docs/v3/reference/en/html_single/#components-dynamicin hibernate documentation about dynamic-component says:
Quote:
Runtime manipulation of the mapping document is also possible, using a DOM parser. Even better, you can access (and change) Hibernate's configuration-time metamodel via the Configuration object.


Is there an example or a more detailed explanation of the runtime manipulation?
How can i manipulate the Configuration object at runtime so that i can add a new column/property to a dynamic-component?

Hibernate version: 3.2.4 SP1

Mapping documents:
<hibernate-mapping>
<class name="sample.DynamicFieldCollection" table="DynamicFieldCollection">
<id name="id" column="Id" type="string">
<generator class="uuid.hex"/>
</id>
<one-to-one name="employee" class="sample.SampleEmployee"/>
<dynamic-component name="dynamicFields">
<property name="field_1" type="string" column="field_1"/>
</dynamic-component>
</class>

<class name="sample.SampleEmployee" table="Employee">
<id name="id" column="Id">
<generator class="uuid.hex"/>
</id>
<property name="title" column="Name"/>
</class>
</hibernate-mapping>

thomas


Top
 Profile  
 
 Post subject:
PostPosted: Mon Jul 30, 2007 11:53 am 
Newbie

Joined: Thu Jul 12, 2007 4:45 am
Posts: 6
Location: Germany
Good afternoon,

this was questioned several times but I can't find any answer, neither here nor in the entire internet. Is there any further documentation or example code available in the meantime?

I have understood from the former postings:

I can prepare a class for adding dynamic attributes by specifying an empty "dynamic component" section in my *.hbm.xml file...

Code:
  <class name="Northwind.Order, Northwind" table="OrderInformation">
   <id name="Id" type="Int32" column="Id">
      <generator class="assigned" />
   </id>

...

   <dynamic-component name="UserAttributes">
      </dynamic-component>
   </class>


...and implementing a map for mapping / accessing the dynamic attributes from my class (C# actually):

Code:
private System.Collections.IDictionary m_userAttributes;

public virtual System.Collections.IDictionary UserAttributes {
   get {
      return m_userAttributes;
   }
   set {
      m_userAttributes = value;
   }
}


Now what exactly do I have to do here...

Code:
Configuration cfg = new Configuration();
cfg.AddAssembly( "Northwind" );

// ???????????????????????????????

ISessionFactory factory = cfg.BuildSessionFactory();


...to map an additional column "FOOBAR" of type String to the class prepared with the map?

Best regards,

Gabriel Schmidt


Top
 Profile  
 
 Post subject:
PostPosted: Fri Aug 10, 2007 5:07 am 
Newbie

Joined: Thu Jul 12, 2007 4:45 am
Posts: 6
Location: Germany
Meanwhile I found some Java code fragments here. I tried to translate them to C#. My code now looks like this:

*.hbm.xml
Code:
<class name="Northwind.Order, Northwind" table="OrderInformation">
   <id name="Id" type="Int32" column="Id">
      <generator class="assigned" />
   </id>
   <dynamic-component name="UserAttributes">
   </dynamic-component>
</class>


*.cs
Code:
      private static Property addDynamicAttribute(
         String attributeName, String columnName, PersistentClass persistentClass ) {

         // Create a new db column specification.
         Column column = new Column( NHibernate.Type.TypeFactory.GetStringType( 100 ), 0 );
         column.Name = columnName;

         // Create a simple value that contains the column and will be set to the property as information.
         SimpleValue simpleValue = new SimpleValue( persistentClass.Table );
         simpleValue.AddColumn( column );
         simpleValue.Type = NHibernate.NHibernateUtil.String;

         // Create a new property.
         Property property = new Property();
         property.Name = attributeName;
         property.Value = simpleValue;

         return property;
      }

      static void Main( string[] args ) {
         try {
            Configuration cfg = new Configuration();
            cfg.AddAssembly( "Northwind" );
            PersistentClass pcOrder = cfg.GetClassMapping( typeof( Order ) );
            Property propUserAttributes = pcOrder.GetProperty( "UserAttributes" );
            Component compUserAttributes = ( Component )propUserAttributes.Value;
            compUserAttributes.AddProperty(
               addDynamicAttribute( "myField1", "field_1", pcOrder ) );
            ISessionFactory factory = cfg.BuildSessionFactory();


The result of executing this code is an exception while building the session factory:
Code:
Aborting with exception:
NHibernate.MappingException: property mapping has wrong number of columns: Order
.Northwind.Order type: IDictionary
   at NHibernate.Mapping.PersistentClass.Validate(IMapping mapping)
   at NHibernate.Mapping.RootClass.Validate(IMapping mapping)
   at NHibernate.Cfg.Configuration.Validate()
   at NHibernate.Cfg.Configuration.BuildSessionFactory()
   at ORM.Center.Main(String[] args) in C:\Documents and Settings\workspace\Visual Studio 2005\Projects\ORM\Center.cs:line 70


Without calling "addDynamicAttribute" everything runs fine using the static mapping. Any ideas, please?

Best regards,

Gabriel Schmidt


Top
 Profile  
 
 Post subject: Re:
PostPosted: Thu Apr 15, 2010 2:34 pm 
Newbie

Joined: Thu Apr 15, 2010 1:57 pm
Posts: 9
Hello Gabriel,
were you able to resolve this error? I am also stuck in the same situation.

Thanks


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.