我们的第一个实体Bean类。
@Entity
@Table(name = "visamanagmenttable")
@DynamicUpdate
@SelectBeforeUpdate(true)
public class VisaManagementBean {
@Id
@Column(name = "visaManageId")
@GeneratedValue(strategy = GenerationType.IDENTITY)
private int visaManageId;
@Column(name = "countryName")
private String countryName;
@Column(name = "visaUniqueId")
private String visaUniqueId;
@Column(name = "visaCategory")
private int visaCategory;
@Column(name = "visaVarientName")
private String visaVarientName;
@Column(name = "visaGovernmentFee")
private String visaGovernmentFee;
@Column(name = "visaServiceFee")
private String visaServiceFee;
@Column(name = "visaSecurityDepositFee")
private String visaSecurityDepositFee;
@Column(name = "visaAdditionalFee")
private String visaAdditionalFee;
@Column(name = "visaAdditionalRemark")
private String visaAdditionalRemark;
@Column(name = "visaSecurityDepositRemark")
private String visaSecurityDepositRemark;
@Column(name = "visaMode")
private String visaMode;
@Column(name = "visaCitizenShipCountry")
private String visaCitizenShipCountry;
@Column(name = "visaLivingCountry")
private String visaLivingCountry;
@Column(name = "visaDescription")
private String visaDescription;
@Column(name = "visaStatus")
private String visaStatus;
@Column(name="createdBy", updatable=false)
private String createdBy;
@Column(name="modifiedBy")
private String modifiedBy;
@CreationTimestamp
@Temporal(TemporalType.TIMESTAMP)
@Column(name="creationDate",updatable=false)
private Date creationDate;
@UpdateTimestamp
@Temporal(TemporalType.TIMESTAMP)
@Column(name="modifiedDate")
private Date modifiedDate;
@Column(name = "status")
private String status;
@Transient
private String[] citizencountry;
@Transient
private String[] livingcountry;
public int getVisaManageId() {
return visaManageId;
}
public String getVisaUniqueId() {
return visaUniqueId;
}
public int getVisaCategory() {
return visaCategory;
}
public String getVisaVarientName() {
return visaVarientName;
}
public String getVisaGovernmentFee() {
return visaGovernmentFee;
}
public String getVisaServiceFee() {
return visaServiceFee;
}
public String getVisaSecurityDepositFee() {
return visaSecurityDepositFee;
}
public String getVisaAdditionalFee() {
return visaAdditionalFee;
}
public String getVisaAdditionalRemark() {
return visaAdditionalRemark;
}
public String getVisaSecurityDepositRemark() {
return visaSecurityDepositRemark;
}
public String getVisaMode() {
return visaMode;
}
public String getVisaCitizenShipCountry() {
return visaCitizenShipCountry;
}
public String getVisaLivingCountry() {
return visaLivingCountry;
}
public String getVisaDescription() {
return visaDescription;
}
public String getVisaStatus() {
return visaStatus;
}
public String getCreatedBy() {
return createdBy;
}
public String getModifiedBy() {
return modifiedBy;
}
public Date getCreationDate() {
return creationDate;
}
public Date getModifiedDate() {
return modifiedDate;
}
public String getStatus() {
return status;
}
public void setVisaManageId(int visaManageId) {
this.visaManageId = visaManageId;
}
public void setVisaUniqueId(String visaUniqueId) {
this.visaUniqueId = visaUniqueId;
}
public void setVisaCategory(int visaCategory) {
this.visaCategory = visaCategory;
}
public void setVisaVarientName(String visaVarientName) {
this.visaVarientName = visaVarientName;
}
public void setVisaGovernmentFee(String visaGovernmentFee) {
this.visaGovernmentFee = visaGovernmentFee;
}
public void setVisaServiceFee(String visaServiceFee) {
this.visaServiceFee = visaServiceFee;
}
public void setVisaSecurityDepositFee(String visaSecurityDepositFee) {
this.visaSecurityDepositFee = visaSecurityDepositFee;
}
public void setVisaAdditionalFee(String visaAdditionalFee) {
this.visaAdditionalFee = visaAdditionalFee;
}
public void setVisaAdditionalRemark(String visaAdditionalRemark) {
this.visaAdditionalRemark = visaAdditionalRemark;
}
public void setVisaSecurityDepositRemark(String visaSecurityDepositRemark) {
this.visaSecurityDepositRemark = visaSecurityDepositRemark;
}
public void setVisaMode(String visaMode) {
this.visaMode = visaMode;
}
public void setVisaCitizenShipCountry(String visaCitizenShipCountry) {
this.visaCitizenShipCountry = visaCitizenShipCountry;
}
public void setVisaLivingCountry(String visaLivingCountry) {
this.visaLivingCountry = visaLivingCountry;
}
public void setVisaDescription(String visaDescription) {
this.visaDescription = visaDescription;
}
public void setVisaStatus(String visaStatus) {
this.visaStatus = visaStatus;
}
public void setCreatedBy(String createdBy) {
this.createdBy = createdBy;
}
public void setModifiedBy(String modifiedBy) {
this.modifiedBy = modifiedBy;
}
public void setCreationDate(Date creationDate) {
this.creationDate = creationDate;
}
public void setModifiedDate(Date modifiedDate) {
this.modifiedDate = modifiedDate;
}
public void setStatus(String status) {
this.status = status;
}
public String getCountryName() {
return countryName;
}
public void setCountryName(String countryName) {
this.countryName = countryName;
}
public String[] getCitizencountry() {
return citizencountry;
}
public String[] getLivingcountry() {
return livingcountry;
}
public void setCitizencountry(String[] citizencountry) {
this.citizencountry = citizencountry;
}
public void setLivingcountry(String[] livingcountry) {
this.livingcountry = livingcountry;
}
}
CitizenCountryBean与签证管理bean之间存在关系(一对多)
@Entity
@Table(name = "citizencountrytable")
@DynamicUpdate
@SelectBeforeUpdate(true)
public class CitizenCountryBean {
@Id
@Column(name = "citizenId")
@GeneratedValue(strategy = GenerationType.IDENTITY)
private int citizenId;
@Column(name = "citizencountryName")
private String citizencountryName;
@OneToMany(targetEntity = VisaManagementBean.class, cascade = CascadeType.ALL, fetch = FetchType.EAGER)
@JoinColumn(name = "citizenId", referencedColumnName = "citizenId")
private Set<VisaManagementBean> visaManagementList = new HashSet<VisaManagementBean>();
@ManyToMany(cascade = CascadeType.ALL,fetch = FetchType.EAGER)
private Set<LivingCountryBean> livingCountryBeanList= new HashSet<>();
@Column(name="createdBy", updatable=false)
private String createdBy;
@Column(name="modifiedBy")
private String modifiedBy;
@CreationTimestamp
@Temporal(TemporalType.TIMESTAMP)
@Column(name="creationDate",updatable=false)
private Date creationDate;
@UpdateTimestamp
@Temporal(TemporalType.TIMESTAMP)
@Column(name="modifiedDate")
private Date modifiedDate;
@Column(name = "status")
private String status;
public int getCitizenId() {
return citizenId;
}
public String getCitizencountryName() {
return citizencountryName;
}
public String getCreatedBy() {
return createdBy;
}
public String getModifiedBy() {
return modifiedBy;
}
public Date getCreationDate() {
return creationDate;
}
public Date getModifiedDate() {
return modifiedDate;
}
public String getStatus() {
return status;
}
public void setCitizenId(int citizenId) {
this.citizenId = citizenId;
}
public void setCitizencountryName(String citizencountryName) {
this.citizencountryName = citizencountryName;
}
public void setCreatedBy(String createdBy) {
this.createdBy = createdBy;
}
public void setModifiedBy(String modifiedBy) {
this.modifiedBy = modifiedBy;
}
public void setCreationDate(Date creationDate) {
this.creationDate = creationDate;
}
public void setModifiedDate(Date modifiedDate) {
this.modifiedDate = modifiedDate;
}
public void setStatus(String status) {
this.status = status;
}
public Set<LivingCountryBean> getLivingCountryBeanList() {
return livingCountryBeanList;
}
public Set<VisaManagementBean> getVisaManagementList() {
return visaManagementList;
}
public void setLivingCountryBeanList(Set<LivingCountryBean> livingCountryBeanList) {
this.livingCountryBeanList = livingCountryBeanList;
}
public void setVisaManagementList(Set<VisaManagementBean> visaManagementList) {
this.visaManagementList = visaManagementList;
}
}
我们的第三个bean类LivingCountryBean和Cityzenshipship之间的关系是多对多的。我们所有的数据都已成功插入到各个表中,但是我们的第3个表是通过休眠从多对多关系创建的,我们得到的是空值。
@Entity
@Table(name = "livingcountrytable")
@DynamicUpdate
@SelectBeforeUpdate(true)
public class LivingCountryBean {
@Id
@Column(name = "livingId")
@GeneratedValue(strategy = GenerationType.IDENTITY)
private int livingId;
@Column(name = "livingcountryName")
private String livingcountryName;
@ManyToMany(mappedBy="livingCountryBeanList")
private Set<CitizenCountryBean> citizenCountryBeanList= new HashSet<>();
@Column(name="createdBy", updatable=false)
private String createdBy;
@Column(name="modifiedBy")
private String modifiedBy;
@CreationTimestamp
@Temporal(TemporalType.TIMESTAMP)
@Column(name="creationDate",updatable=false)
private Date creationDate;
@UpdateTimestamp
@Temporal(TemporalType.TIMESTAMP)
@Column(name="modifiedDate")
private Date modifiedDate;
@Column(name = "status")
private String status;
public int getLivingId() {
return livingId;
}
public String getLivingcountryName() {
return livingcountryName;
}
public Set<CitizenCountryBean> getCitizenCountryBeanList() {
return citizenCountryBeanList;
}
public void setCitizenCountryBeanList(Set<CitizenCountryBean> citizenCountryBeanList) {
this.citizenCountryBeanList = citizenCountryBeanList;
}
public String getCreatedBy() {
return createdBy;
}
public String getModifiedBy() {
return modifiedBy;
}
public Date getCreationDate() {
return creationDate;
}
public Date getModifiedDate() {
return modifiedDate;
}
public String getStatus() {
return status;
}
public void setLivingId(int livingId) {
this.livingId = livingId;
}
public void setLivingcountryName(String livingcountryName) {
this.livingcountryName = livingcountryName;
}
public void setCreatedBy(String createdBy) {
this.createdBy = createdBy;
}
public void setModifiedBy(String modifiedBy) {
this.modifiedBy = modifiedBy;
}
public void setCreationDate(Date creationDate) {
this.creationDate = creationDate;
}
public void setModifiedDate(Date modifiedDate) {
this.modifiedDate = modifiedDate;
}
public void setStatus(String status) {
this.status = status;
}
}
我们在Spring ORM中使用休眠模板类的DAO代码。
@Override
public int createVisaManagement(CitizenCountryBean citizenCountryBean) {
Integer id = (Integer) hibernateTemplate.save(citizenCountryBean);
return id;
}
答案 0 :(得分:0)
尝试:
CitizenCountryBean。
@JoinTable(
name = "CityZen_Living",
joinColumns = { @JoinColumn(name = "citizen_id") },
inverseJoinColumns = { @JoinColumn(name = "living_id") }
)
CitizenCountryBean
@JoinTable(
name = "CityZen_Living",
joinColumns = { @JoinColumn(name = "living_id") },
inverseJoinColumns = { @JoinColumn(name = "citizen_id") }
)
注意:请勿将int用作主键。我认为您需要使用:Long或Interger。在实体中,如果@Column和name字段相同,则不需要@Column。如果要在休眠时将字段更改名称保存到数据库,则只有@Column。
答案 1 :(得分:0)
一件事要牢记。在尝试使数据库持久之前,请确保“对象”结构是一致的。
我看到您有回溯参考。在尝试保存之前,您需要确保存在所有向前和向后引用。如果不是这样,您将变得怪异。
我经常使用的模式是这样的:
在CitizenCountryBean中:
public void add(LivingCountryBean bean) {
livingCountryBeanList.add(bean);
bean.add0(this);
}
在LivingCountryBean中:
public void add(CitizenCountryBean bean) {
bean.add(this);
}
public void add0(CitizenCoutryBean bean) {
citizenCountryBeanList.add(bean);
}
这样可以确保无论何时从任一侧添加反向引用和正向引用,都将正确设置休眠状态。