I would like to be able to query for an employee by the contact details. Here is my model.
You can assume that I have the controller working.
public abstract class BaseEntity implements Serializable {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Long id;
public boolean isNew() {
return this.id == null;
}
}
public abstract class PersonalInformation extends BaseEntity {
@Temporal(TemporalType.DATE)
private Date dateOfBirth;
private String gender;
}
public abstract class Person extends PersonalInformation implements Serializable {
private String firstName;
private String lastName;
private String middleName;
private String alias;
@Temporal(TemporalType.DATE)
private Date dateOfBirth;
private String gender;
@OneToOne(cascade = CascadeType.ALL)
private Contact contact;
}
public class Owner extends Person implements Serializable {
}
public class Contact extends BaseEntity {
private String emailAddress;
private String mobileTelNo;
private String homeTelNo;
private String workTelNo;
}
This is what I tried, thinking that the persistence will be both ways, wo when I would build a controller, I could build the response to include either the owner or the employee or other entity that I might have in my system of contacts
public class Contact extends BaseEntity {
private String emailAddress;
private String mobileTelNo;
private String homeTelNo;
private String workTelNo;
@OneToOne(fetch = FetchType.EAGER, cascade = CascadeType.ALL)
private Owner owner;
@OneToOne(fetch = FetchType.EAGER, cascade = CascadeType.ALL)
private Employee employee;
}
The relation between contact and owner or any other entity that would have a contact is one to one.
Could someone help me map my entities one to another, so that data is persisted? Is what I want to do a good practice, or is there a better way?
Desired output when querying by the email:
GET: http://localhost:8080/v1/contact/email/jow.doe@stackoverflow.com
{
"firstName": "Joe",
"lastName": "Doe",
"middleName": "None",
"alias": "JD",
"dateOfBirth": "2000-02-12",
"gender": "male",
"contact": {
"emailAddress": "joe.doe@stackoverflow.com"
}
}
Note: please ignore if I missed any data in the output that is in the pojos.
IMO Contact
should not have any mappings to Owner
or Employee
. If you don't need it, don't map it. You can query the data by using a HQL query like FROM Owner o JOIN o.contact c WHERE c.emailAddress =:email
.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.