2017-07-19 73 views
1

下面是我的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; 
} 

} 

回答

0

如果你仔細看看你的映射,你」你會發現你混合了財產和基於現場的訪問。

您需要的@EmbeddedId註釋從外地遷移到屬性:

private PK pk = null; 

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

這也適用於PoolEntity poolEntity;PoolEntitypk屬性。

您需要對這些映射保持一致,否則,Hibernate不會找到它們。

+0

非常感謝你@ vlad-mihalcea。它工作得很好。我應該閱讀一些訪問類型。 – Mjasmin

+0

節省一些麻煩並閱讀[高性能Java持久性](https://vladmihalcea.com/books/high-performance-java-persistence/)。您只需享受快速運行您的應用程序,不是嗎? –

+0

我敢打賭,這值得一讀。只是好奇,它有平裝版嗎?此外,任何計劃寫一本OpenJPA書籍? :) – Mjasmin