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