Doubt with the POJOs

I’m following the official document and blog post to use POJO mapping to the Entity. For example, I have a Customer Entity, which has a phone number and address details (other than the name, email, password). If I use the QuickStart guide to get user input would be complaining as it is different from the constructor that declared in the user entity.

Then I tweak it like using the relational database to get all customer input, but it raises another issue on how to append all address details to the customer object? As you might know that the State and Postcode is separate as it owns. Should Address be another new entity or how to put it together? What is the right way to use MongoDB in Java Servlet?

MongoCollection<Customer> customerTbl = database.getCollection("Customer", Customer.class);

Customer newCustomer = new Customer(); // that linking to the constructuor of the entity instead

newCustomer.setEmail(customer.getEmail());
newCustomer.setFullName(customer.getFullName());
newCustomer.setPassword(customer.getPassword());
newCustomer.setState(customer.getState());
newCustomer.setPostCode(customer.getPostCode());
newCustomer.setPhone(customer.getPhone());    
newCustomer.setRegisterDate(new Date());

customerTbl.insertOne(newCustomer);

Thanks

Hello @Pat_Yue,

Should Address be another new entity or how to put it together?

The address information can be separate class (another entity) as shown in the Quick Start guide (you had linked, and it shows using address as a separate class) or the address information can be individual fields, like street, city, etc., and within the Customer POJO class. It is a matter of designing your application.

An Address POJO on its own allows grouping of related information - info related to an address. Also, the Address class can be used in other entities of the application (for example, an Employee or a Supplier entity has an address too).

1 Like

You also have an exemple in this blog post:

https://developer.mongodb.com/quickstart/java-mapping-pojos

Which is using this Github Repo:

Especially this class:

And its models Grade and Score: