下面是我的JPA實體的片段。當我用Hibernate DB2 dialect在JBOSS中運行它時,生成的select查詢使用acctYymm而不是ACCT_YYMM導致-206異常。JPA + JBOSS Hibernate NamedQuery不使用列名稱
@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並加入兩個表之前,我認爲這個問題沒有發生。會不會有任何關係?
添加我的代碼:
兩個汽車和池實體份額PK嵌入類
@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;
}
}
非常感謝你@ vlad-mihalcea。它工作得很好。我應該閱讀一些訪問類型。 – Mjasmin
節省一些麻煩並閱讀[高性能Java持久性](https://vladmihalcea.com/books/high-performance-java-persistence/)。您只需享受快速運行您的應用程序,不是嗎? –
我敢打賭,這值得一讀。只是好奇,它有平裝版嗎?此外,任何計劃寫一本OpenJPA書籍? :) – Mjasmin