Mah*_*leh 0 java mysql hibernate
我在MySQL数据库中有两个表:
1-活动:
2-内容:
我有一个名为CampaignData的java bean(不是hibernate实体)
public class CampaignData {
private long contentId;
private long contentSubTypeId;
private Long distributionGroupId;
}
Run Code Online (Sandbox Code Playgroud)
这是我如何进行查询:
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();
Run Code Online (Sandbox Code Playgroud)
它产生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
Run Code Online (Sandbox Code Playgroud)
当我在数据库中尝试生成的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
Run Code Online (Sandbox Code Playgroud)
请告诉我为什么我会得到例外.
更新:这是第三方应用程序,它在数据库上连接另一个应用程序.
另一个应用价值 hibernate.hbm2ddl.auto=create-drop
为了从数据库中正确地获取您请求的内容,hibernate使用的Entity类应该匹配100%的数据库表.
您有列,fk_Content但私有字段是contentId.只是使用as将无法获得所需的结果.如果你想使用不同的名称(就像你一样),你需要使用正确的列名为hibernate提供@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;
}
}
Run Code Online (Sandbox Code Playgroud)
这应该做到这一点.另外,尝试学习使用Hibernate的Criteria.这是一种比硬编码SQL语句更好的做法.
为了让它工作,我需要使用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")
Run Code Online (Sandbox Code Playgroud)