My question is actually a spin-off of this question as seen here... so it might help to check that thread before proceeding.
In my Spring Boot project, I have two entities Sender
and Recipient
which represent a Customer
and pretty much have the same fields, so I make them extend the base class Customer
;
Customer base class;
@MappedSuperclass
public class Customer extends AuditableEntity {
@Column(name = "firstname")
private String firstname;
@Transient
private CustomerRole role;
public Customer(CustomerRole role) {
this.role = role;
}
//other fields & corresponding getters and setters
}
Sender domain object;
@Entity
@Table(name = "senders")
public class Sender extends Customer {
public Sender(){
super.setRole(CustomerRole.SENDER);
}
}
Recipient domain object;
@Entity
@Table(name = "recipients")
public class Recipient extends Customer {
public Recipient(){
super.setRole(CustomerRole.RECIPIENT);
}
}
NOTE - Sender
and Recipient
are exactly alike except for their roles. These can be easily stored in a single customers Table by making the Customer
base class an entity itself, but I intentionally separate the entities this way because I have an obligation to persist each customer type in separate database tables.
Now I have one form in a view that collects details of both Sender
& Recipient
, so for example to collect the firstname, I had to name the form fields differently as follows;
Sender section of the form;
<input type="text" id="senderFirstname" name="senderFirstname" value="$!sender.firstname">
Recipient section of the form;
<input type="text" id="recipientFirstname" name="recipientFirstname" value="$!recipient.firstname">
But the fields available for a customer are so many that I'm looking for a way to map them to a pojo by means of an annotation as asked in this question here. However, the solutions provided there would mean that I have to create separate proxies for both domain objects and annotate the fields accordingly e.g
public class SenderProxy {
@ParamName("senderFirstname")
private String firstname;
@ParamName("senderLastname")
private String lastname;
//...
}
public class RecipientProxy {
@ParamName("recipientFirstname")
private String firstname;
@ParamName("recipientLastname")
private String lastname;
//...
}
So I got very curious and was wondering, is there a way to map this Proxies to more than one @ParamName such that the base class for example can just be annotated as follows?;
@MappedSuperclass
public class Customer extends AuditableEntity {
@Column(name = "firstname")
@ParamNames({"senderFirstname", "recipientFirstname"})
private String firstname;
@Column(name = "lastname")
@ParamNames({"senderLastname", "recipientLastname"})
private String lastname;
@Transient
private CustomerRole role;
public Customer(CustomerRole role) {
this.role = role;
}
//other fields & corresponding getters and setters
}
And then perhaps find a way to select value of fields based on annotation??