2012-05-15 31 views
0

我在MySQL數據庫的兩個表:奇怪值java.sql.SQLException:列 'fk_Content' 未找到

1活動:

  • PKID:主鍵。
  • fk_Content:內容表上的外鍵。
  • tk_DistributionGroup:類型表distribution_group上的外鍵。

2-的含量:

  • PKID:主鍵。
  • tk_contentSubtype:類型表distribution_list上的外鍵。

我的java bean(不冬眠實體)稱爲CampaignData

public class CampaignData { 

    private long contentId; 
    private long contentSubTypeId; 
    private Long distributionGroupId; 

} 

這裏就是我如何做查詢:

CampaignData campaignData = (CampaignData) session 
       .createSQLQuery(
         "select camp.fk_Content as contentId,camp.tk_DistributionGroup as distributionGroupId,cont.tk_contentSubtype as contentSubTypeId " 
           + "from campaign camp,content cont" 
           + " where camp.pkid=:campaignId and camp.fk_Content=cont.pkid") 
       .setLong("campaignId", campaignId) 
       .setResultTransformer(
         Transformers.aliasToBean(CampaignData.class)) 
       .uniqueResult(); 

產生Hibernate查詢:

select 
     camp.fk_Content as contentId, 
     camp.tk_DistributionGroup as distributionGroupId, 
     cont.tk_contentSubtype as contentSubTypeId 
    from 
     campaign camp, 
     content cont 
    where 
     camp.pkid=? 
     and camp.fk_Content=cont.pkid 

當我嘗試在數據庫中生成的SQL查詢時,它工作正常,數據檢索成功,但在運行應用程序時,我得到異常:

Exception in thread "main" org.hibernate.exception.SQLGrammarException: could not execute query 
    at org.hibernate.exception.SQLStateConverter.convert(SQLStateConverter.java:92) 
    at org.hibernate.exception.JDBCExceptionHelper.convert(JDBCExceptionHelper.java:66) 
    at org.hibernate.loader.Loader.doList(Loader.java:2297) 
    at org.hibernate.loader.Loader.listIgnoreQueryCache(Loader.java:2172) 
    at org.hibernate.loader.Loader.list(Loader.java:2167) 
    at org.hibernate.loader.custom.CustomLoader.list(CustomLoader.java:316) 
    at org.hibernate.impl.SessionImpl.listCustomQuery(SessionImpl.java:1832) 
    at org.hibernate.impl.AbstractSessionImpl.list(AbstractSessionImpl.java:165) 
    at org.hibernate.impl.SQLQueryImpl.list(SQLQueryImpl.java:179) 
    at org.hibernate.impl.AbstractQueryImpl.uniqueResult(AbstractQueryImpl.java:859) 
    at com.xeno.xecamp.desktopManagement.Main.getCampaignSMSs(Main.java:43) 
    at com.xeno.xecamp.desktopManagement.Main.main(Main.java:18) 
Caused by: java.sql.SQLException: Column 'fk_Content' not found. 
    at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:1073) 
    at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:987) 
    at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:982) 
    at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:927) 
    at com.mysql.jdbc.ResultSetImpl.findColumn(ResultSetImpl.java:1144) 
    at com.mysql.jdbc.ResultSetImpl.getBigDecimal(ResultSetImpl.java:1414) 
    at org.hibernate.type.BigIntegerType.get(BigIntegerType.java:57) 
    at org.hibernate.type.NullableType.nullSafeGet(NullableType.java:184) 
    at org.hibernate.type.NullableType.nullSafeGet(NullableType.java:210) 
    at org.hibernate.loader.custom.CustomLoader$ScalarResultColumnProcessor.extract(CustomLoader.java:501) 
    at org.hibernate.loader.custom.CustomLoader$ResultRowProcessor.buildResultRow(CustomLoader.java:447) 
    at org.hibernate.loader.custom.CustomLoader.getResultColumnOrRow(CustomLoader.java:344) 
    at org.hibernate.loader.Loader.getRowFromResultSet(Loader.java:647) 
    at org.hibernate.loader.Loader.doQuery(Loader.java:745) 
    at org.hibernate.loader.Loader.doQueryAndInitializeNonLazyCollections(Loader.java:270) 
    at org.hibernate.loader.Loader.doList(Loader.java:2294) 
    ... 9 more 

請指教爲什麼我收到異常。

更新:這是一個第三方應用程序,它連接到另一個應用程序的數據庫。

hibernate.hbm2ddl.auto=create-drop

+0

我可能是錯的..但是你可以重命名fk_Content到別的東西,看看它是否起作用?這只是fk_傳統上被發現爲外鍵約束,我不知道Hibernate是否正在專門處理它。 – gbvb

回答

5

得到它的工作,我需要使用addScalar如下:

.createSQLQuery(
         "select camp.fk_Content as contentId,camp.tk_DistributionGroup as distributionGroupId,cont.tk_contentSubtype as contentSubTypeId " 
           + "from campaign camp,content cont" 
           + " where camp.pkid=:campaignId and camp.fk_Content=cont.pkid") 
       .addScalar("contentId") 
       .addScalar("distributionGroupId") 
       .addScalar("contentSubTypeId") 
5

其他的應用價值。爲了正確地從你要求什麼數據庫中獲取,那Hibernate使用實體類應與100%的數據庫表。

您有專欄fk_Content但專用字段是contentId。只是使用as不會得到您想要的結果。如果您想使用不同的名稱(如您所做的那樣),則需要使用@Column(name = "")提供適當的列名稱進入休眠狀態。此外,不建議使用基本數據類型。您的CampaignData類看起來像:

@Entity 
@Table(name = "campaign") 
public class CampaignData { 

    private Long contentId; 
    private Long contentSubTypeId; 
    private Long distributionGroupId; 

    @Id 
    @GeneratedValue(strategy=GenerationType.IDENTITY) 
    @Column(name = "pkid", unique = true, nullable = false) 
    public Long getContentId() { 
     return this.contentId; 
    } 

    public void setContentId(Long contentId){ 
     this.contentId = contentId; 
    } 

    @Column(name = "fk_Content") 
    public Long getContentSubTypeId() { 
     return this.contentSubTypeId; 
    } 

    public void setContentSubTypeId(Long contentSubTypeId){ 
     this.contentSubTypeId= contentSubTypeId; 
    } 

    @Column(name = "tk_DistributionGroup") 
    public Long getDistributionGroupId() { 
     return this.distributionGroupId; 
    } 

    public void setDistributionGroupId(Long distributionGroupId){ 
     this.distributionGroupId= distributionGroupId; 
    } 
} 

這應該這樣做。此外,請嘗試學習使用Hibernate's Criteria。這比硬編碼的SQL語句更好。

+0

我上面的代碼是一個sql查詢,我將值設置爲別名並將別名轉換爲一個bean。 –