我在 MySQL 数据库中有两个表:
1- 活动:
- pkid:主键。
- fk_Content:内容表上的外键。
- tk_DistributionGroup:类型表distribution_group的外键。
2-内容:
- pkid:主键。
- tk_contentSubtype:类型表distribution_list上的外键。
我有名为 CampaignData 的 java bean(不是休眠实体)
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();
产生休眠查询:
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
请告知我为什么会遇到异常。
更新:这是一个第 3 方应用程序,它连接到另一个应用程序的数据库。
其他应用价值hibernate.hbm2ddl.auto=create-drop