我正在使用spring数据jpa,并希望更改实体之间发生的默认连接列。例如,在以下映射中,
在Account.java中,
@OneToMany(mappedBy = "account")
@Cache(usage = CacheConcurrencyStrategy.NONSTRICT_READ_WRITE)
private Set<Invoice> invoices = new HashSet<>();
在Invoice.java
中@ManyToOne
private Account account;
默认情况下,id
的{{1}}列与Account
account_id
的{{1}}按照给定的一对多和多对一的映射加入,这也是有效的对于我来说足够了。但现在我的要求是Invoice
account_id
加入Invoice
的{{1}}而不是id
,我希望Account
的另一个字段account_number
成为Invoice
加入id
的{{1}},在我的一对多和多对一的映射中。可能吗?作为一条线索,我对Account
Invoice
但是我收到以下错误:
@ManyToOne
@JoinColumn(name="account_number", nullable=false)
private Account account;
Account.java
Caused by: org.hibernate.MappingException: Repeated column in mapping for entity: com.trace.domain.Invoice column: account_number (should be mapped with insert="false" update="false")
Invoice.java
/**
* Account
*/
@Entity
@Table(name = "account")
@Cache(usage = CacheConcurrencyStrategy.NONSTRICT_READ_WRITE)
public class Account implements Serializable {
private static final long serialVersionUID = 1L;
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
private Long id;
@NotNull
@Column(name = "number", nullable = false)
private String number;
@NotNull
@Column(name = "name")
private String name;
@Column(name = "currency_code")
private String currencyCode;
@Column(name = "edi_type")
private String ediType;
@Column(name = "is_fedex_express_gsr")
private Boolean isFedexExpressGsr;
@Column(name = "is_fedex_ground_gsr")
private Boolean isFedexGroundGsr;
@Column(name = "is_ups_gsr")
private Boolean isUpsGsr;
@Column(name = "electronic_voiding")
private Boolean electronicVoiding;
@Column(name = "activate_signature_service")
private Boolean activateSignatureService;
@Column(name = "reject_invoices")
private Boolean rejectInvoices;
@Column(name = "notify_client_services")
private Boolean notifyClientServices;
@Column(name = "is_active")
private Boolean isActive;
@Column(name = "address")
private String address;
@Column(name = "city")
private String city;
@Column(name = "state")
private String state;
@Column(name = "postal_code")
private String postalCode;
@OneToMany(mappedBy = "account")
@Cache(usage = CacheConcurrencyStrategy.NONSTRICT_READ_WRITE)
private Set<Invoice> invoices = new HashSet<>();
@ManyToOne
private Courier courier;
@ManyToOne
private Client client;
@ManyToMany
@JoinTable(
name = "account_group_members",
joinColumns = {@JoinColumn(name = "account_id", referencedColumnName = "id")},
inverseJoinColumns = {@JoinColumn(name = "group_id", referencedColumnName = "id")})
@Cache(usage = CacheConcurrencyStrategy.NONSTRICT_READ_WRITE)
private Set<AccountGroup> accountGroups = new HashSet<>();
public Long getId() {
return id;
}
public void setId(Long id) {
this.id = id;
}
public String getNumber() {
return number;
}
public void setNumber(String number) {
this.number = number;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getCurrencyCode() {
return currencyCode;
}
public void setCurrencyCode(String currencyCode) {
this.currencyCode = currencyCode;
}
public String getEdiType() {
return ediType;
}
public void setEdiType(String ediType) {
this.ediType = ediType;
}
public Boolean getIsFedexExpressGsr() {
return isFedexExpressGsr;
}
public void setIsFedexExpressGsr(Boolean isFedexExpressGsr) {
this.isFedexExpressGsr = isFedexExpressGsr;
}
public Boolean getIsFedexGroundGsr() {
return isFedexGroundGsr;
}
public void setIsFedexGroundGsr(Boolean isFedexGroundGsr) {
this.isFedexGroundGsr = isFedexGroundGsr;
}
public Boolean getIsUpsGsr() {
return isUpsGsr;
}
public void setIsUpsGsr(Boolean isUpsGsr) {
this.isUpsGsr = isUpsGsr;
}
public Boolean getElectronicVoiding() {
return electronicVoiding;
}
public void setElectronicVoiding(Boolean electronicVoiding) {
this.electronicVoiding = electronicVoiding;
}
public Boolean getActivateSignatureService() {
return activateSignatureService;
}
public void setActivateSignatureService(Boolean activateSignatureService) {
this.activateSignatureService = activateSignatureService;
}
public Boolean getRejectInvoices() {
return rejectInvoices;
}
public void setRejectInvoices(Boolean rejectInvoices) {
this.rejectInvoices = rejectInvoices;
}
public Boolean getNotifyClientServices() {
return notifyClientServices;
}
public void setNotifyClientServices(Boolean notifyClientServices) {
this.notifyClientServices = notifyClientServices;
}
public Boolean getIsActive() {
return isActive;
}
public void setIsActive(Boolean isActive) {
this.isActive = isActive;
}
public String getAddress() {
return address;
}
public void setAddress(String address) {
this.address = address;
}
public String getCity() {
return city;
}
public void setCity(String city) {
this.city = city;
}
public String getState() {
return state;
}
public void setState(String state) {
this.state = state;
}
public String getPostalCode() {
return postalCode;
}
public void setPostalCode(String postalCode) {
this.postalCode = postalCode;
}
public Courier getCourier() {
return courier;
}
public void setCourier(Courier courier) {
this.courier = courier;
}
public Client getClient() {
return client;
}
public void setClient(Client client) {
this.client = client;
}
public Set<Invoice> getInvoices() {
return invoices;
}
public void setInvoices(Set<Invoice> invoices) {
this.invoices = invoices;
}
@Override
public boolean equals(Object o) {
if (this == o) {
return true;
}
if (o == null || getClass() != o.getClass()) {
return false;
}
Account shipper = (Account) o;
if (!Objects.equals(id, shipper.id))
return false;
return true;
}
@Override
public int hashCode() {
return Objects.hashCode(id);
}
@Override
public String toString() {
return "Account{" + "id=" + id + ", number='" + number + "'" + ", name='" + name + "'" + ", currencyCode='"
+ currencyCode + "'" + ", ediType='" + ediType + "'" + ", isFedexExpressGsr='" + isFedexExpressGsr + "'"
+ ", isFedexGroundGsr='" + isFedexGroundGsr + "'" + ", isUpsGsr='" + isUpsGsr + "'"
+ ", electronicVoiding='" + electronicVoiding + "'" + ", activateSignatureService='"
+ activateSignatureService + "'" + ", rejectInvoices='" + rejectInvoices + "'"
+ ", notifyClientServices='" + notifyClientServices + "'" + ", isActive='" + isActive + "'"
+ ", address='" + address + "'" + ", city='" + city + "'" + ", state='" + state + "'" + ", postalCode='"
+ postalCode + "'" + '}';
}
}
答案 0 :(得分:1)
正如错误消息所示,同一列有两种不同的映射:account_number。事实上,你有
@Column(name = "account_number")
private String accountNumber;
在您的实体中,该帐户字段是多余的。
您只需删除该字段。