用JPA连接两个表

时间:2018-07-26 20:22:35

标签: jpa netbeans eclipselink named-query

我正在尝试使用@NamedQuery连接两个表AddressDivision。同一地址有多个部门。目前我有
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_iddivision_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;
}

由于@NamedQueryd的出现使a发生了变化,结果列表返回了Object[]的列表。哪个是对象DivisionAddress。这就是为什么保留d并将a@NamedQuery中删除的原因。

2 个答案:

答案 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;