JPA + JBOSS Hibernate NamedQuery不使用列名

时间:2017-07-19 17:36:07

标签: hibernate jpa

以下是我的JPA实体的片段。当我在JBOSS中使用Hibernate DB2方言运行它时,生成的select查询使用acctYymm而不是ACCT_YYMM导致-206异常。

@Column(name = "ACCT_YYMM")
public String getAcctYymm() {
  return acctYymm;
}

SqlSyntaxErrorException:DB2 SQL错误:SQLCODE = -206,SQLSTATE = 42703,SQLERRMC = CARTUMLERE0 _。 ACCTYYMM ,DRIVER = 3.62.56

我尝试将命名策略更改为EJB3NamingStrategy和DefaultComponentSafeNamingStrategy,它们似乎都没有使用列名。在我实现EmbeddedId并加入两个表之前,我认为这个问题没有发生。可能有任何共同关系吗?

添加我的代码:

Car和Pool Entity共享PK embeddable class

@Embeddable
public class PK implements Serializable {
private String acctYymm;
private String carInit;
private String carNum;

@Column(name = "ACCT_YYMM", nullable = false, length = 4)
public String getAcctYymm() {
    return acctYymm;
}

//skipping getters and setters

@Column(name = "CAR_INIT", nullable = false, length = 4)
public String getCarInit() {
    return carInit;
}

@Column(name = "CAR_NUM", nullable = false, length = 6)
public String getCarNum() {
    return carNum;
}

}
@Entity
@NamedQuery(name = "Car.findAll", query = "select umler from CarEntity umler join umler.poolEntity pool where umler.pk.acctYymm = :acctYymm and umler.pk.carInit = :carInit and umler.pk.carNum = :carNum")
@Table(name = "CART_UMLER", schema = "CARSCHEMA", catalog = "")
public class CarEntity implements Serializable{

@EmbeddedId
private PK pk = null;

public PK getPk() {
    return pk;
}

public void setPk(PK pk) {
    this.pk = pk;
}


@OneToOne
@PrimaryKeyJoinColumns({
        @PrimaryKeyJoinColumn(name="ACCT_YYMM", referencedColumnName="ACCT_YYMM"),
        @PrimaryKeyJoinColumn(name="CAR_INIT", referencedColumnName="CAR_INIT"),
        @PrimaryKeyJoinColumn(name="CAR_NUM", referencedColumnName="CAR_NUM")
})
private PoolEntity poolEntity;

private String carTyp;

@Basic
@Column(name = "CAR_TYP", nullable = false, length = 1)
public String getCarTyp() {
    return carTyp;
}

//skipping getters and setters
}
@Entity
@Table(name = "POOL", schema = "CARSCHEMA", catalog = "")
public class PoolEntity implements Serializable{

@EmbeddedId
private PK pk = null;

public PK getPk() {
    return pk;
}

public void setPk(PK pk) {
    this.pk = pk;
}

private String poolCd;

@Basic
@Column(name = "POOL_CD", nullable = false, length = 7)
public String getPoolCd() {
    return poolCd;
}

public void setPoolCd(String poolCd) {
    this.poolCd = poolCd;
}

}

1 个答案:

答案 0 :(得分:0)

如果仔细查看映射,您会发现混合属性和基于字段的访问权限。

您需要将@EmbeddedId注释从字段移动到属性:

private PK pk = null;

@EmbeddedId
public PK getPk() {
    return pk;
}

适用于PoolEntity poolEntity;PoolEntity pk属性。

你需要对这些映射保持一致,否则,Hibernate不会找到它们。