奇怪的java.sql.SQLException:找不到列'fk_Content'

时间:2012-05-15 12:03:01

标签: java mysql hibernate

我在MySQL数据库中有两个表:

1-广告系列

  • pkid :主键。
  • fk_Content :内容表上的外键。
  • tk_DistributionGroup :类型表distribution_group上的外键。

2-内容:

  • pkid :主键。
  • tk_contentSubtype :类型表distribution_list上的外键。

我有一个叫做CampaignData的java bean(不是hibernate实体)

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

的其他应用程序值

2 个答案:

答案 0 :(得分: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语句更好的做法。

答案 1 :(得分: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")