-->
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.  [ 2 posts ] 
Author Message
 Post subject: Modify fields dynamically using Reflection
PostPosted: Tue Jul 26, 2016 6:17 am 
Newbie

Joined: Tue Jul 26, 2016 6:12 am
Posts: 1
Is there a way to update a field in a row, without to mention the field name?

I have a list of items, that each item has properties. From each property I can get the property name and the property value. The table's columns are named by the properties.

I'm looking for a method that will help me to do something like this:
Code:
      for(Item currentItem : itemsArray)
      {
         tableName row = new tableName();
         Property [] itemProperties = currentItem.getPropertiesArray();
         for(Property prop : itemProperties)
         {
            row.updateField(prop.getName(), prop.getValue()); // I'm looking for a method like this. that gets a filed name and a value.
         }
      }


Thanks.


Top
 Profile  
 
 Post subject: Re: Modify fields using variable that represents the field name
PostPosted: Tue Jul 26, 2016 10:32 am 
Hibernate Team
Hibernate Team

Joined: Thu Sep 11, 2014 2:50 am
Posts: 1628
Location: Romania
You can use either FieldUtils from Apache Commons Lang, or ReflectionUtils from Spring:

Code:
FieldUtils.writeField(tableName, prop.getName(), prop.getValue(), true);


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