我正在尝试将@JoinColumn这两个实体命名为Customer和Account。一个客户可以拥有多个帐户,因此我认为在客户实体中使用@OneToMany是正确的。和@ManyToOne帐户实体中。但是,它一直在控制台中为我提供“重复”列名称。 可以通过更改变量名来解决,但是我不能简单地更改它,因为hibernate不会在数据库中找到列名。甚至添加@Column name属性也行不通。
有什么办法可以解决这个问题?
我尝试了以下操作:
我已尝试在数据库中执行SQL查询。该查询是可行的,但我不知道如何在休眠jpa中映射它。
Oracle查询
SELECT a.USR_ID, a.USR_MGNT_NO, a.CUST_NO, a.ACCT_STATUS,
b.ACCT_SEQ, b.ACCT_NO, b.CUST_NO, b.CUST_NM, b.SBJ_CD, b.ACCT_PRD_NM
FROM LB_USR_BASE a LEFT OUTER JOIN
LB_ACCT_BASE b ON
a.CUST_NO=b.CUST_NO
WHERE a.USR_MGNT_NO=:usrMgtNo;
客户实体
@Entity
@Table(name = "CUSTOMER")
@Data
@Builder
@AllArgsConstructor
@NoArgsConstructor
public class Customer implements Serializable{
private static final long serialVersionUID = 1L;
@Id
private String custNo;
private String usrId;
private String usrMgntNo;
private String acctStatus;
@OneToMany(cascade = CascadeType.ALL, mappedBy = "user")
@JoinColumn(name = "custNo")
private Set<Account> accounts = new HashSet<Account>();
}
帐户实体
@Entity
@Table(name = "ACCOUNT")
@Data
@Builder
@AllArgsConstructor
@NoArgsConstructor
public class Account {
@Id
private String acctId;
private String acctNo;
@ManyToOne
private UsrBase user;
private String sbjCd;
private String custNo;
}
存储库
public interface CustAcctRepository extends JpaRepository<Customer, String>{
List<Customer> findAcctByUsrMgntNo(String usrMgntNo);
}
服务/控制器
List<Customer> tes = CustAcctRepo.findAcctByUsrMgntNo("12345"));
Caused by: org.hibernate.DuplicateMappingException: Table [ACCOUNT] contains physical column name [cust_no] referred to by multiple physical column names: [cust_no], [custNo]
==========
我修改了两个实体,如下所示:
public class Account {
@Id
private String acctSeq;
private String acctNo;
@ManyToOne(fetch = FetchType.LAZY)
@JoinColumn(name = "custNo",insertable = false, updatable = false)
private UsrBase user;
}
和
public class Customer implements Serializable{
private static final long serialVersionUID = 1L;
@Id
private String custNo;
private String usrId;
private String usrMgntNo;
private String acctStatus;
@OneToMany(cascade = CascadeType.ALL)
private Set<AcctBase> accounts = new HashSet<AcctBase>();
}
答案 0 :(得分:0)
请参见,在下面的代码中,您正在使用两个字段创建映射:“用户”和“ custNo”。使用用户或Account.custNo
进行映射@OneToMany(cascade = CascadeType.ALL, mappedBy = "user")
@JoinColumn(name = "custNo")
private Set<Account> accounts = new HashSet<Account>();