Hibernate ManyToOne映射SQL生成错误

时间:2019-07-11 17:37:31

标签: hibernate hibernate-mapping

我在雇主和地址之间有一个双向的一对多映射。急切地获取地址。查询雇主记录时,hibernate生成的sql出现错误,导致SQLSyntaxErrorException

java.sql.SQLSyntaxErrorException: ORA-00904: "ADDRESSES0_"."EMPLOYER_EMPR_ACCT_ID": invalid identifier.

我提供了映射和生成的sql。

此映射在OpenJPA中可以正常工作,但在Hibernate中却无法正常工作。映射似乎是正确的,但不确定为什么生成的sql中的列名不正确。非常感谢您对此提供任何帮助。

@Entity
@Table(name = "UI_EMPLOYER")
public class Employer extends AbstractAuditableEntity {

    @Id
    @Column(name = "EMPR_ACCT_ID", nullable = false, updatable = false,
    length = 8)
    @Min(value = 0)
    @Max(value = 99999999)
    private Long employerAccountId;

// bi-directional many-to-one association to EmployerAddress
    @OneToMany(mappedBy = "employer", cascade = ALL, , fetch = EAGER)
    @OrderBy
    private List<EmployerAddress> addresses;
}

@Entity
@Table(name = "UI_EMPR_ADDR")
public class EmployerAddress extends AbstractAuditableEntity {

    @EmbeddedId
    private EmployerAddressPK id;

// bi-directional many-to-one association to Employer
    @ManyToOne(optional = false)
    @PrimaryKeyJoinColumn(name = "EMPR_ACCT_ID",
        referencedColumnName = "EMPR_ACCT_ID")
    private Employer employer;

// bi-directional many-to-one association to Address
    @ManyToOne(optional = false, cascade = ALL)
    @JoinColumn(name = "UI_ADDRESS_SEQ_ID",
        referencedColumnName = "UI_ADDRESS_SEQ_ID")
    private Address address;
}

@Embeddable
public class EmployerAddressPK implements Serializable {

    @Column(name = "ADDR_TYP_CD", length = 4, nullable = false,
        updatable = false)
    @Enumerated(STRING)
    @NotNull
    private AddressType addressType;

    @Column(name = "EMPR_ACCT_ID", updatable = false, length = 8,
        nullable = false)
    private Long employerAccountId;
}

@Entity
@Table(name = "UI_ADDRESS")
public class Address extends AbstractAuditableEntity {

    @Id
    @Column(name = "UI_ADDRESS_SEQ_ID", length = 10, nullable = false,
        updatable = false)
    @GeneratedValue(strategy = SEQUENCE, generator = "UI_ADDRESS_SEQ")
    @SequenceGenerator(name = "UI_ADDRESS_SEQ", sequenceName = "UI_ADDRESS_SEQ",
        allocationSize = 1)
    private Long addressId;

// bi-directional many-to-one association to EmployerAddress
    @OneToMany(mappedBy = "address")
    private List<EmployerAddress> employerAddresses;

}

hibernate生成的查询是:

SELECT
    addresses0_.employer_empr_acct_id AS employer_empr_acct9_9_0_,
    addresses0_.addr_typ_cd AS addr_typ_cd1_9_0_,
    addresses0_.empr_acct_id AS empr_acct_id2_9_0_,
    addresses0_.addr_typ_cd AS addr_typ_cd1_9_1_,
    addresses0_.empr_acct_id AS empr_acct_id2_9_1_,
    addresses0_.ui_address_seq_id AS ui_address_seq_id8_9_1_,
    addresses0_.employer_empr_acct_id AS employer_empr_acct9_9_1_,
    address1_.ui_address_seq_id AS ui_address_seq_id1_3_2_,
    address1_.addr_1 AS addr_6_3_2_,
    address1_.addr_2 AS addr_7_3_2_
FROM
    uitax.ui_empr_addr addresses0_
    INNER JOIN uitax.ui_address address1_ ON addresses0_.ui_address_seq_id = address1_.ui_address_seq_id
WHERE
    addresses0_.employer_empr_acct_id =?
ORDER BY
    addresses0_.addr_typ_cd ASC,
    addresses0_.empr_acct_id ASC

0 个答案:

没有答案
相关问题