Overriding #JsonProperty in a nested property - java

I have 2 classes:
class Customer {
. . .
#JsonProperty("customer_address")
private Address address;
}
and
class Company {
. . .
#JsonProperty("company_address")
private Address address;
}
The structure of the Address object is the same, both for company and customer, but unfortunately my JSON counterpart uses different JSON property names in the two cases, like
class Address { // in Customer
#JsonProperty("customer_city")
private String city;
}
class Address { // in Company
#JsonProperty("company_city")
private String city;
}
I'm wondering, but I miss how, if I can "override" the JsonProperty annotation in Address from the enclosing Customer/Company, so to have one only class.
Any suggestion?

Related

Best practice for DTO classes that contain complex objects

If I have a Entity class as such where a user can have multiple addresses:
#Entity
public class User {
private String name;
private List<Address> address;
}
#Entity
public class Address {
private String area;
private String city;
}
Should the DTO exposed for the webservice contain Address object (like what User contains) or an AddressDTO object like this?
public class UserDTO {
private String name;
private List<AddressDTO> addressList;
}
Where can I find example code using DTO objects that contain nested objects as such, where I can see best practices on how to populate those nested objects inside the main DTO? (I've googled ad nauseam but so far no examples with this scenario)

How to add where on custom type in hibernate

EmployeeRepository class
CriteriaQuery<Employee> query = criteriaBuilder.createQuery(Employee.class);
Root<Employee> employee = query.from(Employee.class);
query.select(employee)
.where(employee.get("address.city").in("pune","Mumbai","Delhi"));// getting error here
Employee class
class Employee {
private Long empId;
#Type(employeeCustomType)
private Address address; //this is custom type
// setter and getter
}
Address class
class Address{
private String city;
private Integer zipCode;
}
please just assume we have employeeCustomType class
any one have any idea on it.
I am just trying to add where clause on address object of city parameter.
using CriteriaBuilder.
Any other suggestion to achieve this scenario in hibernate

List denormalization with morphia

Suppose i have two morphia entities: Person and Team which are look like this
#Entity
public class Person {
private String name;
private String login;
private String mail;
private List<Team> teams;
}
#Entity
public class Team {
private String name;
private String description;
private List<Person> members;
//some more fields
}
I want map this model into Mongodb database like this
Users collection
{
name:"someName",
login:"somelogin",
mail:"some#mail.com",
teams: [
{id:"teamId", name:"TeamName"} //only specific fields fron Team Entity
{id:"anotherTeamId", name:"AnotherTeamName"}
]
}
Teams collection
{
id:"teamId",
name:"TeamName",
description:"Very strong team",
members: [id:"aaa", name: "someName"] //only specific fields fron User Entity
//some other fields
}
{
id:"anotherTeamId",
name:"AnotherTeamName",
description:"Brave new team",
members: [id:"aaa", name: "someName"] //only specific fields fron User Entity
//some other fields
}
So, I want denormolize only specific fields (only name for example) from Team document into User's teams field.
I don't understand Can I use morphia (or some other odm) for this case? Which annotations I should use in my Entities?
It seems that #Reference annotation is not allowed with List<> fields.
I think, i should create inner class PersonTeam, which will contain Team's name and id, and use it in Person class
#Entity
public class Person {
private String name;
private String login;
private String mail;
private List<PersonTeam> teams;
}
public class PersonTeam {
private String teamId;
private String teamName;
}
Is this a good way to solve my problem? thank you!
That's about the only way to do it with morphia at least. You might consider using #Reference on those fields and only storing the IDs. That'd help if your concern is saving space.

Mapstruct ignore target nested property mapping

I have following DTO and Domain objects. I am using Mapstruct to copy domain object to DTO object.
public class AddressDomain {
private String street;
private Telephone telephone;
}
public class CompanyDomain{
private String id;
private Address address;
}
public class AddressDTO {
private String street;
private Telephone telephone;
}
public class CompanyDTO{
private String id;
private Address address;
}
Mapping Domain to DTO using below Mapper. i don't want to map telephone property from domain to DTO. How to do that? i tried providing nested target property in mapping ignore but it gives error:
public interface CompanyMapper {
//**below line gives error**
#Mapping(target = "address.telephone", ignore=true)
CompanyDTO map(AddressDTO dto);
}
Your current definition maps an address into a company object which doesn't seem right. You need to declare two methods, one for mapping addresses and one for mapping companies (whose generated implementation will in turn invoke the address mapping method):
public interface CompanyMapper {
CompanyDTO map(Company company);
#Mapping(target="telephone", ignore=true)
AddressDTO map(Address address);
}

#Embedded beans as final attribute?

We are using some #Embeddable beans within some JPA Entities for better code structure and reuse. What I'm wondering is: every example I come across provides setters and getters for the #Embedded objects. Why? I tried just having them as final fields - that works, too, and makes much sense in our case, since the embedded stuff is always present. Is there some disadvantage of this that I am not aware of?
For example: usually it is done that way:
#Embeddable
public class Address {
// ...
}
#Entity
public class Person {
#Embedded
private Address address;
public void setAddress(Address address) {
this.address = address;
}
public Address getAddress() {
return address;
}
}
Is there any trouble when I write Person this way, when there is always an Address:
#Entity
public class Person {
#Embedded
private final Address address = new Address();
public Address getAddress() {
return address;
}
}
You cannot persist final or static fields, and that applies whether embedded or non-embedded. The JPA spec is very clear about that, as are documents for the different JPA providers.
JPA Spec $2.1
The entity class must not be final. No methods or persistent instance
variables of the entity class may be final.

Categories

Resources