0
votes

I have this JSON:

{
  'name': 'Ana',
  'lastName':'Lee',
  'address': {
     'street':'321 Malvo St',
     'city':'Nowhere',
     'state':'MA',
     'zip':'010101'
   }
 }

I need to persist using MongoDB Driver in Java. How should I create field 'address' in my class?

 @Document(collection = "user")
 public class User extends AbstractEntity{

       private String name;

       private String lastName;

       private ????? address

       ...
  }

address type ?????, should be:
- a new class Address with fields street, city, etc..
- a Document
- a String
- a Map
- other

1

1 Answers

3
votes

You need to create a new Address class,

    class Adderss {
      private String street;
      private String city;
      private String state;
      private String zip;

      //getters and setters
   }

Then you can use it in your User class,

private Address address;

Or else you can use a java.util.Map for this.

private Map<String, String> address;

Both works fine but if you need to use those address details in your logic. I recommend you to use Address class.