简体   繁体   中英

Strange java.sql.SQLException: Column 'fk_Content' not found

i have two tables in MySQL Database:

1- campaign:

  • pkid : primary key.
  • fk_Content : foreign key on content table.
  • tk_DistributionGroup : foreign key on type table distribution_group.

2- content:

  • pkid : primary key.
  • tk_contentSubtype : foreign key on type table distribution_list.

I have java bean (not hibernate entity) called CampaignData

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:

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

In order to properly get from the database what you request, the Entity classes that hibernate uses should match 100% the database tables.

You have the column fk_Content but the private field is contentId . Just using the as won't get you the desired result. If you want to use different names (like you did), you need to provide hibernate with the proper column names, using @Column(name = "") . Furthermore, using basic data types is not recommended. Your CampaignData class would look like:

@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 . It's a far more better practice than hard-coded SQL statements.

to get it to work i needed to use addScalar as follows:

.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")

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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