使用 JPA 连接两个表
Joining two tables with JPA
我正在尝试使用 @NamedQuery
连接两个表,Address
和 Division
。同一地址有多个部门。目前我有
Division.java:
@Entity
@Table(name="division")
@NamedQuery(name="Division.mailingAddress", query="SELECT d, a from Division d LEFT JOIN d.addresses a")
public class Division implements Serializable{
private static final long serialVersionUID = 1L;
@Id
@Basic(optional = false)
@Column(name="division_id")
private Short divisionId;
@Basic(optional = false)
@Column(name="division_name")
private String divisionName;
@Basic(optional = false)
@Column(name="address_id")
private Short addressId;
@OneToMany(mappedBy="divisionMailingAddress")
private List<Address> addresses;
//getters and setters
}
Addresses.java:
@Entity
@Table(name="address")
public class Address implements Serializable{
private static final long serialVersionUID = 1L;
@Id
@GeneratedValue(strategy = GeneratedType.IDENTITY)
@Basic(optional = false)
@Column(name = "address_id")
private Short addressID;
@Basic(optional = false)
@Column(name = "mailing_address")
private String mailingAddress;
@ManyToOne
private Division divisionMailingAddress;
}
我在 运行 时得到的错误是:
Unknown column 't0.DIVISIONMAILINGADDRESS_division_id' in 'on clause'
我想要得到的是在 MySQL 中有效的这一行:
SELECT * FROM (division d join address a on d.address_id = a.address_id);
目前 JPA 正在将命名查询解析为:
SELECT t1."all columns", t0."all columns" from division t1 LEFT OUTER JOIN address t0 ON (t0.DIVISIONMAILINGADDRESS_division_id = t1.division_id)
我在做什么有什么问题? JPA 是否希望连接来自地址而不是部门?我如何获得 address_id
与 division_id
之间的通话?
更新:
在大力帮助下,这两个 类 已被更改以实现我想要的结果。
Division.java:
@Entity
@Table(name="division")
@NamedQuery(name="Division.mailingAddress", query="SELECT d from Division d LEFT JOIN d.addressesM2O a")
public class Division implements Serializable{
private static final long serialVersionUID = 1L;
@Id
@Basic(optional = false)
@Column(name="division_id")
private Short divisionId;
@Basic(optional = false)
@Column(name="division_name")
private String divisionName;
@Basic(optional = false)
@Column(name="address_id")
private Short addressId;
@ManyToOne(targetEntity = Address.java)
@JoinColumn(name = "address_id", updatable=false, insertable=false)
private Address addressesM2O;
//getters and setters
}
Address.java:
@Entity
@Table(name="address")
public class Address implements Serializable{
private static final long serialVersionUID = 1L;
@Id
@GeneratedValue(strategy = GeneratedType.IDENTITY)
@Basic(optional = false)
@Column(name = "address_id")
private Short addressID;
@Basic(optional = false)
@Column(name = "mailing_address")
private String mailingAddress;
@OneToMany(mappedBy="addressesM2O")
private List<Division> divisionO2M;
}
@NamedQuery
已更改,因为 d
和 a
的存在使结果列表 return 成为 Object[]
的列表。 Division
和 Address
是对象。这就是为什么 d
被保留而 a
从 @NamedQuery
.
中删除的原因
我认为问题是您在地址 class 中提到 @ManyToOne
时没有提到 @JoinColumn
,因此在进行连接时 JPA 本身正在创建外部键 DIVISIONMAILINGADDRESS_division_id.
尝试在地址 class 中提及 ManyToOne 关系,如有任何问题请告诉我:
@ManyToOne(targetEntity = Division.class)
@JoinColumn(name= "division_id")//basically the name of the column here should be same as the one mentioned in your database
对我有用的是将 @OneToMany
和 @ManyToOne
更改为 @OneToOne
,在 address
table 中使用 @PrimaryKeyJoinColumn
。代码更新如下:
Division.java 更改为:
@OneToOne(mappedBy="divisionMailingAddress")
private Address address;
Address.java改为:
@OneToOne
@PrimaryKeyJoinColumn
private Division divisionMailingAddress;
我正在尝试使用 @NamedQuery
连接两个表,Address
和 Division
。同一地址有多个部门。目前我有
Division.java:
@Entity
@Table(name="division")
@NamedQuery(name="Division.mailingAddress", query="SELECT d, a from Division d LEFT JOIN d.addresses a")
public class Division implements Serializable{
private static final long serialVersionUID = 1L;
@Id
@Basic(optional = false)
@Column(name="division_id")
private Short divisionId;
@Basic(optional = false)
@Column(name="division_name")
private String divisionName;
@Basic(optional = false)
@Column(name="address_id")
private Short addressId;
@OneToMany(mappedBy="divisionMailingAddress")
private List<Address> addresses;
//getters and setters
}
Addresses.java:
@Entity
@Table(name="address")
public class Address implements Serializable{
private static final long serialVersionUID = 1L;
@Id
@GeneratedValue(strategy = GeneratedType.IDENTITY)
@Basic(optional = false)
@Column(name = "address_id")
private Short addressID;
@Basic(optional = false)
@Column(name = "mailing_address")
private String mailingAddress;
@ManyToOne
private Division divisionMailingAddress;
}
我在 运行 时得到的错误是:
Unknown column 't0.DIVISIONMAILINGADDRESS_division_id' in 'on clause'
我想要得到的是在 MySQL 中有效的这一行:
SELECT * FROM (division d join address a on d.address_id = a.address_id);
目前 JPA 正在将命名查询解析为:
SELECT t1."all columns", t0."all columns" from division t1 LEFT OUTER JOIN address t0 ON (t0.DIVISIONMAILINGADDRESS_division_id = t1.division_id)
我在做什么有什么问题? JPA 是否希望连接来自地址而不是部门?我如何获得 address_id
与 division_id
之间的通话?
更新:
在大力帮助下,这两个 类 已被更改以实现我想要的结果。
Division.java:
@Entity
@Table(name="division")
@NamedQuery(name="Division.mailingAddress", query="SELECT d from Division d LEFT JOIN d.addressesM2O a")
public class Division implements Serializable{
private static final long serialVersionUID = 1L;
@Id
@Basic(optional = false)
@Column(name="division_id")
private Short divisionId;
@Basic(optional = false)
@Column(name="division_name")
private String divisionName;
@Basic(optional = false)
@Column(name="address_id")
private Short addressId;
@ManyToOne(targetEntity = Address.java)
@JoinColumn(name = "address_id", updatable=false, insertable=false)
private Address addressesM2O;
//getters and setters
}
Address.java:
@Entity
@Table(name="address")
public class Address implements Serializable{
private static final long serialVersionUID = 1L;
@Id
@GeneratedValue(strategy = GeneratedType.IDENTITY)
@Basic(optional = false)
@Column(name = "address_id")
private Short addressID;
@Basic(optional = false)
@Column(name = "mailing_address")
private String mailingAddress;
@OneToMany(mappedBy="addressesM2O")
private List<Division> divisionO2M;
}
@NamedQuery
已更改,因为 d
和 a
的存在使结果列表 return 成为 Object[]
的列表。 Division
和 Address
是对象。这就是为什么 d
被保留而 a
从 @NamedQuery
.
我认为问题是您在地址 class 中提到 @ManyToOne
时没有提到 @JoinColumn
,因此在进行连接时 JPA 本身正在创建外部键 DIVISIONMAILINGADDRESS_division_id.
尝试在地址 class 中提及 ManyToOne 关系,如有任何问题请告诉我:
@ManyToOne(targetEntity = Division.class)
@JoinColumn(name= "division_id")//basically the name of the column here should be same as the one mentioned in your database
对我有用的是将 @OneToMany
和 @ManyToOne
更改为 @OneToOne
,在 address
table 中使用 @PrimaryKeyJoinColumn
。代码更新如下:
Division.java 更改为:
@OneToOne(mappedBy="divisionMailingAddress")
private Address address;
Address.java改为:
@OneToOne
@PrimaryKeyJoinColumn
private Division divisionMailingAddress;