在 JPA、Hibernate 中连接 2 个带有非键列的表
Joining 2 tables with non-key columns in JPA, Hibernate
我正在使用具有 tables skl_transactions
和 merchant_config
的遗留数据库。这两个 tables 有一个列,其中它们的值彼此相等(例如 skl_transactions.column_a
=== merchant_config.column.b
。我正在寻找一种方法来加入两个 tables基于该公共值而不是查询数据库两次。
看看我的想法,但我总是出错
(" => "org.hibernate.MappingException: Unable to find column with logical name: column_a in org.hibernate.mapping.Table(merchant_config) and its related supertables and secondary tables
Caused by: org.hibernate.MappingException: Unable to find column with logical name: column_a in org.hibernate.mapping.Table(merchant_config) and its related supertables and secondary tables"}}
下面是我的实体class
@Entity
@Table(name = "skl_transactions")
public class Foo implements Serializable {
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
@Basic(optional = false)
protected Long id;
@Column(name = "column_a")
private String senderAddress;
-----
@ManyToOne(fetch = FetchType.LAZY)
@JoinColumn(name="column_b", referencedColumnName = "column_a")
private Bar merchant;
// getters and setters
}
@Entity
@Table(name = "merchant_config")
public class Bar implements Serializable {
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
@Basic(optional = false)
protected Long id;
@Column(name = "column_b")
private String merchantAddress;
-----
@OneToMany(mappedBy = "merchant_config", fetch = FetchType.LAZY)
private List<Foo> transactions = new ArrayList<>();
// getters and setters
}
注意:两列都未标记为主键或外键。
我正在寻找的是当我从 skl_transactions
table 查询交易时,它会加入从 merchant_config
table 进行交易的商家。
您需要将 mappedBy
更改为 merchant
Foo
@Entity
@Table(name = "skl_transactions")
public class Foo implements Serializable {
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
@Basic(optional = false)
protected Long id;
@Column(name = "column_a")
private String senderAddress;
-----
@ManyToOne(fetch = FetchType.LAZY)
@JoinColumn(name="column_b", referencedColumnName = "column_a", insertable=false, updatable=false)
private Bar merchant;
// getters and setters
}
栏
@Entity
@Table(name = "merchant_config")
public class Bar implements Serializable {
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
@Basic(optional = false)
protected Long id;
@Column(name = "column_b")
private String merchantAddress;
-----
@OneToMany(mappedBy = "merchant", fetch = FetchType.LAZY)
private List<Foo> transactions = new ArrayList<>();
// getters and setters
}
如果以上有任何问题,请尝试将 name
和 referencedColumnName
反过来
in Foo
@ManyToOne(fetch = FetchType.LAZY)
@JoinColumn(name="column_a", referencedColumnName = "column_b", insertable=false, updatable=false)
private Bar merchant;
in Bar
@OneToMany(mappedBy = "merchant", fetch = FetchType.LAZY)
private List<Foo> transactions = new ArrayList<>();
我正在使用具有 tables skl_transactions
和 merchant_config
的遗留数据库。这两个 tables 有一个列,其中它们的值彼此相等(例如 skl_transactions.column_a
=== merchant_config.column.b
。我正在寻找一种方法来加入两个 tables基于该公共值而不是查询数据库两次。
看看我的想法,但我总是出错
(" => "org.hibernate.MappingException: Unable to find column with logical name: column_a in org.hibernate.mapping.Table(merchant_config) and its related supertables and secondary tables
Caused by: org.hibernate.MappingException: Unable to find column with logical name: column_a in org.hibernate.mapping.Table(merchant_config) and its related supertables and secondary tables"}}
下面是我的实体class
@Entity
@Table(name = "skl_transactions")
public class Foo implements Serializable {
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
@Basic(optional = false)
protected Long id;
@Column(name = "column_a")
private String senderAddress;
-----
@ManyToOne(fetch = FetchType.LAZY)
@JoinColumn(name="column_b", referencedColumnName = "column_a")
private Bar merchant;
// getters and setters
}
@Entity
@Table(name = "merchant_config")
public class Bar implements Serializable {
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
@Basic(optional = false)
protected Long id;
@Column(name = "column_b")
private String merchantAddress;
-----
@OneToMany(mappedBy = "merchant_config", fetch = FetchType.LAZY)
private List<Foo> transactions = new ArrayList<>();
// getters and setters
}
注意:两列都未标记为主键或外键。
我正在寻找的是当我从 skl_transactions
table 查询交易时,它会加入从 merchant_config
table 进行交易的商家。
您需要将 mappedBy
更改为 merchant
Foo
@Entity
@Table(name = "skl_transactions")
public class Foo implements Serializable {
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
@Basic(optional = false)
protected Long id;
@Column(name = "column_a")
private String senderAddress;
-----
@ManyToOne(fetch = FetchType.LAZY)
@JoinColumn(name="column_b", referencedColumnName = "column_a", insertable=false, updatable=false)
private Bar merchant;
// getters and setters
}
栏
@Entity
@Table(name = "merchant_config")
public class Bar implements Serializable {
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
@Basic(optional = false)
protected Long id;
@Column(name = "column_b")
private String merchantAddress;
-----
@OneToMany(mappedBy = "merchant", fetch = FetchType.LAZY)
private List<Foo> transactions = new ArrayList<>();
// getters and setters
}
如果以上有任何问题,请尝试将 name
和 referencedColumnName
反过来
in Foo
@ManyToOne(fetch = FetchType.LAZY)
@JoinColumn(name="column_a", referencedColumnName = "column_b", insertable=false, updatable=false)
private Bar merchant;
in Bar
@OneToMany(mappedBy = "merchant", fetch = FetchType.LAZY)
private List<Foo> transactions = new ArrayList<>();