Using this example:
CREATE TABLE IF NOT EXISTS COUNTRY (
COUNTRY_CODE VARCHAR(3) NOT NULL,
DIALLING_CODE VARCHAR(5) NOT NULL,
CREATION_TIMESTAMP TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,
CREATED_BY VARCHAR(20) NOT NULL,
LAST_UPDATE_TIMESTAMP TIMESTAMP NULL ON UPDATE CURRENT_TIMESTAMP,
LAST_UPDATED_BY VARCHAR(20),
DELETION_TIMESTAMP TIMESTAMP NULL,
DELETED_BY VARCHAR(20),
PRIMARY KEY(NUMERIC_CODE),
UNIQUE(ALPHA2_CODE),
UNIQUE(ALPHA3_CODE),
UNIQUE(DIALLING_CODE),
FOREIGN KEY(CREATED_BY) REFERENCES USER(USER_NAME),
FOREIGN KEY(LAST_UPDATED_BY) REFERENCES USER(USER_NAME),
FOREIGN KEY (DELETED_BY) REFERENCES USER(USER_NAME)
) CHARACTER SET utf8;
CREATE TABLE IF NOT EXISTS COUNTRY_NAME (
COUNTRY_CODE VARCHAR(3) NOT NULL,
LANGUAGE_CODE VARCHAR(2) NOT NULL,
NAME VARCHAR(20) NOT NULL,
CREATION_TIMESTAMP TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,
CREATED_BY VARCHAR(20) NOT NULL,
LAST_UPDATE_TIMESTAMP TIMESTAMP NULL ON UPDATE CURRENT_TIMESTAMP,
LAST_UPDATED_BY VARCHAR(20),
PRIMARY KEY (COUNTRY_CODE, LANGUAGE_CODE),
FOREIGN KEY (COUNTRY_CODE) REFERENCES COUNTRY (COUNTRY_CODE),
FOREIGN KEY (LANGUAGE_CODE) REFERENCES LANGUAGE (ALPHA2_CODE),
FOREIGN KEY (CREATED_BY) REFERENCES USER(USER_NAME),
FOREIGN KEY (LAST_UPDATED_BY) REFERENCES USER(USER_NAME)
) CHARACTER SET utf8;
The JPA representation for Country is as follows:
@XmlRootElement
@Entity
@Table(name="COUNTRY")
@Access(AccessType.FIELD)
@Cacheable
public class Country extends AbstractSoftDeleteAuditableEntity<String> implements za.co.sindi.persistence.entity.Entity<String>, Serializable {
private static final long serialVersionUID = -4019436514961967327L;
@Id
@Column(name="COUNTRY_CODE", length=3, nullable=false)
@Size(min=3, max=3)
@Pattern(regexp="^\\d{3}$")
private String id;
@Column(name="DIALLING_CODE", length=5, nullable=false)
@Pattern(regexp="^\\+[0-9]{2,5}$")
private String diallingCode;
@OneToMany(cascade= CascadeType.ALL, mappedBy="country")
private List<CountryName> names;
/* (non-Javadoc)
* @see za.co.sindi.entity.IDBasedEntity#getId()
*/
public String getId() {
// TODO Auto-generated method stub
return id;
}
/* (non-Javadoc)
* @see za.co.sindi.entity.IDBasedEntity#setId(java.io.Serializable)
*/
public void setId(String id) {
// TODO Auto-generated method stub
this.id = id;
}
/**
* @return the diallingCode
*/
public String getDiallingCode() {
return diallingCode;
}
/**
* @param diallingCode the diallingCode to set
*/
public void setDiallingCode(String diallingCode) {
this.diallingCode = diallingCode;
}
/**
* @return the names
*/
public List<CountryName> getNames() {
return names;
}
/**
* @param names the names to set
*/
public void setNames(List<CountryName> names) {
this.names = names;
}
}
With the above model, I will have to iterate names
attribute to find a CountryName
which matches a language code.
Is there a better way to retrieve a Country
and CountryName
based on a language code? If not, how can I achieve a simpler approach that can allow me to pull a Country
information with language code to an entity like as follows:
public class Country implements Serializable {
private static final long serialVersionUID = -4019436514961967327L;
@Id
@Column(name="COUNTRY_CODE", length=3, nullable=false)
@Size(min=3, max=3)
@Pattern(regexp="^\\d{3}$")
private String id;
@Column(name="DIALLING_CODE", length=5, nullable=false)
@Pattern(regexp="^\\+[0-9]{2,5}$")
private String diallingCode;
private String name;
private Language language;
//Getters and setters will be auto generated here....
}
That way, this can be achieved:
Country country = countryDAO.find(countryCode);
System.out.println(country.getName());
As I said in the comments, I think the best solution would be to use a separate query (and DAO) for CountryName
, because it would avoid loading unnecessary country names when you only need one for specified countryCode
.
One option is to use a @Transient java.util.Map
field which would be loaded with country names, with countryCode
as map key,
@Transient
private Map<String, CountryName> countryNameMap;
...
public CountryName getName(String countryCode) {
if (countryNameMap == null) {
countryNameMap = loadNamesIntoMap(); // a loop with countryNameMap.put(countryName.getCountryCode(), countryName)
}
return countryNameMap.get(countryCode);
}
Another interesting option is to actually use java.util.Map
for relation between Country
and CountryName
@OneToMany(mappedBy="country")
@MapKey(name="countryCode")
private Map<String, CountryName> names;
You would then access the name with country.getNames().get(countryCode)
.
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.