我试图本地化我的 jpa 应用程序。为此,我在课堂上使用了 @MapKeyColumn 属性。所以我以这种方式定义了这个类:
@Entity
@Inheritance(strategy = InheritanceType.JOINED)
@Table(name = "BIENTOO_SPORT")
public class Sport implements Serializable {
private static final long serialVersionUID = 1L;
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
@Column(name = "sportId")
private Long id;
@Column(name = "code")
private String code;
@OneToMany(fetch = FetchType.LAZY, orphanRemoval = true, mappedBy = "sport")
@MapKeyColumn(name = "SPORT_LANGUAGE_KEY")
private Map<String,SportLocalizedString> sportLocalizedString;
public Long getId() {
return id;
}
public void setId(Long id) {
this.id = id;
}
public String getCode() {
return code;
}
public void setCode(String code) {
this.code = code;
}
public Map<String, SportLocalizedString> getSportLocalizedString() {
return sportLocalizedString;
}
public void setSportLocalizedString(Map<String, SportLocalizedString> sportLocalizedString) {
this.sportLocalizedString = sportLocalizedString;
}
public SportLocalizedString newSportLocalizedString(){
return new SportLocalizedString();
}
@Entity
@Table(name = "BIENTOO_SPORT_LOCALIZED_STRING")
public class SportLocalizedString implements Serializable {
@Id
@GeneratedValue(generator = "bientooSportLocalizedString", strategy = GenerationType.TABLE)
@TableGenerator(name = "bientooSportLocalizedString", table = "SEQUENCE_GENERATOR", pkColumnName = "ID_NAME", valueColumnName = "ID_VAL", pkColumnValue = "sportLocalizedString", allocationSize = 50)
@Column(name = "SPORT_LOCALIZED_ID")
private Long id;
@Column(name = "SPORT_LANGUAGE_KEY")
private String language;
@Column(name = "SPORT_LANGUAGE_NAME")
private String name;
@ManyToOne
private Sport sport;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getLanguage() {
return language;
}
public void setLanguage(String language) {
this.language = language;
}
public Sport getSport() {
return sport;
}
public void setSport(Sport sport) {
this.sport = sport;
}
}
}
正如您在 Sport 中看到的那样,我与内部阶层有 OneToMany 关系。当我启动应用程序时,一切正常。但是当我检索 Sport 对象时,Map sportLocalizedString 为空。我做错了什么?