我试图从两个不同的表中查询数据库,即
query = "select rbt.rbtCode, rbt.maskedName, cmas.catId, cmas.maskedName " +
"from CrbtRbt rbt, CrbtCategoryMaster cmas " +
"where rbt.playable='Y' and rbt.showOnWeb='Y' and rbt.rbtCode!=0 " +
"and rbt.catId=cmas.catId";
但是当我通过session.createQuery(query);
它将包名称附加到即将到来的实体名称中的下一个,即
select rbt.rbtCode, rbt.maskedName, cmas.catId, cmas.maskedName
from CrbtRbt rbt, com.telemune.toolGeneratedPojos.CrbtCategoryMaster cmas
where rbt.playable='Y' and rbt.showOnWeb='Y' and rbt.rbtCode!=0 and rbt.catId=cmas.catId
并给出以下例外:
org.hibernate.hql.internal.ast.QuerySyntaxException: CrbtRbt is not mapped [select rbt.rbtCode, rbt.maskedName, cmas.catId, cmas.maskedName from CrbtRbt rbt, com.telemune.toolGeneratedPojos.CrbtCategoryMaster cmas where rbt.playable='Y' and rbt.showOnWeb='Y' and rbt.rbtCode!=0 and rbt.catId=cmas.catId]
at org.hibernate.hql.internal.ast.util.SessionFactoryHelper.requireClassPersister(SessionFactoryHelper.java:180)
at org.hibernate.hql.internal.ast.tree.FromElementFactory.addFromElement(FromElementFactory.java:110)
at org.hibernate.hql.internal.ast.tree.FromClause.addFromElement(FromClause.java:93)
at org.hibernate.hql.internal.ast.HqlSqlWalker.createFromElement(HqlSqlWalker.java:325)
at org.hibernate.hql.internal.antlr.HqlSqlBaseWalker.fromElement(HqlSqlBaseWalker.java:3252)
at org.hibernate.hql.internal.antlr.HqlSqlBaseWalker.fromElementList(HqlSqlBaseWalker.java:3141)
at org.hibernate.hql.internal.antlr.HqlSqlBaseWalker.fromClause(HqlSqlBaseWalker.java:694)
at org.hibernate.hql.internal.antlr.HqlSqlBaseWalker.query(HqlSqlBaseWalker.java:550)
at org.hibernate.hql.internal.antlr.HqlSqlBaseWalker.selectStatement(HqlSqlBaseWalker.java:287)
at org.hibernate.hql.internal.antlr.HqlSqlBaseWalker.statement(HqlSqlBaseWalker.java:235)
at org.hibernate.hql.internal.ast.QueryTranslatorImpl.analyze(QueryTranslatorImpl.java:248)
at org.hibernate.hql.internal.ast.QueryTranslatorImpl.doCompile(QueryTranslatorImpl.java:183)
at org.hibernate.hql.internal.ast.QueryTranslatorImpl.compile(QueryTranslatorImpl.java:136)
at org.hibernate.engine.query.spi.HQLQueryPlan.<init>(HQLQueryPlan.java:101)
at org.hibernate.engine.query.spi.HQLQueryPlan.<init>(HQLQueryPlan.java:80)
at org.hibernate.engine.query.spi.QueryPlanCache.getHQLQueryPlan(QueryPlanCache.java:119)
at org.hibernate.internal.AbstractSessionImpl.getHQLQueryPlan(AbstractSessionImpl.java:214)
at org.hibernate.internal.AbstractSessionImpl.createQuery(AbstractSessionImpl.java:192)
at org.hibernate.internal.SessionImpl.createQuery(SessionImpl.java:1537)
at com.telemune.generator.TestQuery.select(TestQuery.java:114)
at com.telemune.generator.PojoGenerator.main(PojoGenerator.java:191)
但是如果我逐个查询实体,它会成功显示所需的结果。
即
select rbt.rbtCode, rbt.maskedName
from CrbtRbt rbt
where rbt.playable='Y' and rbt.showOnWeb='Y' and rbt.rbtCode!=0;
给出了期望的结果。
任何人都可以解释我能做些什么吗?
这是一些示例代码: 查询代码
hql = session.createQuery(query);
hql.setMaxResults( querySchema.getMaxRes() );
list=hql.list();
有关我已完成所有映射的信息,并且非常小心地包含了所有库,没有任何错误。
更新
正如我所说,我已仔细完成了映射,但有人可能会有疑问,所以这里是映射:
<hibernate-mapping>
<class name="com.telemune.toolGeneratedPojos.CrbtCategoryMaster" schema="SDP" table="CRBT_CATEGORY_MASTER">
<id name="catId" type="java.lang.Integer">
<column name="CAT_ID" precision="4" scale="0" />
<generator class="assigned" />
</id>
<property column="SHOW_IN_SMS" name="showInSms" type="java.lang.String" />
<property column="SHOW_ON_WEB" name="showOnWeb" type="java.lang.String" />
<property column="PLAYABLE" name="playable" type="java.lang.String" />
<property column="STATUS" name="status" type="java.lang.String" />
<property column="IMAGE_PATH" name="imagePath" type="java.lang.String" />
<property column="DESCRIPTION" name="description" type="java.lang.String" />
<property column="MASKED_NAME" name="maskedName" type="java.lang.String" />
<property column="IVR_FILEPATH_1" name="ivrFilepath1" type="java.lang.String" />
<property column="IVR_FILEPATH" name="ivrFilepath" type="java.lang.String" />
<property column="MASKED_NAME_1" name="maskedName1" type="java.lang.String" />
</class></hibernate-mapping>
和CRBT_RBT:
<hibernate-mapping>
<class name="com.telemune.toolGeneratedPojos.CrbtRbt" schema="SDP" table="CRBT_RBT">
<id name="rbtCode" type="java.lang.Integer">
<column name="RBT_CODE" precision="10" scale="0" />
<generator class="assigned" />
</id>
<property column="PLAYABLE" name="playable" type="java.lang.String" />
<property column="OTHER" name="other" type="java.lang.Integer" />
<property column="IMAGE_PATH" name="imagePath" type="java.lang.String" />
<property column="RBT_ORDER" name="rbtOrder" type="java.lang.Integer" />
<property column="VALIDITY_PERIOD" name="validityPeriod" type="java.lang.Integer" />
<property column="LYRICIST" name="lyricist" type="java.lang.String" />
<property column="APPROVED_BY" name="approvedBy" type="java.lang.String" />
<property column="PREV_CAT_ID" name="prevCatId" type="java.lang.Integer" />
<property column="RELEASE_YEAR" name="releaseYear" type="java.lang.Integer" />
<property column="COMPOSER" name="composer" type="java.lang.String" />
<property column="SHOW_ON_WEB" name="showOnWeb" type="java.lang.String" />
<property column="MASKED_NAME" name="maskedName" type="java.lang.String" />
<property column="CONTENT_PROVIDER_CODE" name="contentProviderCode" type="java.lang.Integer" />
<property column="CREATE_DATE" name="createDate" type="java.sql.Date" />
<property column="IVR_FILEPATH" name="ivrFilepath" type="java.lang.String" />
<property column="CORP_ID" name="corpId" type="java.lang.Integer" />
<property column="SHOW_IN_SMS" name="showInSms" type="java.lang.String" />
<property column="ALBUM_NAME" name="albumName" type="java.lang.String" />
<property column="NOKIA" name="nokia" type="java.lang.Integer" />
<property column="STATUS" name="status" type="java.lang.String" />
<property column="FILE_PATH" name="filePath" type="java.lang.String" />
<property column="REFERENCE_ID" name="referenceId" type="java.lang.Integer" />
<property column="CHARGING_CODE" name="chargingCode" type="java.lang.Integer" />
<property column="RBT_NICK" name="rbtNick" type="java.lang.String" />
<property column="APPROVAL_DATE" name="approvalDate" type="java.sql.Date" />
<property column="CAT_ID" name="catId" type="java.lang.Integer" />
<property column="RBT_SCORE" name="rbtScore" type="java.lang.Integer" />
<property column="ARTIST_NAME" name="artistName" type="java.lang.String" />
</class>
</hibernate-mapping>
我不认为映射中有任何错误。
Hibernate.cfg.xml:
<hibernate-configuration>
<session-factory>
<property name="hibernate.connection.driver_class">oracle.jdbc.driver.OracleDriver</property>
<property name="hibernate.connection.password">sdp</property>
<property name="hibernate.connection.url">jdbc:oracle:thin:@10.168.2.127:1521:mastera</property>
<property name="hibernate.connection.username">sdp</property>
<property name="hibernate.dialect">org.hibernate.dialect.OracleDialect</property>
<mapping resource="com/telemune/toolGeneratedPojos/CrbtRbt.hbm.xml"/>
<mapping resource="com/telemune/toolGeneratedPojos/CrbtCategoryMaster.hbm.xml"/>
</session-factory>
</hibernate-configuration>
答案 0 :(得分:0)
好吧,我明白了。
List<MyObject> list = services.stream()
.flatMap(s -> s.getObjects().stream())
.collect(Collectors.toList());
return configuration.buildSessionFactory(serviceRegistry);
这是在创建会话工厂时为每个类以编程方式添加.hbm.xml文件的代码。
SessionFactory getSessionFactory(className) {
try {
configuration.addResource( className + ".hbm.xml" );
}catch (Exception e) {
e.printStackTrace();
}
configuration.configure();
serviceRegistry = new ServiceRegistryBuilder().applySettings(configuration.getProperties()).buildServiceRegistry();
此代码的问题是:
1。正在为每次调用(getSessionFactory(classname))构建一个新的会话工厂。
2。每次都会打开一个新会话。 因此,只添加了配置的最后一个类的映射作为资源添加,这就是为什么它通过将packageName附加到它的名称来搜索其他类。
我做的是: 我只计算了包中的映射文件的数量,并在同一会话工厂中的循环的帮助下添加它们。 它起作用了。
这是方法:
try {
sessionFactory = ...getSessionFactory(className);
session = sessionFactory.openSession();
}
catch (Exception e) {
e.printStackTrace();
}
这只是一个愚蠢的错误。