我正在尝试使用@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
发生了变化,结果列表返回了Object[]
的列表。哪个是对象Division
和Address
。这就是为什么保留d
并将a
从@NamedQuery
中删除的原因。
答案 0 :(得分:1)
我认为问题在于,您在Address类中没有提到@JoinColumn
时却提到了@ManyToOne
,因此在进行联接时,JPA本身正在创建外键 DIVISIONMAILINGADDRESS_division_id < / em>。
尝试如下在Address类中提及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
答案 1 :(得分:0)
对我有用的是将@OneToMany
表中的@ManyToOne
和@OneToOne
更改为@PrimaryKeyJoinColumn
并变成address
。代码更新如下:
Division.java更改为:
@OneToOne(mappedBy="divisionMailingAddress")
private Address address;
Address.java更改为:
@OneToOne
@PrimaryKeyJoinColumn
private Division divisionMailingAddress;