hql = "select rm.* from remoteunit rm inner join mo m on rm.MOID = m.ID left join
orgmomapping o on rm.MOID = o.MOID join organization org on org.ID = o.ORGANIZATIONID
where m.DOMAINID = :id";
query = session.createSQLQuery(hql);
此查询在使用
查询时返回对象列表remoteUnitList = (ArrayList<RemoteUnitDetails>)query.list();
但是当我试图获得一个对象时
RemoteUnitDetails details = (RemoteUnitDetails)remoteUnitsList.get(i);
我得到了类强制转换异常。
我使用的是MySQL数据库。在hibernate中使用条件时,我无法匹配orgmomapping和remoteunit表。因为
orgmomapping -> id, organizationId, remoteUnitId
remoteunit -> id
此远程单位ID可能在orgmomapping中存在,也可能不存在。
请提出解决方案
由于
更新
RemoteUnitDetails.hbm.xml
<hibernate-mapping>
<class name="---.RemoteUnitDetails" table="remoteunit"
lazy="false">
<id name="id" column="ID">
<generator class="increment" />
</id>
<many-to-one name="remoteUnit" column="MOID"
class="---.RemoteUnit" not-null="true"></many-to-one>
<property name="address" column="ADDRESS" />
<property name="postalCode" column="POSTALCODE" />
<property name="city" column="CITY" />
<property name="region" column="REGION" />
<property name="visible" column="VISIBLE" />
<property name="lowPowerMode" column="LOWPOWERMODE" />
<property name="wakeUpInterval" column="WAKEUPINTERVAL" />
<property name="awakeInterval" column="AWAKEINTERVAL" />
<property name="resetAtNextInterval" column="RESETATNEXTINTERVAL" />
<property name="logInterval" column="LOGINTERVAL" />
<property name="sendIntervalLogData" column="SENDINTERVALLOGDATA" />
<property name="sendIntervalAttr1" column="SENDINTERVALATTR1" />
<property name="sendIntervalAttr2" column="SENDINTERVALATTR2" />
</class>
</hibernate-mapping>
RemoteUnit.hbm.xml
<hibernate-mapping>
<class name="---.RemoteUnit" table="mo" lazy="false">
<id name="id" column="ID">
<generator class="increment" />
</id>
<property name="name" />
<property name="aliasName" column="SCREENNAME" />
<property name="description" column="DESCRIPTION" />
<property name="isActive" column="ISLIVE" />
<many-to-one name="domain" column="DOMAINID"
class="---.Domain" not-null="true" />
<!-- Foreign key -->
<many-to-one name="moType" column="MOTYPEID"
class="---.MoType" not-null="true" />
</class>
</hibernate-mapping>
OrgMoMapping.hbm.xml
<hibernate-mapping>
<class name="---.OrgRemoteUnitMapping" table="orgmomapping"
lazy="false">
<id name="id" column="ID">
<generator class="identity" />
</id>
<many-to-one name="organization" column="ORGANIZATIONID"
class="---.Organization" not-null="true"></many-to-one>
<many-to-one name="remoteUnit" column="MOID"
class="---.RemoteUnit" not-null="true"></many-to-one>
</class>
</hibernate-mapping>
答案 0 :(得分:0)
我不建议您为此目的使用本机SQL,因为您可以使用条件API轻松实现此目的。你可以做这样的事情。它可能对你有帮助。
ProjectionList projections = Projections.projectionList();
projections.add(Projections.property("column.columnName"), "columnName");
Criteria criteria = getCurrentSession().createCriteria(<<remoteunit>>.class, "remoteunit");
criteria.createAlias("remoteunit.<<remoteunit_to_mo_joinfield>>", "mo", JoinType.INNER_JOIN);
criteria.createAlias("mo.<<mo_to_orgmomapping_join_field>>", "orgmomapping", JoinType.LEFT_OUTER_JOIN);
criteria.setProjection(projections);
criteria.setResultTransformer(Criteria.ALIAS_TO_ENTITY_MAP);
List<Map<String,? extends Object>> list = criteria.list();