简体   繁体   English

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

[英]Strange java.sql.SQLException: Column 'fk_Content' not found

i have two tables in MySQL Database: 我在MySQL数据库中有两个表:

1- campaign: 1-广告活动:

  • pkid : primary key. pkid :主键。
  • fk_Content : foreign key on content table. fk_Content :内容表上的外键。
  • tk_DistributionGroup : foreign key on type table distribution_group. tk_DistributionGroup :类型表distribution_group上的外键。

2- content: 2-内容:

  • pkid : primary key. pkid :主键。
  • tk_contentSubtype : foreign key on type table distribution_list. tk_contentSubtype :类型表distribution_list上的外键。

I have java bean (not hibernate entity) called CampaignData 我有一个名为CampaignData的Java Bean(不是休眠实体)

public class CampaignData {

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

}

here's how i am doing the query: 这是我做查询的方式:

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();

which produces the hibernate query: 产生休眠查询:

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

when i try the produced SQL query in the database, it works fine and data is retrieved successfully, but when running the application i get the exception: 当我在数据库中尝试产生的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

please advise why i am getting the exception. 请告诉我为什么我要例外。

UPDATE: this is a 3rd party application that connects on the database for another application. 更新:这是一个第三方应用程序,它在数据库上连接了另一个应用程序。

the other application value for hibernate.hbm2ddl.auto=create-drop hibernate.hbm2ddl.auto=create-drop的另一个应用程序值

In order to properly get from the database what you request, the Entity classes that hibernate uses should match 100% the database tables. 为了正确地从数据库中获取您所请求的内容,休眠使用的Entity类应与数据库表100%匹配。

You have the column fk_Content but the private field is contentId . 您有fk_Content列,但私有字段是contentId Just using the as won't get you the desired result. 仅使用as不会获得理想的结果。 If you want to use different names (like you did), you need to provide hibernate with the proper column names, using @Column(name = "") . 如果您想使用其他名称(就像您一样),则需要使用@Column(name = "")为hibernate提供适当的列名称。 Furthermore, using basic data types is not recommended. 此外,不建议使用基本数据类型。 Your CampaignData class would look like: 您的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;
    }
}

This should do it. 这应该做。 Also, try learning to use Hibernate's Criteria . 另外,尝试学习使用Hibernate的条件 It's a far more better practice than hard-coded SQL statements. 这比硬编码的SQL语句好得多。

to get it to work i needed to use addScalar as follows: 为了使其正常工作,我需要使用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")

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM