Here's the idea -
1. Person table stores the Person's details and his/her Address,
2. City and State have their individual tables,
3. Address table contains data about City and which State the City is a part of,
4. Contact table contains Contact Numbers, where a Person can have multiple Contact numbers.
Following is the MySql Table schema design -
1. Persons
2. Address
3. City
4. State
5. Contact
I have created the following Domains for the above Tables -
1. PersonsDomain
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
@Column(name = "PersonID")
private int personId;
@Column(name = "FirstName")
private String firstName;
@Column(name = "Age")
private int age;
@Column(name = "DateOfBirth")
private Date dateOfBirth;
@OneToMany(mappedBy = "PersonID", cascade = CascadeType.MERGE, fetch = FetchType.LAZY)
@JsonBackReference
private Set<ContactDomain> contactDomain;
@ManyToOne
@JoinColumn(name = "AddressID", nullable = true)
@JsonBackReference
private Set<AddressDomain> addressDomain;
2. AddressDomain
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
@Column(name = "AddressID")
private int addressId;
@OneToOne(mappedBy = "CityID", cascade = CascadeType.MERGE, fetch = FetchType.LAZY)
@Fetch(FetchMode.JOIN)
@JsonBackReference
private CityDomain cityDomain;
@ManyToOne
@JoinColumn(name = "StateID", nullable = true)
@JsonBackReference
private StateDomain stateDomain;
3. CityDomain
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
@Column(name = "CityID")
private int cityId;
@Column(name = "CityName")
private String cityName;
4. StateDomain
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
@Column(name = "StateID")
private int stateId;
@Column(name = "StateName")
private String stateName;
5. ContactDomain
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
@Column(name = "ContactID")
private int contactId;
@Column(name = "ContactNumber")
private int contactNumber;
@ManyToOne
@JoinColumn(name = "PersonID", nullable = true)
@JsonBackReference
private PersonsDomain personId;
@Column(name = "Description")
private String description;
I want to know if, I have correctly mapped the relations in Hibernate.
You cant use @ManyToOne for Set, e,g PersonsDomain you have used
@ManyToOne
@JoinColumn(name = "AddressID", nullable = true)
@JsonBackReference
private Set<AddressDomain> addressDomain;
instead of @ManyToOne
, you need to replace with @OneToMany
.
Also you need to remove @JoinColumn
from here because in your case relationship is one directional (PersonsDomain
to AddressDomain
) and PersonDomain
has many AddressDomain
, so you can't use it for this scenario.
Also you have used referenced property unknown CityID
in AddressDomain
. In @OneToMany mappedBy field use that owns the relationship. This element is only specified on the inverse (non-owning)side of the association.
please remove it from below :
@OneToOne(mappedBy = "CityID", cascade = CascadeType.MERGE, fetch = FetchType.LAZY)
@Fetch(FetchMode.JOIN)
@JsonBackReference
private CityDomain cityDomain;
Also it's a good practice use object(entity) name instead of database Id column for denoting a object, as you have used ContactDomain
->
private PersonsDomain personId;
So it will good if you used field name personsDomain
instead of personId
, because OOPs programing personsDomain making much scene.
I have update the Entities which you have mention in description: PersonDomain:
@Entity
public class PersonsDomain {
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
@Column(name = "PersonID")
private int personId;
@Column(name = "FirstName")
private String firstName;
@Column(name = "Age")
private int age;
@Column(name = "DateOfBirth")
private Date dateOfBirth;
@OneToMany(mappedBy = "personId", cascade = CascadeType.MERGE, fetch = FetchType.LAZY)
private Set<ContactDomain> contactDomain;
@OneToMany
private Set<AddressDomain> addressDomain;
}
AddressDomain:
@Entity
public class AddressDomain {
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
@Column(name = "AddressID")
private int addressId;
@OneToOne(cascade = CascadeType.MERGE, fetch = FetchType.LAZY)
@Fetch(FetchMode.JOIN)
private CityDomain cityDomain;
@ManyToOne
@JoinColumn(name = "StateID", nullable = true)
private StateDomain stateDomain;
}
CityDomain:
@Entity
public class CityDomain {
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
@Column(name = "CityID")
private int cityId;
@Column(name = "CityName")
private String cityName;
}
ContactDomain:
@Entity
public class ContactDomain {
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
@Column(name = "ContactID")
private int contactId;
@Column(name = "ContactNumber")
private int contactNumber;
@ManyToOne
@JoinColumn(name = "PersonID", nullable = true)
private PersonsDomain personsDomain;
@Column(name = "Description")
private String description;
}
StateDomain:
@Entity
public class StateDomain {
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
@Column(name = "StateID")
private int stateId;
@Column(name = "StateName")
private String stateName;
}
Also here are another thing a citydmoin has many statedomin, so you can this this relation, also another things here but it's depended on your requirement so you need to changed the things according your requirement.
Also as per your mysql schema user has one address but you are using Set of address
in persondomin
, so make sure Person have one Address on Many Address, changed it accroding your requirement.