2012-05-15 33 views
0

我在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

+0

我可能是错的..但是你可以重命名fk_Content到别的东西,看看它是否起作用?这只是fk_传统上被发现为外键约束,我不知道Hibernate是否正在专门处理它。 – gbvb

回答

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") 
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语句更好。

+0

我上面的代码是一个sql查询,我将值设置为别名并将别名转换为一个bean。 –