我有两个相关的实体,一个是主表,主表中的一列有一个引用表。
错误是:
Caused by : javax.el.ELException: /jsf/submit.xhtml @20,76 listener="#{BankLocationMB.saveLocation}": org.springframework.dao.InvalidDataAccessApiUsageException: org.hibernate.TransientPropertyValueException:
Not-null property references a transient value - transient instance must be saved before current operation : bank.entity.BankLocation.bankFormat -> bank.entity.RefBankFormat;
nested exception is java.lang.IllegalStateException: org.hibernate.TransientPropertyValueException: Not-null property references a transient value - transient instance must be saved before current operation :
bank.entity.BankLocation.bankFormat -> bank.entity.RefBankFormat
@Entity
@Table(name = "BANK_LOCATION", schema = "OWNR")
public class BankLocation implements Serializable {
private static final long serialVersionUID = 1L;
@Id
@Basic(optional = false)
@NotNull
@Column(name = "BANK_LOCATION_ID")
@GeneratedValue(strategy = GenerationType.SEQUENCE, generator = "BANK_LOCATION_ID_SEQ")
@SequenceGenerator(name = "BANK_LOCATION_ID_SEQ", sequenceName = "OWNR.BANK_LOCATION_ID_SEQ", allocationSize = 1)
private Long bankLocationId;
@Size(max = 32)
@Column(name = "BANK_NAME")
private String bankName;
@JoinColumn(name = "BANK_FORMAT_ID", referencedColumnName = "BANK_FORMAT_ID")
@ManyToOne(targetEntity=RefBankFormat.class, optional = false)
private RefBankFormat bankFormat;
public RefBankFormat getBankFormat() {
return bankFormat;
}
public void setBankFormat(RefBankFormat bankFormat) {
this.bankFormat = bankFormat;
}
@Override
public int hashCode() {
int hash = 0;
hash += (bankLocationId != null ? bankLocationId.hashCode() : 0);
return hash;
}
@Override
public boolean equals(Object object) {
// TODO: Warning - this method won't work in the case the id fields are not set
if (!(object instanceof BankLocation)) {
return false;
}
BankLocation other = (BankLocation) object;
if ((this.bankLocationId == null && other.bankLocationId != null) || (this.bankLocationId != null && !this.bankLocationId.equals(other.bankLocationId))) {
return false;
}
return true;
}
@Override
public String toString() {
return "bank.entity.BankLocation[ bankLocationId=" + bankLocationId + " ]";
}
}
引用表实体
@Entity
@Table(name = "REF_BANK_FORMAT", schema = "OWNR")
public class RefBankFormat implements Serializable {
private static final long serialVersionUID = 1L;
@Id
@Basic(optional = false)
@NotNull
@Column(name = "BANK_FORMAT_ID")
private Integer bankFormatId;
@Basic(optional = false)
@NotNull
@Size(min = 1, max = 50)
@Column(name = "DISPLAY_NAME")
private String displayName;
@Size(max = 50)
@Column(name = "DESCRIPTION")
private String description;
public RefBankFormat() {
}
public RefBankFormat(Integer bankFormatId) {
this.bankFormatId = bankFormatId;
}
public RefBankFormat(Integer bankFormatId, String displayName) {
this.bankFormatId = bankFormatId;
this.displayName = displayName;
}
public Integer getbankFormatId() {
return bankFormatId;
}
public void setbankFormatId(Integer bankFormatId) {
this.bankFormatId = bankFormatId;
}
public String getDisplayName() {
return displayName;
}
public void setDisplayName(String displayName) {
this.displayName = displayName;
}
public String getDescription() {
return description;
}
public void setDescription(String description) {
this.description = description;
}
@Override
public int hashCode() {
int hash = 0;
hash += (bankFormatId != null ? bankFormatId.hashCode() : 0);
return hash;
}
@Override
public boolean equals(Object object) {
// TODO: Warning - this method won't work in the case the id fields are not set
if (!(object instanceof RefBankFormat)) {
return false;
}
RefBankFormat other = (RefBankFormat) object;
if ((this.bankFormatId == null && other.bankFormatId != null) || (this.bankFormatId != null && !this.bankFormatId.equals(other.bankFormatId))) {
return false;
}
return true;
}
@Override
public String toString() {
return "bank.entity.RefBankFormat[ bankFormatId=" + bankFormatId + " ]";
}
}
有人能帮我解决哪里出了问题吗?
2条答案
按热度按时间xggvc2p61#
bankformat是强制设置的(不为空),因此您首先必须将此对象保存在数据库中,然后才允许保存banklocation对象。
或
定义可传递持久性的策略,即级联类型。
hwamh0ep2#
你差不多问了两次同样的问题,答案和这里一样:https://stackoverflow.com/a/64926449/412446