mercredi 24 décembre 2014

Best approach to save relationship mapping of country,state,city in orm


I am building a project in play framework java with jpa and I want set country,state,city dropdown fields in a form to be dynamic(from database).So only admin can increase or decrease the value of city,country,state in the form dropdown and user can only select those value. So what is the best approach.My approaches are specified below


Approach 1


I have created a Location model as below



@Entity
public class Locations {

@Id
private Long id;

@Constraints.Required
private String country;

@Constraints.Required
private String state;

@Constraints.Required
private String city;
}


The above approach saves a new row to a table everytime even if the location have same country and city so the same data is inserted


Approach 2


I have created models Country,State,City which individually save their respective value.And another model Locations which saves how the above 3 entities(like which country have which state and city) are related as given below



@Entity
public class Locations {

@Id
@Column(name = "id", nullable = false)
@GeneratedValue(strategy = GenerationType.AUTO)
private Long id;

@Constraints.Required
@OneToOne(cascade = CascadeType.ALL)
private Country country;

@Constraints.Required
@OneToOne(cascade = CascadeType.ALL)
private State state;

@Constraints.Required
@OneToOne(cascade = CascadeType.ALL)
private City city;
}


So what is the best approach to map country,state,city in a relation in orm??


Thanks





Aucun commentaire:

Enregistrer un commentaire