Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8660427
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T16:15:02+00:00 2026-06-12T16:15:02+00:00

First, I have an object like that: public class Entity { public int data1;

  • 0

First, I have an object like that:

public class Entity {
    public int data1;
    public String data2;
    public float data3;
    public SubEntity data4;
}

public class SubEntity{
    public int id;
    public SubEntity(int id){
      tis.id = id;
    }
}

And a HashMap:

  HashMap<String, Object> map = new HashMap<String, Object>();
  map.put("data1", 1);
  map.put("data2", "name");
  map.put("data3", 1.7);
  map.put("data4", new SubEntity(11));

I need the right way to set value for all field of entity dynamic by use reflect from hashmap. Something like this:

    for (Field f : entity.getClass().getDeclaredFields()) {
       String name = f.getName();
       Object obj = map.get("name");
       // Need set value field base on its name and type. 

} 

How can I achieve that? Assume I have many sub classes in entity.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-12T16:15:04+00:00Added an answer on June 12, 2026 at 4:15 pm

    If you want to go the reflection route, then why not use Field.set(Object, Object) and its more type-safe siblings (see doc)

    f.set(myEntity, obj);
    

    Note. You may need to make the field accessible first if it’s private/protected.

    However if you can I would perhaps delegate to the object and it could populate itself via the map e.g.

    myEntity.populateFromMap(myMap);
    

    and do the hard(ish) work within your class.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Using Entity Framework Code First, I have something like: public class Foo { public
I have an entity class that has an embedded object within it: @Entity public
I have two entity classes: public class Invoice { public int ID { get;
I am using entity framework 4.1 code first. I have a GrantApplication class: public
I'm trying out Entity Framework Code first CTP4. Suppose I have: public class Parent
I have several Entity Framework Code First DbContext objects that use a custom Initializer.
I have a class that takes an IRepository in its constructor like this ...
I have a simple class Role: @Entity @Table (name = ROLE) public class Role
Technology: Hibernate 3.0 Suppose i have Entity class Company @Entity @Table(name=tbl_companies) public class Company
I defined a model like this public class Planilla { [Key] public int IDPlanilla

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.