-->
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: Manipulating metadata at runtime. Something wrong?
PostPosted: Tue Jan 15, 2008 11:24 am 
Newbie

Joined: Fri Jan 02, 2004 10:17 pm
Posts: 3
Hibernate version:
3.2

Mapping documents:
Code:
<hibernate-mapping>
  <class name="Contact" table="contacts"> 
   <id column="id" name="id">
      <generator class="native"/>
   </id>
   <property name="name" column="name" type="string"/>
   <dynamic-component name="customProperties">      
   </dynamic-component>     
  </class>
</hibernate-mapping>

Code:
Code:
public class HibTest {
    private static final String TEST_FIELD_NAME = "s1";
    private static final String TEST_VALUE = "test@test.com";
    public static String CUSTOM_COMPONENT_NAME = "customProperties";
    private Class entityClass = Contact.class;
   
   
   public HibTest() {
      Configuration configuration = new Configuration().configure();
      SessionFactory sessionFactory = configuration.buildSessionFactory();
      Session session = sessionFactory.openSession();
        configuration.buildSessionFactory().openSession();     
       
        Column column = new Column(TEST_FIELD_NAME);
        column.setName(TEST_FIELD_NAME);
        column.setNullable(false);
        column.setUnique(false);
       
        PersistentClass persistentClass = configuration.getClassMapping(entityClass.getName());
        persistentClass.getTable().addColumn(column);
        SimpleValue simpleValue = new SimpleValue();
        column.setValue(simpleValue);
        simpleValue.addColumn(column);
        simpleValue.setTypeName("string");
       
        simpleValue.setTable(persistentClass.getTable());
        Property property = new Property();
        property.setName(TEST_FIELD_NAME);
        property.setValue(simpleValue);
        property.setNodeName(property.getName());
        Component customProperties = (Component) persistentClass.getProperty(CUSTOM_COMPONENT_NAME).getValue();
        customProperties.addProperty(property);
       
        session = configuration.buildSessionFactory().openSession();
        try {
           Contact contact = (Contact) session.get(Contact.class, 4);
            Object value = contact.getValueOfCustomField(TEST_FIELD_NAME);
            System.out.println("value = " + value);
        } catch (Exception e) {
           System.out.println("e = " + e);
        }
   }
   
    public static void main(String[] args) {
       HibTest test = new HibTest();
    }
}


Code:
public class Contact {
   private int id;
   
   private String name;

    private Map customProperties;
    public Map getCustomProperties() {
           return customProperties;
    }
    public void setCustomProperties(Map customProperties) {
           this.customProperties = customProperties;
    }
    public Object getValueOfCustomField(String name) {
        return getCustomProperties().get(name);
    }
    public void setValueOfCustomField(String name, Object value) {
        getCustomProperties().put(name, value);
    }
   public int getId() {
      return id;
   }
   public void setId(int id) {
      this.id = id;
   }
   public String getName() {
      return name;
   }
   public void setName(String name) {
      this.name = name;
   }
}

Full stack trace of any exception that occurs:
Code:
Exception in thread "main" org.hibernate.MappingException: property mapping has wrong number of columns: Contact.customProperties type: component[]
   at org.hibernate.mapping.PersistentClass.validate(PersistentClass.java:441)
   at org.hibernate.mapping.RootClass.validate(RootClass.java:192)
   at org.hibernate.cfg.Configuration.validate(Configuration.java:1102)
   at org.hibernate.cfg.Configuration.buildSessionFactory(Configuration.java:1287)
   at HibTest.<init>(HibTest.java:46)
   at HibTest.main(HibTest.java:57)


It occurs at second "configuration.buildSessionFactory().openSession();"

Name and version of the database you are using:
Postgresql 8.2

Questions:
1. I wish to add a property to the dynamic-component at runtime. However, I get an exception (see above). What's wrong?

2. I want to manipulate metadata at runtime in JEE environment (Jboss 4.2). How do I do that?

Thanks!


Top
 Profile  
 
 Post subject:
PostPosted: Wed Jan 23, 2008 3:37 pm 
Newbie

Joined: Wed Jan 23, 2008 3:35 pm
Posts: 3
im having exactly the same problem.

i got the "property mapping has wrong number of columns" exception while trying to dynamically modify the Configuration mappings.

any clue or solution about this ?


Top
 Profile  
 
 Post subject: My solution on this
PostPosted: Fri Jan 25, 2008 3:33 pm 
Newbie

Joined: Wed Jan 23, 2008 3:35 pm
Posts: 3
Using NHibernate i just did the work around avoiding runtime manipulation of the configuration.

My solution is:

1 - Load the hbm into an XmlDocument.
2 - Modify the XmlDocument adding the proper fields (nodes) to the CustomProperties <dynamic-component>
3 - Load the modified XmlDocument into the configuration Configuration.AddDocument(XmlDocument)

One thing to consider is the fact that dynamic-components have a component behavior so the CustomField IDictionary is gonna be null if all the values in the collection are null.


Top
 Profile  
 
 Post subject:
PostPosted: Sat Feb 02, 2008 6:09 pm 
Newbie

Joined: Sat Feb 02, 2008 5:46 pm
Posts: 2
I also tried to load the hbm into an XmlDocument and modify it but I keep on getting errors.

Code:
XmlDocument xmldoc = new XmlDocument();         
         xmldoc.Load("Model/Driver.hbm.xml");
         XmlNamespaceManager nsMgr = new XmlNamespaceManager(xmldoc.NameTable);
         nsMgr.AddNamespace("nh", "urn:nhibernate-mapping-2.2");
         XmlNode node = xmldoc.SelectSingleNode("/nh:hibernate-mapping/nh:class/nh:dynamic-component",nsMgr);         
         
         XmlElement element = xmldoc.CreateElement("property");         
         
         //Create new attribute Name
         XmlAttribute attrName = xmldoc.CreateAttribute("name");
         attrName.Value = "NewField";

         //Create new attribute Type
         XmlAttribute attrType = xmldoc.CreateAttribute("type");
         attrType.Value = "string";

         //Add the attributes to the property node
         element.Attributes.Append(attrName);
         element.Attributes.Append(attrType);
         
         //Append the propperty node
         node.AppendChild(element);   
      
Configuration.AddDocument(XmlDoc)


I see that an attribute xmlns="" is added to the "property" and I assume that is the problem, but I don't know how I can remove this.

I get the following error:
Quote:
NHibernate.MappingException: (XmlDocument)(10,8): XML validation error: The element 'dynamic-component' in namespace 'urn:nhibernate-mapping-2.2' has invalid child element 'property'. List of possible elements expected: 'property, many-to-one, one-to-one, component, dynamic-component, any, map, set, list, bag, array, primitive-array' in namespace 'urn:nhibernate-mapping-2.2'.




Any suggestions?


Top
 Profile  
 
 Post subject: how to remove attribute xmlns=""
PostPosted: Tue Feb 05, 2008 11:42 am 
Newbie

Joined: Wed Jan 23, 2008 3:35 pm
Posts: 3
you should create the new element for the nhibernate namespace context.

so .. replace this line of code:

XmlElement element = xmldoc.CreateElement("property");

for the following one:

XmlElement element = xmlDoc.CreateElement("property", "urn:nhibernate-mapping-2.2");

by this, you would be specifiying the namespace for the new element.

hope this works

jazz...


Top
 Profile  
 
 Post subject:
PostPosted: Wed Feb 06, 2008 8:40 am 
Newbie

Joined: Sat Feb 02, 2008 5:46 pm
Posts: 2
Thanks for the tip, I will give it a try

For testing purposes I took another approach, I just created a second mapping XML-file and that works of course ;-)


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.